Skip to content

Instantly share code, notes, and snippets.

@enomoto
Last active May 1, 2024 23:22
Show Gist options
  • Save enomoto/2c8892a30472997f4ada7458ee8236f8 to your computer and use it in GitHub Desktop.
Save enomoto/2c8892a30472997f4ada7458ee8236f8 to your computer and use it in GitHub Desktop.
Change locale in XCTestCase
private extension MyTests {
private func setLocaleAsJP() {
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.current))!
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.myCurrentLocale))!
method_exchangeImplementations(original, swizzled)
}
}
// https://stackoverflow.com/questions/31065859/how-can-i-change-the-locale-on-the-xcode-playground
fileprivate extension NSLocale {
@objc class func myCurrentLocale() -> NSLocale {
return NSLocale(localeIdentifier: "ja")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment