Ios 使用NSLocale使用Xcode 6.1翻译语言代码

Ios 使用NSLocale使用Xcode 6.1翻译语言代码,ios,objective-c,xcode,nslocale,xcode6.1,Ios,Objective C,Xcode,Nslocale,Xcode6.1,自从上次更新以来,我无法将语言代码(en、fr等)翻译成各自的名称(英语、法语等) 它可以在真实设备上工作,但不能在模拟器中工作。它使用以前版本的Xcode工作。我知道发行说明中写着,在某些情况下,[NSLocale currentLocale]可能会返回en\u US,但这并不能解释为什么它们不再被“翻译”。我使用以下代码: NSString* lang = @"en"; NSLog(@"%@", [[NSLocale currentLocale] displayNam

自从上次更新以来,我无法将语言代码(en、fr等)翻译成各自的名称(英语、法语等)

它可以在真实设备上工作,但不能在模拟器中工作。它使用以前版本的Xcode工作。我知道发行说明中写着,在某些情况下,
[NSLocale currentLocale]
可能会返回
en\u US
,但这并不能解释为什么它们不再被“翻译”。我使用以下代码:

NSString* lang = @"en";
NSLog(@"%@",
    [[NSLocale currentLocale]
        displayNameForKey:NSLocaleIdentifier
        value:lang]
);
它显示
(null)
,而不是
英语

问题是我的应用程序在某些地方崩溃,因此我想知道是否有解决办法

奇怪的是,苹果公司给出的以下例子效果非常好

NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
NSString *displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];
NSLog(@"displayNameString fr_FR: %@", displayNameString);
displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"];
NSLog(@"displayNameString en_US: %@", displayNameString);

在某些情况下,[NSLocale currentLocale]可能会返回en_US 在iOS 8.1模拟器中选择的区域设置。(18512161)

解决方法是使用第一种方法。在currentLocal实例上使用objectForKey是可行的

NSString* lang = @"en_US";
NSLocale *currentLocal = [NSLocale autoupdatingCurrentLocale];
//get the localeIdentifier and create a new NSLocale.
id localIdentifier = [currentLocal objectForKey:NSLocaleIdentifier];
NSLocale *theLocal = [NSLocale localeWithLocaleIdentifier:localIdentifier];
NSLog(@"%@",[theLocal displayNameForKey:NSLocaleIdentifier
                                                  value:lang]
      );

这是一个已知的苹果问题,仅适用于iOS 8.1模拟器-不可在8.1设备上复制。请参阅以下Xcode 6.1发行说明中的问题描述:

本地化和键盘设置(包括第三方键盘)是 iOS中的Safari、地图和开发者应用程序没有正确遵守 8.1模拟器[NSLOCE currentLocale]返回en_US,并且只有英文和表情键盘可用。(1841863018512161)


有关更多详细信息,请参阅。

嗯,在我看来,您所做的就像我问题末尾的代码,但我接受它,因为它解决了我的问题。谢谢可能重复的
NSString* lang = @"en_US";
NSLocale *currentLocal = [NSLocale autoupdatingCurrentLocale];
//get the localeIdentifier and create a new NSLocale.
id localIdentifier = [currentLocal objectForKey:NSLocaleIdentifier];
NSLocale *theLocal = [NSLocale localeWithLocaleIdentifier:localIdentifier];
NSLog(@"%@",[theLocal displayNameForKey:NSLocaleIdentifier
                                                  value:lang]
      );