如何更改Xcode Playground上的区域设置

时间:2023-01-23 17:01:15

I'd like to change the locale on the Xcode Playground to test localization.

我想更改Xcode Playground上的语言环境来测试本地化。

I found this solution, but it doesn't work on the Xcode 6.3.2 Playground: http://natashatherobot.com/locale-playground-swift/

我找到了这个解决方案,但它不适用于Xcode 6.3.2 Playground:http://natashatherobot.com/locale-playground-swift/

1 个解决方案

#1


10  

Oh, I've found a solution!

哦,我找到了解决方案!

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)

EDIT: Swift 4.1 version:

编辑: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)

#1


10  

Oh, I've found a solution!

哦,我找到了解决方案!

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)

EDIT: Swift 4.1 version:

编辑: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)