Have different fallback language than the keys used in NSLocalizedString(@"Text aka. key",@"Description")
I use everywhere NSLocalizedString(@"Text in deutsch",@"das Textfeld Text in deutsch") I have two Localizable.strings files. One for german and one for english.
What I realized now is. If you have a german iPhone, you get the german text, if you have your iPhone set to english you get the english text. 开发者_如何学运维But if you have lets say french, then you would get the german text, too because I use german as the keys, right ? Is there a way to set english as fallback language instead of the german one used everywhere in my code ? (I have so many occurences of NSLocalizedString that it would be a pain to change the keys now everywhere (in code, in Localized.string.en and in Localized.string.de))
You have to set CFBundleDevelopmentRegion
in your Info.plist
to English.
Doing so, whenever the user's preferred language is unsupported (that is other than English and German in your case), then the development language will be used.
I usually use a global func to wrap NSLocalizedString
, and also provide a fallback when there is a un-translated German phrase and you want it to fall back to English.
public func LS(_ key: String) -> String {
let value = NSLocalizedString(key, comment: "")
if value != key || NSLocale.preferredLanguages.first == "en" {
return value
}
// Fall back to en
guard
let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
let bundle = Bundle(path: path)
else { return value }
return NSLocalizedString(key, bundle: bundle, comment: "")
}
There's more about localization and I wrote more about how fallback works.
A localization (something.lproj) is always picked. The keys themselves aren't used if the l10n provides a translation; this means you can use keys like "DISCOMBOBULATE_OR_OK" which translates to "Discombobulate" in languages where it fits and "OK" in languages where the translation for "Discombobulate" is too long for the space available.
Possibilities:
- The language in Info.plist's CFBundleDevelopmentRegion is used to pick the default language.
- The phone's fallback language is influenced by where you buy it (For example, on my languages screen, "English" is always displayed either first or second.)
It's worth asking someone with a UK/US/AU phone to test.
精彩评论