[SOLVED] How can I change the locale on the Xcode Playground

Issue

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

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

Solution

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:

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)

Answered By – mono

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.