Skip to content

Instantly share code, notes, and snippets.

@MarcoSantarossa
Created September 17, 2018 12:53
Show Gist options
  • Save MarcoSantarossa/d2236c1570cc95f99874db52f41acbde to your computer and use it in GitHub Desktop.
Save MarcoSantarossa/d2236c1570cc95f99874db52f41acbde to your computer and use it in GitHub Desktop.
Localization
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