Adam Johns Adam Johns - 11 months ago 63
iOS Question

String localization for framework

I'm building a framework that includes UI components. I'm retrieving localized strings with:

var bundle = Bundle(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 (i.e. if I have localizations for
, the parent application must also turn on localization for
in project settings:

enter image description here

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: "")

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
it will auto-choose the
string over the
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?

Answer Source

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.