Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the locale on the Xcode Playground

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/

like image 490
mono Avatar asked Jun 26 '15 05:06

mono


People also ask

Can you use swift playground on Mac?

Swift Playgrounds is a revolutionary app for iPad and Mac that makes it fun to learn and experiment with Swift — a powerful programming language created by Apple and used by the pros to build today's most popular apps. Swift Playgrounds requires no coding knowledge, so it's perfect for students just starting out.

What is the difference between Swift playground and Xcode?

One of the biggest differences between Swift Playgrounds and Xcode Playgrounds is that Swift Playgrounds are much less powerful and are built more as an educational tool. My biggest fear is that as Apple brings Swift Playgrounds to the Mac that they will stop supporting and growing Xcode Playgrounds.

Why is my Xcode playground not running?

Restarting Xcode and rebooting my macbook. Stopping the simulator via the Activity monitor and restarting it. Opening up new tabs in an attempt to refresh. Uninstalling and reinstalling Xcode via the app store.

What is variable in Swift playground?

They are called variables because they can vary – you can change their values freely. Playgrounds start with a line of code that creates a variable for us: var str = "Hello, playground" That creates a new variable called str , giving it the value “Hello, playground”.


2 Answers

XCode 13 / Swift 5

To change the current locale in the playground you can create extension of NSLocale and override 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)

Outputs:

£12,345.67

NOTE: It also works with unit tests.

like image 200
iUrii Avatar answered Sep 22 '22 04:09

iUrii


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)
like image 39
mono Avatar answered Sep 25 '22 04:09

mono