LoginSignup
19
18

More than 5 years have passed since last update.

XcodeのPlaygroundでlocaleを変更する方法

Posted at

国際化周りの実装を試すのに、XcodeのPlaygroundでlocale変更出来たら便利だなーと思って調べてたのですが、情報が全く見つからず。
で、ちょっと考えて試したらMethod Swizzlingでうまく行きました( ´・‿・`)

シミュレーターの切り替えはこちら:
iOS - Xcode6で言語設定の切り替えが簡単になった - Qiita

extension NSLocale {
    class func mono_currentLocale() -> NSLocale {
        return NSLocale(localeIdentifier: "fr") // ここは適当に変えましょう
    }
}

let method = class_getClassMethod(NSLocale.self, "currentLocale")
let swizzledMethod = class_getClassMethod(NSLocale.self, "mono_currentLocale")
method_exchangeImplementations(method, swizzledMethod)
print(NSLocale.currentLocale().localeIdentifier) // -> "fr"

method_exchangeImplementations(method, swizzledMethod) をコメントアウトすると、デフォルトの en_USになるはずです。

以前はHow To Test Locale In Your Playground in Swiftの方法で出来たようですが、Xcode 6.3.2でコンパイルエラーになってしまったので、新しいやり方を考えたらうまくいきました。
(http://stackoverflow.com/questions/31065859/how-can-i-change-the-locale-on-the-xcode-playground で質問してセルフ回答)

locale一覧はこちら:
https://gist.github.com/jacobbubu/1836273

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18