gpt4 book ai didi

iphone - 完整显示语言环境

转载 作者:行者123 更新时间:2023-12-03 18:40:31 25 4
gpt4 key购买 nike

我从以下代码中获取用户首选语言:

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString *language = [languages objectAtIndex:0];

这将返回“英语”的“en”或“德语”的“de”。有什么方法可以自动获取完整的语言名称,即 English 而不是 en?

谢谢!

米哈伊·福诺阿奇

最佳答案

要获取用户的首选语言列表,请使用[NSLocale PreferredLanguages]。这将返回语言代码字符串的数组。要将这些语言代码转换为显示名称,您需要使用 NSLocale displayNameForKey:value: 实例方法以及键 NSLocalIdentifier

对于当前语言环境中语言的显示名称,请在 [NSLocale currentLocale] 上调用该方法 - 在我的例子中,“de”变为“German”。如果您想要语言本身的名称,请首先为该语言创建一个 NSLocale 实例,然后使用该对象作为接收者。例如,德语中的“de”变为“Deutsch”。

这段代码应该清楚地表明:

NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language];
NSLog(@"language code = %@, display name = %@, in language = %@",
language,
[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
[locale displayNameForKey:NSLocaleIdentifier value:language]);
}

以及我 iPhone 上的输出:

language code = en, display name = English, in language = English
language code = zh-Hans, display name = Chinese (Simplified Han), in language = 中文 (简体中文)
language code = ja, display name = Japanese, in language = 日本語
language code = pt, display name = Portuguese, in language = português
language code = de, display name = German, in language = Deutsch
language code = fr, display name = French, in language = français
language code = nl, display name = Dutch, in language = Nederlands
language code = it, display name = Italian, in language = italiano
language code = es, display name = Spanish, in language = español
language code = pt-PT, display name = Portuguese (Portugal), in language = português (Portugal)
language code = da, display name = Danish, in language = dansk
language code = fi, display name = Finnish, in language = suomi
language code = nb, display name = Norwegian Bokmål, in language = norsk bokmål
language code = sv, display name = Swedish, in language = svenska
language code = ko, display name = Korean, in language = 한국어
language code = zh-Hant, display name = Chinese (Traditional Han), in language = 中文 (繁體中文)
language code = ru, display name = Russian, in language = русский
language code = pl, display name = Polish, in language = polski
language code = tr, display name = Turkish, in language = Türkçe
language code = uk, display name = Ukrainian, in language = українська
language code = ar, display name = Arabic, in language = العربية
language code = hr, display name = Croatian, in language = hrvatski
language code = cs, display name = Czech, in language = čeština
language code = el, display name = Greek, in language = Ελληνικά
language code = he, display name = Hebrew, in language = עברית
language code = ro, display name = Romanian, in language = română
language code = sk, display name = Slovak, in language = slovenský
language code = th, display name = Thai, in language = ไทย
language code = id, display name = Indonesian, in language = Bahasa Indonesia

关于iphone - 完整显示语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2246243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com