mon*_*ono 8 xcode locale localization ios swift
我想更改Xcode Playground上的语言环境来测试本地化.
我找到了这个解决方案,但它不适用于Xcode 6.3.2 Playground:http: //natashatherobot.com/locale-playground-swift/
mon*_*ono 13
哦,我找到了解决方案!
extension NSLocale {
class func mono_currentLocale() -> NSLocale {
return NSLocale(localeIdentifier: "fr")
}
}
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.currentLocale))
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.mono_currentLocale))
method_exchangeImplementations(original, swizzled)
Run Code Online (Sandbox Code Playgroud)
编辑:Swift 4.1版本:
extension NSLocale {
@objc class func mono_currentLocale() -> NSLocale {
return NSLocale(localeIdentifier: "fr")
}
}
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.current))!
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.mono_currentLocale))!
method_exchangeImplementations(original, swizzled)
Run Code Online (Sandbox Code Playgroud)
XCode 13 / Swift 5
\n要更改游乐场中的当前区域设置,您可以创建扩展名NSLocale并覆盖currentLocale:
extension NSLocale {\n @objc\n static let currentLocale = NSLocale(localeIdentifier: "en_GB") // Set a needed locale\n}\n\nlet formatter = NumberFormatter()\nformatter.numberStyle = .currency\nlet text = formatter.string(from: 12345.67 as NSNumber)!\nprint(text)\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n\xc2\xa312,345.67\nRun Code Online (Sandbox Code Playgroud)\n\n\n注意:它也适用于单元测试。
\n
| 归档时间: |
|
| 查看次数: |
1631 次 |
| 最近记录: |