We have a requirement in the App in which for certain countries/regions we have to support only certain languages. If the User's current language is not supported, We would have a custom fallback/default language for specific regions. like it could be Arabic for middle east countries, English for Europe etc.
Since iOS localization allows fallback to only one Base localizations, we had to take a custom approach to implement the same.
To achieve the same, we checked the User's current language and Country and if required we overrode the "kLanguageBundleKey" property within the Bundle class, and
set the following user defaults, (here for example say english) Foundation.UserDefaults.standard.set(["en"], forKey: "AppleLanguages") Foundation.UserDefaults.standard.synchronize().
And it works!
The problem is that in iOS, post this override, when the user changes language from the Phone's settings app. Launches our app, Bundle.main.preferredLocalizations.first always gives what we overwrote and not the one present in the Phone's settings.
Given this,
Is there way to get the user's current language even after such overrides? or should we do the language overrides some other way?
If the User's current language is not supported, We would have a custom fallback/default language for specific regions. like it could be Arabic for middle east countries, English for Europe etc.
Assuming you are using the standard localization mechanism that manages the localized resources with .lproj
folders in the app bundle, I don't see any supported way to custom fallback/default language for specific regions, and so would suggest that you file a feedback report – If you do so, please share your report ID here.
set the following user defaults, (here for example say english) Foundation.UserDefaults.standard.set(["en"], forKey: "AppleLanguages") Foundation.UserDefaults.standard.synchronize().
AppleLanguages
is a private API. It isn't a surprise to me that changing this user defaults key breaks the process the system uses to determine the preferred localization. Also, apps that change the key will risk at getting rejected in the App Review process, and so I'd suggest that you avoid doing that.
In your case, I think the path will still be that your app bundle provides appropriate .lproj
folders for all your target languages / regions so the system loads the resources from the folders, even though the resources there may not completely localized.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.