iOS学习之NSLocale

来源:互联网 发布:js自动获取input光标 编辑:程序博客网 时间:2024/05/18 00:47
本地化封装了关于语言,文化以及技术约定和规范的信息。用于提供于用户所处地域相关的定制化信息和首选项信息的设置。通过获取用户的本地化信息设置,我们可以为用户提供更加友好人性化的界面设置,包括更改更改应用程序的界面的语言,货币类型,数字,日期格式的格式化,提供正确的地理位置显示等等。IOS内置为应用程序的开发提供了很好的本地化机制,良好的本地化意味着应用程序可以为更多的用户提供服务。其中NSLocale类的的主要作用便是用来封装本地化相关的各种信息,下面简单列举下NSLocale的一些方法,但NSLocale更多是使用在对数字,时间日期本地化的处理的过程。

1.创建本地化对象

// 根据本地标识符创建本地化对象NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];// 当前用户设置的本地化对象[NSLocale currentLocale]

2.获取系统本地化信息

// 获取系统所有本地化标识符数组列表[NSLocale availableLocaleIdentifiers] ;// 获取所有已知合法的国家代码数组列表[NSLocale ISOCountryCodes] ;// 获取所有已知合法的ISO货币代码数组列表[NSLocale ISOCurrencyCodes] ;// 获取所有已知合法的ISO语言代码数组列表[NSLocale ISOLanguageCodes] ; // 通用的货币编码 NSArray *commonISOCurrencyCodes=[NSLocale commonISOCurrencyCodes];

3.获取当前系统设置语言的标识符

[[NSLocale currentLocale] localeIdentifier];

等价于
[[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
4.获取本地化对象的具体内容

NSLocale *local = [NSLocale currentLocale];
[local objectForKey:NSLocaleIdentifier];
[local objectForKey: NSLocaleLanguageCode];
key值参见NSLocale Calendar Keys

5.获取当前语言的排版方向和字符方向

[NSLocale lineDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];[NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] ;

6.获取用户的语言偏好设置列表,该列表对应于IOS中Setting>General>Language弹出的面板中的语言列表。

[NSLocale preferredLanguages]

第一个元素即为当前用户设置的语言

7.监听用户本地化设置的消息

[[NSNotificationCenter defaultCenter] addObserver:self                                         selector:@selector(localChangedHandler:)                                             name:NSCurrentLocaleDidChangeNotification object:nil];

8.以本地化方式获取国际化信息的显示名称

NSLocale *curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"] ;NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );// 法文(法国)curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hant"] ;NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );//法文(法國)

9.语言方向枚举
NSLocaleLanguageDirection

These constants describe the text direction for a language. Used by the methodslineDirectionForLanguage: and characterDirectionForLanguage:.

enum {   NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,// 未知   NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight,   NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft,   NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom,   NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop};

10.预定义key键
NSLocale Component Keys

NSLocale 的组成key

NSString * const NSLocaleIdentifier;// 标识NSString * const NSLocaleLanguageCode;// 语言码zhNSString * const NSLocaleCountryCode;// 区域码NSString * const NSLocaleScriptCode;// The key for the locale script code.NSString * const NSLocaleVariantCode;// The key for the locale variant code.NSString * const NSLocaleExemplarCharacterSet;// 元字符集 NSCharacterSetNSString * const NSLocaleCalendar;// 当地日历  NSCalendarNSString * const NSLocaleCollationIdentifier;// The key for the collation associated with the locale.NSString * const NSLocaleUsesMetricSystem;// 是否使用公制 NSNumberNSString * const NSLocaleMeasurementSystem;// 当地度量单位NSString * const NSLocaleDecimalSeparator;// 小数点符号NSString * const NSLocaleGroupingSeparator;// 分组符号NSString * const NSLocaleCurrencySymbol;// 货币符号NSString * const NSLocaleCurrencyCode;// 货币代码NSString * const NSLocaleCollatorIdentifier;// 具体语言码??zh-HansNSString * const NSLocaleQuotationBeginDelimiterKey;// 引号开始符号NSString * const NSLocaleQuotationEndDelimiterKey;// 引号结束符号NSString * const NSLocaleAlternateQuotationBeginDelimiterKey;// 单引号开始符号NSString * const NSLocaleAlternateQuotationEndDelimiterKey;// 单引号结束符号

NSLocale Calendar Keys

NSCalendar 包含的.

NSString * const NSGregorianCalendar;// 公历标识NSString * const NSBuddhistCalendar;// 佛历标识NSString * const NSChineseCalendar;// 农历标识(不支持)NSString * const NSHebrewCalendar;// 希伯来历NSString * const NSIslamicCalendar;// 回历NSString * const NSIslamicCivilCalendar;NSString * const NSJapaneseCalendar;// 日本历NSString * const NSRepublicOfChinaCalendar;// **历法(不能用来格式,有些函数不正确?)NSString * const NSPersianCalendar;// 波斯历法NSString * const NSIndianCalendar;// 印度历法NSString * const NSISO8601Calendar;// ISO8601历法,标准还没制定?

上面的可用来创建NSCalendar或者由NSCalendar的标识函数得到.

0 0