Created
December 3, 2019 15:03
-
-
Save ck3g/f27c08b415910bc3b9f7e18851463427 to your computer and use it in GitHub Desktop.
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
var translations: [String: [String:String]] = [ | |
"en": [ | |
"title": "Title English", | |
"content": "Hello World" | |
], | |
"de": [ | |
"title": "Title Deutsch", | |
"content": "Hallo Welt" | |
] | |
] | |
struct I18n { | |
static func translate(_ key: String, locale: String) -> String { | |
translations[locale]?[key] ?? "<missing-translation: \(locale):\(key)>" | |
} | |
static func translate(_ key: String) -> String { | |
let locale = "en" // Find default locale | |
return translate(key, locale: locale) | |
} | |
static func t(_ key: String, locale: String) -> String { | |
translate(key, locale: locale) | |
} | |
static func t(_ key: String) -> String { | |
translate(key) | |
} | |
} | |
I18n.t("title") | |
I18n.translate("title", locale: "de") | |
I18n.t("unknown-key") | |
I18n.t("title", locale: "en_GB") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment