Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my iOS app only detect the current language properly on first run?

Tags:

I am localizing my iOS app, and in the Simulator it runs correctly in my chosen language every time.

When testing on my iPhone 5, it only detects the language properly the first time the app runs. Every other time I recompile and run my app on the device, it detects "en" as the language, even though I am testing with Español ("es") selected.

I detect the language using:

[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]

I've also used:

[[NSLocale preferredLanguages] objectAtIndex:0]

Same result.

If I kill the app after the first run, and restart it on the device, it continues to detect the language properly.

But if I kill the app and then recompile/restart via Xcode after the initial run, it will load with "en" (English) detected instead.

After that, killing and re-starting the app continuously detects as English unless I delete the app completely, and recompile/reinstall/run the app via Xcode. The cycle then repeats... subsequent rebuild/restart without first deleting the app from the device results in misdetection.

All other apps on my device display with Spanish language the entire time. The entire UI shows in Spanish.

UPDATE: I've now tested on my iPad (3rd gen) also running iOS 6, and am experiencing the same behavior.

UPDATE 2:

In didFinishLaunchingWithOptions, I have this code to detect language: (language is an NSString*):

language = [[NSLocale preferredLanguages] objectAtIndex:0];

Followed by this debugging statement, to compare the value I'm getting, as well as a slightly different way of detecting it, just for debugging:

NSLog(@"Detected language: %@ / %@", language, [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]);

The output shows as "Detected language: es / es" when the app works properly in Spanish mode, and then shows as "Detected language: en / en" when it doesn't. Still no idea why it decides to load as English sometimes...

UPDATE 4: I appreciate everybody's answers, and I've tried the various suggestions. Unfortunately I was unable to award the +100 bounty as none of the suggestions seemed to fix the issue. If someone does ultimate find a solution that works for me, I will award another +50 bounty to them at that time.

UPDATE 5: I have updated from Xcode 4.5 to 4.5.2, and experiencing this same issue.

UPDATE 6: I have now created a new test project from scratch, and it works perfectly fine! Obviously something must be wrong in the way my project is laid out, or perhaps in one of the data files. I guess my next journey will be to re-create the project from scratch, copying file data over one by one...

UPDATE 7 (MONTHS LATER): Sadly, I am again facing this issue after temporarily resolving it (seemingly) by painstakingly recreating my project. On first load, the language is correctly rendered, but on subsequent loads, it reverts back to English.

SOLVED See my final solution below. Thanks for the help everyone. I may dole out some of the bounty since it will go to waste anyway.

like image 233
Mason G. Zhwiti Avatar asked Nov 27 '12 07:11

Mason G. Zhwiti


People also ask

How iOS determines the language for your app?

Q: How does iOS determine the language for my app? A: To determine the language for your app, iOS considers not only the order of the user language preferences (in General > Language & Region of the Settings application) but also the localizations your app declares it supports.

How do I change the language of iOS apps?

Change The App Language in iPhone or iPad AppsTap on the Settings app on the Home screen. Scroll down, select the app you wish to change its language. Select Language under Preferred Language. Choose the language you want to use.

How do I find current device language?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").


1 Answers

I have FINALLY solved this problem after many months! Thanks to all for the help (I also had some good back and forth with an Apple developer via the dev channels).

TL;DR: I was accidentally syncing language preferences (among many other unexpected things) between devices, using my app's iCloud key value store (via MKiCloudSync)! Read on...

I am using a third-party class called MKiCloudSync, which helps with syncing [NSUserDefaults standardUserDefaults] to my app's iCloud key value store. My intention when I began using it was to let it handle some user favorites syncing in the background.

However, not understanding how standardUserDefaults works, what I didn't realize is that there are a lot of other things being written into standardUserDefaults other than just my own custom app settings!

So what was happening was this:

  1. Start the app up for the first time. Fresh standardUserDefaults in place, and the internal "AppleLanguages" key that stores the ordered list of language preferences is correct based on the current device choices.

  2. App displays properly in the designated language.

  3. In the background, MKiCloudSync syncs ALL standardUserDefaults to iCloud. Conversely, if you had run this app elsewhere, say with an English set device, that device would have also synced it's language settings up to iCloud. So now this current running app is actually having it's language preferences overwritten.

  4. BOOM ... next time the app is run, no matter what you have selected on the device, it's whatever was pulled down from iCloud that will be used as the default language!

What I plan to do to solve the issue with my next app update:

  1. Use a forked version of MKiCloudSync that allows for syncing only whitelisted key names.

  2. Add code that will do a one-time cleanup, first cleaning out the iCloud keystore for my app, then (based on this SO answer), calling this code to reset the user defaults:

    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

In my testing so far, this sort of solves the issue... unfortunately, the user would have to restart the app for the language fix to kick in. However, my guess is most users are not experiencing this issue, as they are unlikely to be using multiple devices with different default languages.

like image 69
Mason G. Zhwiti Avatar answered Sep 22 '22 23:09

Mason G. Zhwiti