preferredLanguages iOS 9

2019-01-20 01:09发布

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

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];  
             if ([language isEqualToString:@"fr"]) {  
}else{  
}  

But since iOS 9, I'have to replace "fr" by "fr-FR". The problem is that only works for France. How can I support all the other regions (Canada, Belgium,..) ? and the "general setting" for french ?

Thanks

5条回答
▲ chillily
2楼-- · 2019-01-20 01:14

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 {
}
查看更多
家丑人穷心不美
3楼-- · 2019-01-20 01:17

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];
查看更多
祖国的老花朵
4楼-- · 2019-01-20 01:20

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.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-20 01:20

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];
}
查看更多
聊天终结者
6楼-- · 2019-01-20 01:36

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.

查看更多
登录 后发表回答