![](/img/trans.png)
[英]How can I display views using autolayout constraints in Xcode playground?
[英]How can I change the locale on the Xcode Playground
我想更改 Xcode 游樂場上的語言環境以測試本地化。
我找到了這個解決方案,但它在 Xcode 6.3.2 游樂場上不起作用: http://natashatherobot.com/locale-playground-swift/
哦,我找到了解決方案!
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)
編輯: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)
XCode 13 / Swift 5
要更改 playground 中的當前語言環境,您可以創建NSLocale
的擴展並覆蓋currentLocale
:
extension NSLocale {
@objc
static let currentLocale = NSLocale(localeIdentifier: "en_GB") // Set a needed locale
}
let formatter = NumberFormatter()
formatter.numberStyle = .currency
let text = formatter.string(from: 12345.67 as NSNumber)!
print(text)
輸出:
£12,345.67
注意:它也適用於單元測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.