-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce string providing mechanism
The proposal is that the Core SDK will return to us a dictionary with the keys and values just as they come from the backend. This is also exactly how the strings are saved in the `Localizable.strings` file, accessed through the `Localization` enum. When the Core SDK is configured and is able to provide the dictionary, we save it to a StringProviding struct. When a string is accessed through the use of `Localization`, then `Localization` knows what is the key of the string, and also specifies a fallback in case reading from the file fails. So with this same key, we can go to the StringProviding, consult the dictionary, and if the string is not there, use the file fallback, and if not use the string fallback. This means that with this simple PR, once the Core SDK provides the dictionary, we support custom locales for the whole Widgets SDK with minimal effort. The alternative is to create a model which we need to keep in sync between Core and Widgets, which seems like a big mess. MOB-2282
- Loading branch information
1 parent
47448ea
commit 343d8cd
Showing
7 changed files
with
123 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Foundation | ||
|
||
extension Localization { | ||
static func tr( | ||
_ table: String, | ||
_ key: String, | ||
_ args: CVarArg..., | ||
fallback value: String, | ||
stringProviding: StringProviding? = Glia.sharedInstance.stringProviding, | ||
bundleManaging: BundleManaging = .live | ||
) -> String { | ||
guard | ||
let stringProviding, | ||
let remoteString = stringProviding.getRemoteString(key) | ||
else { | ||
let format = bundleManaging.current().localizedString(forKey: key, value: value, table: table) | ||
return String(format: format, locale: Locale.current, arguments: args) | ||
} | ||
|
||
return remoteString | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
struct StringProviding { | ||
var getRemoteString: ((String) -> String?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import GliaWidgets | ||
|
||
final class LocalizationTests: XCTestCase { | ||
let testString = "Glia" | ||
|
||
func test_stringProvider() { | ||
let stringProviding = StringProviding(getRemoteString: { _ in self.testString }) | ||
|
||
let localizationString = Localization.tr( | ||
"", | ||
"", | ||
fallback: "", | ||
stringProviding: stringProviding | ||
) | ||
|
||
XCTAssertEqual(localizationString, testString) | ||
} | ||
|
||
func test_fallback() { | ||
let localizationString = Localization.tr( | ||
"", | ||
"", | ||
fallback: testString | ||
) | ||
|
||
XCTAssertEqual(localizationString, testString) | ||
} | ||
|
||
func test_fallbackWhenStringProvidingReturnsNil() { | ||
let stringProviding = StringProviding(getRemoteString: { _ in nil }) | ||
|
||
let localizationString = Localization.tr( | ||
"", | ||
"", | ||
fallback: testString, | ||
stringProviding: stringProviding | ||
) | ||
|
||
XCTAssertEqual(localizationString, testString) | ||
} | ||
|
||
func test_fileFromString() { | ||
let localizationString = Localization.tr( | ||
"Localizable", | ||
"alert.action.settings", | ||
fallback: "" | ||
) | ||
|
||
XCTAssertEqual(localizationString, "Settings") | ||
} | ||
|
||
func test_fileFromStringWhenStringProvidingReturnsNil() { | ||
let stringProviding = StringProviding(getRemoteString: { _ in nil }) | ||
|
||
let localizationString = Localization.tr( | ||
"Localizable", | ||
"alert.action.settings", | ||
fallback: "", | ||
stringProviding: stringProviding | ||
) | ||
|
||
XCTAssertEqual(localizationString, "Settings") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters