preferredLanguages iOS 9

前端 未结 5 1908
轻奢々
轻奢々 2020-12-11 09:36

in order to localize my App, I use the following code:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];  
             if ([language is         


        
相关标签:
5条回答
  • 2020-12-11 09:59

    If language is returning other values such a "fr-FR" and "fr-CA", then you should split language on the - character. This will work even you simply get "fr".

    NSString *firstLanguage = [[NSLocale preferredLanguages] firstObject];
    NSString *language = [[firstLanguage componentsSeparatedByString:@"-"] firstObject];
    if ([language isEqualToString:@"fr"]) {
    } else {
    }
    
    0 讨论(0)
  • 2020-12-11 10:05

    My fix on my code is simple :

    NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSRange range = [language rangeOfString:@"-" options:NSBackwardsSearch];
    NSString * languageMark = [language substringToIndex:range.location];
    
    0 讨论(0)
  • 2020-12-11 10:09

    You shouldn't split the NSLocale. NSLocale has some Keys, which you can retrieve with objectForKey:

    In your example you can write the following:

    [[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]
                isEqualToString:@"fr"]
    

    [NSLocale currentLocale] is actually the same as [[NSLocale preferredLanguages] firstObject] but has some additional information where you can retrieve which decimal separator should be used or which currency symbol.

    Other relevant Keys can be found in the class reference from apple.

    0 讨论(0)
  • 2020-12-11 10:15

    Hey there Don't do like that It will create issue when localization are set different for language having different regions as like es-mx and es-cl. In that case above solution will create issue. Use following code for the solution:

    NSString *selectedLanguage = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
    

    In case your have selected zh-Hans-US from setting But your app is having zh-Hans-TW and zh-Hans-CN only. That the above code will return you "zh-Hans-TW" the first preferred Localization.

    Try this may be useful to you.

    0 讨论(0)
  • 2020-12-11 10:22

    Paste my codes in here, maybe help you

    - (NSString *)localizedStringForKey:(NSString *)key withDefault:(NSString *)defaultString
    {
        static NSBundle *bundle = nil;
        if (bundle == nil)
        {
            NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"your_resources" ofType:@"bundle"];
            bundle = [NSBundle bundleWithPath:bundlePath] ?: [NSBundle mainBundle];
            //manually select the desired lproj folder
            for (NSString *language in [NSLocale preferredLanguages])
            {
                for (NSString *loc in [bundle localizations] ) {
                    if ([language hasPrefix:loc])
                    {
                        bundlePath = [bundle pathForResource:loc ofType:@"lproj"];
                        bundle = [NSBundle bundleWithPath:bundlePath];
                        goto getString;
                    }
                }
            }
        }
    getString:
        defaultString = [bundle localizedStringForKey:key value:defaultString table:nil];
        return [[NSBundle mainBundle] localizedStringForKey:key value:defaultString table:nil];
    }
    
    0 讨论(0)
提交回复
热议问题