【问题标题】:Translate language code with Xcode 6.1 using NSLocale使用 NSLocale 使用 Xcode 6.1 翻译语言代码
【发布时间】:2014-12-21 14:33:22
【问题描述】:

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

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

NSString* lang = @"en";
NSLog(@"%@",
    [[NSLocale currentLocale]
        displayNameForKey:NSLocaleIdentifier
        value:lang]
);

显示(null),而不是English

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

奇怪的是,Apple 给出的以下示例运行良好。

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);

【问题讨论】:

标签: ios objective-c xcode nslocale xcode6.1


【解决方案1】:

来自Xcode release note

在某些情况下,[NSLocale currentLocale] 可能会返回 en_US 在 iOS 8.1 模拟器中选择的语言环境。 (18512161)

+ (NSLocale *)autoupdatingCurrentLocale NS_AVAILABLE(10_5, 2_0); // generally you should use this method

+ (NSLocale *)currentLocale;    // an object representing the user's current locale

一种解决方法是使用第一个。在 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]
      );

【讨论】:

  • 嗯,在我看来,您所做的就像我问题末尾的代码一样,但我接受它,因为它解决了我的问题。谢谢!
【解决方案2】:

这是一个已知的 Apple 问题,仅适用于 iOS 8.1 模拟器 - 无法在 8.1 设备上重现。请参阅以下 Xcode 6.1 发行说明中的​​问题描述:

本地化和键盘设置(包括第 3 方键盘)是 iOS 中的 Safari、地图和开发者应用程序未正确识别 8.1 模拟器。 [NSLocale currentLocale] 返回 en_US 并且只有英文和表情符号键盘可用。 (18418630, 18512161)。

更多详情请见Xcode 6.1 Release Notes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2020-04-20
    相关资源
    最近更新 更多