NSLocale

作者: MeteorCode | 来源:发表于2018-12-26 17:19 被阅读5次

NSLocale类包括国家,语言,货币等信息

初始化:

init(localeIdentifier: String) //使用给定的区域设置标识符初始化区域设置
init?(coder: NSCoder) //通过解压数据初始化

获取用户locale设置

class var autoupdatingCurrent: Locale //用户的当前偏好
class var current: Locale //当前用户的区域设置
class let currentLocaleDidChangeNotification: NSNotificationName //用户设置locale设置变化的通知
class var system: Locale //系统

获取iOS提供的已知标识码

工厂方法:
class var availableLocaleIdentifiers: [String] //系统上可用的区域设置标识符列表
class var isoCountryCodes: [String] //已知国家/地区ISO代码列表
class var isoLanguageCodes: [String] //已知语言ISO代码列表
class var isoCurrencyCodes: [String] //已知货币ISO代码列表
class var commonISOCurrencyCodes: [String] //常见货币代码列表

标识符之间的转换

class func canonicalLocaleIdentifier(from: String) -> String //返回给定区域设置标识字符串的规范标识符
class func componentsLocaleIdentifier: String) -> [String : String] //返回解析语言环境ID的结果的字典
class func localeIdentifier(fromComponents: [String : String]) -> String //从给定字典中指定的组件返回区域设置标识符
class func canonicalLanguageIdentifier(from: String) -> String //通过将任意区域设置标识字符串映射到规范标识符来返回规范语言标识符
class func localeIdentifier(fromWindowsLocaleCode: UInt32) -> String? //从Windows区域设置代码返回区域设置标识符
class func windowsLocaleCode(fromLocaleIdentifier: String) -> UInt32 //从区域设置标识符返回Window区域设置代码

获取有关locale的显示信息

实例方法:
func localizedString(forLocaleIdentifier: String) -> String //返回指定语言环境标识符的本地化字符串
func localizedString(forCountryCode: String) -> String? //返回指定国家/地区代码的本地化字符串
func localizedString(forLanguageCode: String) -> String? //返回指定语言代码的本地化字符串
func localizedString(forScriptCode: String) -> String? //返回指定脚本代码的本地化字符串
func localizedString(forVariantCode: String) -> String? //返回指定变体代码的本地化字符串
func localizedString(forCollationIdentifier: String) -> String? //返回指定排序规则标识符的本地化字符串
func localizedString(forCollatorIdentifier: String) -> String? //返回指定的collator标识符的本地化字符串
func localizedString(forCurrencyCode: String) -> String? //返回指定货币代码的本地化字符串
func localizedString(forCalendarIdentifier: String) -> String? //返回指定日历标识符的本地化字符串

获取key对应的locale信息

实例方法:
func object(forKey: NSLocale.Key) -> Any? //返回对应指定key的id
func displayName(forKey: NSLocale.Key, value: Any) -> String? //返回对应指定key和id的显示名称

  • key列表:
    public static let identifier: NSLocale.Key
    public static let languageCode: NSLocale.Key // 语言
    public static let countryCode: NSLocale.Key // 地区
    public static let scriptCode: NSLocale.Key // NSString
    public static let variantCode: NSLocale.Key // NSString
    public static let exemplarCharacterSet: NSLocale.Key // NSCharacterSet
    public static let calendar: NSLocale.Key // NSCalendar
    public static let collationIdentifier: NSLocale.Key // NSString
    public static let usesMetricSystem: NSLocale.Key // NSNumber boolean
    public static let measurementSystem: NSLocale.Key // NSString
    public static let decimalSeparator: NSLocale.Key // NSString
    public static let groupingSeparator: NSLocale.Key // NSString
    public static let currencySymbol: NSLocale.Key // NSString
    public static let currencyCode: NSLocale.Key // NSString
  • 使用
//获取当前用户的locale的地区标识
let id = (Locale.current as NSLocale).object(forKey: .countryCode) as? String
//通过标识读取地区名字
let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: id)
//获取当前用户的语言标识
let id = (Locale.current as NSLocale).object(forKey: .languageCode) as? String 
//通过标识读取语言名字
let languageName = (Locale.current as NSLocale).displayName(forKey: .languageCode, value: id)

获取用户偏好语言

class var preferredLanguages: [String] //用户首选语言的有序列表

获取语言的线条和字符方向

class func characterDirection(forLanguage: String) -> NSLocale.LanguageDirection //返回指定ISO语言代码的行中字符序列的方向
class func lineDirection(forLanguage: String) -> NSLocale.LanguageDirection //返回指定ISO语言代码的行序列的方向。

获取有关Locale设置的信息

var localeIdentifier: String //语言环境的标识符。
var countryCode: String? //区域设置的国家/地区代码
var languageCode: String //语言环境的语言代码
var scriptCode: String? //语言环境的脚本代码
var variantCode: String? //语言环境的变体代码。
var exemplarCharacteSet: CharacterSet //范例字符集
var collationIdentifier: String? //整理标识符
var collatorIdentifier: String //整理者标识符
var usesMetricSystem: Bool //一个布尔值,指示区域设置是否使用公制系统。
var decimalSeparator: String //语言环境的小数分隔符
var groupingSeparator: String //分组分隔符
var currencyCode: String? //货币代码。
var currencySymbol: String //货币符号
var calendarIdentifier: String //日历标识符
var quotationBeginDelimiter: String //开头的引号
var quotationEndDelimiter: String //结束引号
var alternateQuotationBeginDelimiter: String //备用开始引号
var alternateQuotationEndDelimiter: String //备用结束引号

相关文章

  • iOS语音识别

    //创建本地化NSLocale *local =[[NSLocale alloc] initWithLocaleI...

  • NSLocale

    国家编码语言编码货币符号货币编码 =============currentLocale==============...

  • NSLocale

    NSLocale类包括国家,语言,货币等信息 初始化: init(localeIdentifier: String...

  • NSLocale

  • NSDate、NSTimeZone、NSLocale、NSDat

    NSDate、NSTimeZone、NSLocale、NSDateComponents、NSCalendar、NS...

  • iOS NSLocale

    1: 父类 2: 属性 3:方法 4: 分类(NSLocaleCreation)

  • iOS NSLocale

    1.NSLocale 若你只开发中国区的应用,需要保证用户修改当前语言环境时应用的显示不发生变化。而像NSDate...

  • iOS 当前系统的语言

    获取当前系统支持的语言 [NSLocale availableLocaleIdentifiers] 最近使用的语言...

  • NSLocale类使用

    之前文章讲过NSData的格式化(NSDate 格式化),今天来说一下实际代码使用中遇到的问题。 最近在工作中,需...

  • NSLocale 和 NSLocalizedString

    1.获取手机当前的语言环境 2.获取手机当前的位置(国家) 3.ios项目常用的国际化方法 4. NSLocale...

网友评论

    本文标题:NSLocale

    本文链接:https://www.haomeiwen.com/subject/txbhlqtx.html