【问题标题】:iOS9 AppleLanguages different from older iOSiOS9 AppleLanguages 与旧版 iOS 不同
【发布时间】:2015-12-15 16:10:30
【问题描述】:

我现在如何获得实际的系统语言?似乎他们在最后一个破折号之后放置了区域后缀。所以之前cs 现在是cs-DE 如果语言是捷克语并且区域设置是德语。但是有些语言没有后缀,比如 GB 语言是 en-GB,但区域设置是德语。

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* language = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [language objectAtIndex:0];
NSLog(@"localeIdentifier: %@", preferredLang);

【问题讨论】:

    标签: ios xcode cocoa-touch ios9


    【解决方案1】:

    使用NSLocale 类中的componentsFromLocaleIdentifier 方法

    Here is the documentation

    你可以这样做:

    NSString* localeID = [NSLocale currentLocale].localeIdentifier;
    NSDictionary* components = [NSLocale componentsFromLocaleIdentifier:localeID];
    NSString* languageID = components[NSLocaleLanguageCode];
    

    编辑

    如果应用当前翻译的语言不是设备的语言,则以这种方式获取语言会产生一些问题。确实, components[NSLocaleLanguageCode] 将返回设备的语言。

    要获取应用的当前语言,您应该使用[[NSBundle mainBundle] preferredLocalizations].firstObject

    要获取设备所在的地区,你仍然可以使用components[NSLocaleCountryCode]

    【讨论】:

    • localeID 应该是 NSString 我猜
    【解决方案2】:

    我最近遇到了这个问题。根据Apple's documentation,您将获得带有区域指示符的区域设置ID,类似于iOS 9 上的[language designator]-[region designator]。 如果您只想获取语言环境 ID,我找到了一个解决方案,您可以使用
    [[NSBundle mainBundle] preferredLocalizations]

    【讨论】:

      【解决方案3】:

      还有一个解决方案,如果你们喜欢,

        NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
        NSString *currentLanguage = [languages objectAtIndex:0];
      
      
          if ([[currentLanguage componentsSeparatedByString:@"-"] count] == 2)
                  currentLanguage = [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:0];
      
         // Only for chinese Language.
          else  if ([[currentLanguage componentsSeparatedByString:@"-"] count] == 3)
              currentLanguage = [NSString stringWithFormat:@"%@-%@",  [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:0],
                                 [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:1]
                                 ];
      

      "currentLanguage" 将为您提供当前语言,以便您将其用于本地化或任何进一步的用途。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-29
        相关资源
        最近更新 更多