获取本地语言

获取本地语言,第1张

概述  Getting Language and Locale Preferences Directly There may be situations where you want to get the preferred locale ID or the list of languages directly from the user preferences. Mac OS X stores ea

 

Getting Language and Locale Preferences Directly

There may be situations where you want to get the preferred locale ID or the List of languages directly from the user preferences. Mac OS X stores each user’s List of preferred languages in that user’s defaults database. The List of preferred languages is IDentifIEd by the defaults key AppleLanguages and is stored in the global variable NSGlobalDomain. You can access that List using the NSUserDefaults class in Cocoa or the Core Foundation preferences functions.

important: If you get the user language preference from the defaults database,you must get the canonical form using theCFLocaleCreateCanonicalLanguageIDentifIErFromString function (in Mac OS X v10.4 and later) or CFLocaleCreateCanonicalLocaleIDentifIErFromString function (in Mac OS X v10.3 and later) before using the IDentifIEr.

 

The following example shows you to get the List of preferred languages from the defaults database using Cocoa. The returned array contains the languages associated with theAppleLanguages key in the user's preferred order. Thus,in most cases,you would simply want to get the first object in the array.

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

The locale for the current user is also stored in the defaults database under theAppleLocale key.

 


important: Although you can get the user's preferred settings from the defaults database,it is recommended you use the CFBundle functions or NSBundle class instead. The associated functions and methods of those objects return the preferred language or locale that is also supported by your application. (Bear in mind that the returned values may not correspond directly with the user's exact preferences.)

总结

以上是内存溢出为你收集整理的获取本地语言全部内容,希望文章能够帮你解决获取本地语言所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存