我想要显示设备UI使用的当前语言。我应该使用什么代码?

我想把它作为一个NSString完全拼写出来的格式。(@ en_US)

编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。


当前回答

I actually misread the original question, thought it asked for the "app UI" language (that's what I had googled for), not the "device UI", in which case the best answers would be the ones using preferredLocalizations, but those answers still give you a code, there is one more step to get a nice string to display. So, while the "device UI" language is already answered, if you want to display a nice string for which of the UI languages you support is currently in use, obviously the simplest solution is:

NSLocalizedString(@"currentLanguage", @"")

在你的每一个UI本地化中你都指定了你想要的显示方式。例如,在你的。strings文件的en版本中,你会有:

"currentLanguage"="English";

在你的fr版本的.strings文件中,你会有:

"currentLanguage"="Francais";

等。不要混淆代码等,你可以让你自己的字符串很好地匹配你的UI。

其他回答

显然,解决方案依赖于,例如

[[NSLocale preferredLanguages] objectAtIndex:0]

通常工作正常,并返回当前设备语言。

但在某些情况下,它可能会产生误导:

如果你想要获取这个值的应用程序已经改变了语言,例如使用这样的代码:

NSString *lg = @"en"; // or anything like @"en", @"fr", etc.
[[NSUserDefaults standardUserDefaults] 
    setObject:[NSArray arrayWithObjects:lg, nil]  
    forKey:@"AppleLanguages"]

在这种情况下,[NSLocale preferredLanguages]实际上返回这个特定应用程序的首选语言集(和使用),而不是当前设备语言!

和…在这种情况下,正确获得实际当前设备语言(而不是之前在应用程序中设置的语言)的唯一方法是首先清除NSUserDefaults中的@"appleLanguages"键,像这样:

[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"AppleLanguages"];

然后,[NSLocale preferredLanguages]现在返回正确的值。

希望这对你有所帮助。

对于获得用户设备当前语言使用以下它的代码,它为我工作。

NSString * myString = [[NSLocale preferredlanguage]objectAtIndex:0];

甚至还有更好的方法来获取当前设备语言。让我们试试下面的代码-

NSLog(@"Current Language - %@", [[NSLocale preferredLanguages] firstObject]);

这是阿比森的建议

提供的解决方案实际上会返回设备的当前区域——而不是当前选择的语言。这些通常是同一件事。但是,如果我在北美,我将我的语言设置为日语,我的区域仍然是英语(美国)。为了检索当前选择的语言,你可以这样做:

NSString * language = [[NSLocale preferredLanguages] firstObject];

这将为当前选择的语言返回两个字母的代码。英语是“en”,西班牙语是“es”,德语是“de”,等等。更多例子,请参阅维基百科的条目(特别是639-1列):

ISO 639-1代码列表

然后,将这两个字母的代码转换为您想要显示的字符串就很简单了。如果是"en",显示"English"。

EDIT

值得引用NSLocale.h中的头信息:

+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information

对应用语言感兴趣的人可以看看@mindvision的答案

I actually misread the original question, thought it asked for the "app UI" language (that's what I had googled for), not the "device UI", in which case the best answers would be the ones using preferredLocalizations, but those answers still give you a code, there is one more step to get a nice string to display. So, while the "device UI" language is already answered, if you want to display a nice string for which of the UI languages you support is currently in use, obviously the simplest solution is:

NSLocalizedString(@"currentLanguage", @"")

在你的每一个UI本地化中你都指定了你想要的显示方式。例如,在你的。strings文件的en版本中,你会有:

"currentLanguage"="English";

在你的fr版本的.strings文件中,你会有:

"currentLanguage"="Francais";

等。不要混淆代码等,你可以让你自己的字符串很好地匹配你的UI。