Created
September 17, 2018 12:53
-
-
Save MarcoSantarossa/d2236c1570cc95f99874db52f41acbde to your computer and use it in GitHub Desktop.
Localization
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
protocol Localizable { | |
associatedtype LocalizableKey | |
static var tableName: String? { get } | |
static func localize(key: LocalizableKey, localizer: LocalizerType) -> String | |
} | |
extension Localizable where LocalizableKey: RawRepresentable { | |
static var tableName: String? { | |
return String(describing: Self.self) | |
} | |
static func localize(key: LocalizableKey, localizer: LocalizerType) -> String { | |
return localizer.localize("\(key.rawValue)", tableName: self.tableName) | |
} | |
} | |
protocol LocalizerType: class { | |
func localize(_ value: String, tableName: String?) -> String | |
} | |
final class Localizer: LocalizerType { | |
func localize(_ string: String, tableName: String?) -> String { | |
return NSLocalizedString(string, tableName: tableName, value: "**\(string)**", comment: "") | |
} | |
} | |
struct LocalizableTest: Localizable { | |
enum LocalizableKey: String { | |
case testKey = "test.key" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment