NSLocale
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语言代码的行序列的方向。
- 方向
NSLocale.LanguageDirection
case unknown//语言的方向未知
case leftToRight//语言方向是从左到右
case rightToLeft//语言方向是从右到左
case topToBottom//语言方向是从上到下。
case bottomToTop//语言方向从下到上。
获取有关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 //备用结束引号