22

I'm developing app for Russian-speaking people. A lot of countries near Russia have the second language - Russian. My app has two localizations: Russian and English. And I need to set Russian localization like default. English should be only for people who use English like device's language.

I know that Apple recommends to use localization in priority of preferred languages in settings but I have an important reason to don't follow this recommendation.

How can I set Russian localization like default?

6 Answers 6

33

I think you need to use the Localization native development region key in your info.plist file.

enter image description here

2
  • 6
    This is the answer people!
    – hasan
    Jan 8, 2015 at 19:46
  • 1
    @hasan83 not working for me. Can u plz suggest a solution.
    – mars
    Jul 5, 2017 at 7:12
12

You probably need to set Russian as a value of CFBundleDevelopmentRegion in the Info.plist.

CFBundleDevelopmentRegion (String - iOS, OS X) specifies the native region for the bundle. This key contains a string value that usually corresponds to the native language of the person who wrote the bundle. The language specified by this value is used as the default language if a resource cannot be located for the user’s preferred region or language.

Update

If you really want and need to override the language priority of the OS, you could use AppleLanguages user defaults key (although, as you know, it is not recommended):

[[NSUserDefaults standardUserDefaults] setObject:@[@“ru”, @“en”] forKey:@“AppleLanguages”];
7
  • I've tried to do this but with no results. If you press on value of CFBundleDevelopmentRegion field in Info.plist you can see that XCode proposes only 11 languages for this parameter. I tried to manually enter "ru" but no results.
    – 12pm
    Jan 30, 2014 at 15:52
  • Your application has Russian and English localizations, and this key is set to ru, right? Then you select some other language, say Spanish, and the UI is still English? I noticed that at least in iOS 7 the OS takes into account the order of the languages in system preferences. So if you switched to Spanish after English, the order will be Spanish, English, Russian, and the app will be in English. Check if that’s the case. Switch to the unsupported language after Russian, for example.
    – eofster
    Jan 30, 2014 at 16:07
  • Yes, if order is: Spanish, Russian, English - then app uses Russian language. But for example in Ukraine a lot of people after bought an iPhone choose Ukrainian firstly and don't change the languages order ever. In this case ukrainian users have second item in order - english.
    – 12pm
    Jan 30, 2014 at 16:17
  • 2
    There can be problems with setting AppleLanguages key. You might have to set it very early, like in the main().
    – eofster
    Jan 30, 2014 at 16:22
  • 3
    This problem has 6 years and Apple did not fix that.
    – Duck
    Mar 31, 2015 at 17:18
5

In my case I have only Russian localization with hard-coded strings in my app, but I use libraries with localization bundles. Strings from libraries were in English by default. Because NSBundle.mainBundle.preferredLanguages were [ "en" ].

They became Russian after I specified app localization language in Info.plist with CFBundleLocalizations key:

screenshot

CFBundleLocalizations (Array - iOS, OS X) identifies the localizations handled manually by your app. If your executable is unbundled or does not use the existing bundle localization mechanism, you can include this key to specify the localizations your app does handle.

Each entry in this property’s array is a string identifying the language name or ISO language designator of the supported localization. See “Language and Locale Designations” in Internationalization and Localization Guide in Internationalization Documentation for information on how to specify language designators.

4

I have not found the right solution. Only one way that is suitable for my issue is to use NSLocalizedStringFromTableInBundle instead NSLocalizedString. This way doesn't allow to localize images or nibs but with strings it works great.

Firstly you need to define the current device language:

NSString *lang = (NSString *)[[NSLocale preferredLanguages] objectAtIndex:0];

Next find the bundle for this language:

NSBundle *myLangBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:lang ofType:@"lproj"]];

If bundle is found then use it in NSLocalizedStringFromTableInBundle:

_label.text = NSLocalizedStringFromTableInBundle(@"LabelText",nil,myLangBundle,nil);

Otherwise find and use default bundle.

4

As eofster said you should set AppleLanguages key in NSUserDefaults to array of languages with desired order, but in order to work at first lunch you should do it very early, even earlier than applicationWillLunchWithOptions:

As for me this solution works great and has advantage on using NSLocalizedStringFromTableInBundle because it works with storyboards and nibs as well.

Here is step by step solution on Swift language.

  1. Make subclass of UIApplication as described here: Subclass UIApplication with Swift

  2. Second override init method of you UIApplication subclass to substitute list of preferred languages on your own.

    override init() {
        if let languages = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages") as? [String],
            let language = languages.first where !language.hasPrefix("en")
        {
            NSUserDefaults.standardUserDefaults().setObject(["ru", "en"], forKey: "AppleLanguages")
        }
    
        super.init()
    }
    

    Swift 4:

    override init() {
    if let languages = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
        let language = languages.first(where:{!$0.hasPrefix("en")})
    {
        UserDefaults.standard.set(["ru", "en"], forKey: "AppleLanguages")
    }
    
    super.init()
    

    }

  3. That's all. If first preferred language is not English you'll substitute list with Russian as first language.

P.S. If you're doing not only localization, but internationalization don't forget to check locale which is used by date pickers, date formatters, number formatters, etc.

0

preferredLanguages sometimes return "ru_RU" but my lproj is ru.lproj. So I decided to another solutions. I added "test.lang.phrase = "YES"; to all my localizable strings files. I load a default localization if localization isn't found.

NSString *result = nil;
NSString *testLang = NSLocalizedString(@"test.lang.phrase", nil);
if ([testLang isEqualToString:@"YES"]) {
    result = NSLocalizedString(str, nil);
}
if (!result) {
    NSBundle *myLangBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];
    result = NSLocalizedStringFromTableInBundle(str, nil, myLangBundle,nil);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.