0

in Xcode 4.6.3 I set the "Localization native development region" = en.

I provide my app with Spanish, English and Russian locales.

Unfortunately when I set the Language of the Iphone Simulator to for example "German" my app uses then the Spanish locale.

I suspect because I have a "Spanish Computer".

But I would like that the customers of my app get English if their locale is not provided by the app.

How can I ensure this?

One thing I noticed. In the "copy bundle resources" tab I see my other Localizable.strings files appear red. But I know that they are being despite red deployed...

djromero
  • 19,551
  • 4
  • 71
  • 68
jack
  • 1,861
  • 4
  • 31
  • 52

2 Answers2

7

Actual language which the app will have has a dynamic nature because it depends on a dynamic list in device settings. iOS will fall down through it and choose the 1st supported language.

Let's consider some app supports "en" and "ru". Despite that fact that "en" is much more preferable language for Portuguese locale, "ru" will be chosen because it situated higher than "en" in device settings on the screenshot below.

enter image description here

Pavel Osipov
  • 2,067
  • 1
  • 19
  • 27
2

I had the same issue with one of my apps. What I did is force en as the second language in AppleLanguages and it's working now. You have to do that before the application starts (main). It's a bit of an ugly hack but works for now (NSLocalizedString is looking at that list to determine the language)... You have to be careful though, because you may get some weird stuff from NSLocale after you do that. For example if you want to use the - displayNameForKey:value: it won't be in sync and you'll have to restart the app so you get the correct result. so what you can do is initialize NSLocale with the first element in the [NSLocale preferredLanguages] (NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale preferredLanguages][0]];) and then it's going to return the expected results.

Here's what you have to do to swap the languages:

// remove what was previously stored in NSUserDefaults (otherwise the previously selected language will be still the first one in the list and your app won't be localized in the language selected in settings)
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; // make the change immediate

// reorder the languages so English is always the default fallback
NSMutableArray *mutableLanguages = [[NSLocale preferredLanguages] mutableCopy];
NSInteger enIndex = NSNotFound;
for (NSString *lang in mutableLanguages) { if ([lang isEqualToString:@"en"]) { enIndex = [mutableLanguages indexOfObject:lang]; break; } }
@try {
    if ((enIndex != 0) && (enIndex != 1)) { [mutableLanguages exchangeObjectAtIndex:1 withObjectAtIndex:enIndex]; }
} @catch (NSException *exception) {

}
// save the changes
[[NSUserDefaults standardUserDefaults] setObject:mutableLanguages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; // to make the change immediate

return UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegate class]));
Ivan Genchev
  • 2,746
  • 14
  • 25
  • well I'm a little afraid of using this hack. Do you have a explanation on why IOS is not picking the correct Locale given in "Localization native development region" . I use a value of "en" because the folder is called en.lproj – jack Nov 27 '13 at 15:18
  • I have no idea why it's not using the default fallback... From some point (don't know exactly when they started using that) the fallback language is just the second language in the "preferredLanguages" array... That's why I started using this hack in the first place. – Ivan Genchev Nov 27 '13 at 15:27
  • According to the docs (https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Articles/InternatSupport.html) it's actually the default behaviour - if your list has "de" and "es" as fallback, the app will be displayed in "es" if you don't support "es" then it will be in "en", which is the "localization used during development of the software". – Ivan Genchev Nov 27 '13 at 15:40
  • well I looked at the docs.thanks. So to be exact I have 3 Localizable.strings in folders en.lproj , de.lproj and fr.lproj "Localization native development region" = English. My Development environment Xcode is English. But When I set the simulator to polski the chosen language is german. When reading the docs this should be english. So maybe your hack is the only way to succeed? – jack Nov 27 '13 at 15:49
  • As far as I understood the docs, it shouldn't be english - it should be german if german is the second language in the list. If you select german and after that polish, then german is going to be the second language in the list and it's going to be used as a fallback. If you didn't have german translation then it would be the "Localization native development region". – Ivan Genchev Nov 27 '13 at 15:53
  • just to make sure you mean the CFBundleLocalizations de en fr in the app.plist file ? – jack Nov 27 '13 at 16:13
  • 1
    No I mean the order of the array you get from [NSLocale preferredLanguages]. The order is the same in the settings application of the phone. That is why if you select german and after that select polish the translations are in german. If you select english, change the language of the phone, then change it to polish and assuming you have en.lproj your app is going to be translated in english and not german, but if you select german and after that polish, then your app is going to be translated in german and not english. – Ivan Genchev Nov 27 '13 at 16:20
  • well that makes sense. So I have to resort to your hack – jack Nov 27 '13 at 16:23
  • 1
    I have Tried that on my main.m file but not working..first time i lanuch then it's not converted after 2nd launch it's working – Maulik shah Mar 29 '19 at 05:47