I'm building a framework that includes UI components. I'm retrieving localized strings with:
var bundle = Bundle(identifier: "com.my.identifier")
bundle.localizedString(forKey: "myKey", value: nil, table: nil)
The problem I'm seeing is whatever parent application integrates with my framework, must also have localization enabled for my same localizations (e.g. if I have localizations for es-ES
, the parent application must also turn on localization for es-ES
in project settings:
Otherwise my localized strings always default to english.
I have found a workaround by manually grabbing a language bundle based on the device's preferred language:
var bundle = Bundle(identifier: "com.my.identifier")
guard let deviceLanguage = Locale.preferredLanguages.first else {
return notFound
}
guard let languagePath = bundle.path(forResource: deviceLanguage, ofType: "lproj") else {
return notFound
}
guard let languageBundle = Bundle(path: languagePath) else {
return notFound
}
return NSLocalizedString(self, tableName: "Localizable", bundle: languageBundle, value:"KEY_NOT_FOUND", comment: "")
But the issue with this workaround solution is that I lose the built-in iOS language fallback (e.g. if the device preferred language is es-MX
it will auto-choose the es-ES
string over the en
string I have).
For my framework, is there a way to avoid forcing parent applications to turn on localization for all my localized strings, yet still keep iOS language fallback logic?
Why do you want your framework to use a localization not supported by the app? This is a terrible user experience.
Imagine app that uses your framework only supports English and Spanish. Now imagine that a user of the app have their device setup to use German first, and English second.
Since the app doesn't support German, the app shows English. The entire app should show English. If your app framework manages to show German in this case, the app's user experience is now very confusing because much of the app is in English but the parts supported by your framework appear in German.
tl;dr - Don't do anything. Let the default behavior work as-is. It makes for a much better user experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With