iOS:如何获取设备当前语言设置?

发布于 2024-11-26 15:33:53 字数 546 浏览 9 评论 0原文

我的应用程序中的一些功能应该基于其运行设备的语言设置。

我想获取实际的语言而不是某些国家/地区设置。例如,如果语言是英语,我不在乎它是美国、英国、澳大利亚等...

我熟悉 NSLocale 对象,但它似乎与 < code>Region Format 设置而不是 Language 设置(请参见下面的屏幕截图),因此当我尝试使用 [locale displayNameForKey:NSLocaleIdentifier 值从中检索语言时: [语言环境localeIdentifier] 我得到诸如 English (United States) 而不是 English 之类的内容;另外,我认为我需要的是语言数据,而不是区域格式(我是对的吗?)。

谁能指导我如何检索语言设置?

在此处输入图像描述

There are some features within my application that are supposed to be based on the language settings of the device where it's running.

I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...

I'm familiar with the NSLocale object, but it seems to relate to the Region Format setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier] I get things like English (United States) instead of English; also, I think that what I need is the Language data and not the Region Format (am I right?).

Can anyone direct me to how to retrieve the language setting?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

记忆で 2024-12-03 15:33:54

在XCode的帮助文档中找到解决方案,它写道:

获取当前语言

要从主应用程序包中获取应用程序正在使用的语言,请使用NSBundle类中的preferredLocalizations方法:

NSString * languageID = [[NSBundle mainBundle ] PreferredLocalizations].firstObject;

Find the solution in XCode's helper document, it wrote:

Getting the Current Language

To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:

NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;

慢慢从新开始 2024-12-03 15:33:54

使用下面的代码获取本地化语言,而不会遇到 en-india、en-us 等问题。

 NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

在 ios9 中和之后,需要考虑此代码

Use below code to fetch Localised language without having trouble to the en-india, en-us etc..

 NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

In and After ios9 this code need to take in cosideration

独行侠 2024-12-03 15:33:54

要了解本地化中选择的当前语言,请使用

[[NSBundle mainBundle] preferredLocalizations]

示例:

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

要获取两个字母的单词

NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];

Swift:

let language = NSBundle.mainBundle().preferredLocalizations.first as NSString

To know the current language selected within your localizations use

[[NSBundle mainBundle] preferredLocalizations]

Example:

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

To get two letter word

NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];

Swift:

let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
2024-12-03 15:33:53

存储的用户首选语言可以从语言环境中作为数组检索,当前语言标识符是该数组中的第一个对象:

NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];

如果您希望语言具有更易读的形式,请使用 NSLocale 的 displayNameForKey:value: 方法:

NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];

User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:

NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];

If you want language in more readable form then use displayNameForKey:value: method of NSLocale:

NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
黎歌 2024-12-03 15:33:53

试试这个:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];

Try this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
鹿! 2024-12-03 15:33:53

在 Swift 中获取语言和区域:

    LF.log("language", NSLocale.preferredLanguages())
    LF.log("locale", NSBundle.mainBundle().preferredLocalizations)

就我而言,我得到:

language: '(
    "zh-Hans"
)'
locale: '(
    en
)'

Getting language and region in Swift:

    LF.log("language", NSLocale.preferredLanguages())
    LF.log("locale", NSBundle.mainBundle().preferredLocalizations)

In my case I'm getting:

language: '(
    "zh-Hans"
)'
locale: '(
    en
)'
暖风昔人 2024-12-03 15:33:53

Swift 4 中:

let currentLanguage = Locale.current.languageCode

它只会为您提供语言代码,而不会提供国家/地区代码。

In Swift 4:

let currentLanguage = Locale.current.languageCode

It will give you just the language code, no country code.

甩你一脸翔 2024-12-03 15:33:53

迅速:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString

Swift:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
凉城 2024-12-03 15:33:53

工作解决方案:

    let language = NSLocale.preferredLanguages()[0]
    let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
    //let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
    let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
    print(languageCode)

Working solution:

    let language = NSLocale.preferredLanguages()[0]
    let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
    //let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
    let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
    print(languageCode)
生来就爱笑 2024-12-03 15:33:53
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文