5

I'm interested in allowing my users to choose the language / locale my app appears to them in. I have figured out how to load the right bundle, choose the correct strings files, etc. However, some system items I still seem to have no control over. Switch controls, for example.

Is there any way for me to tell the system that this app should now behave in a specific language, rather than the user's current system setting?

5
  • 1
    Whoa-whoo. method_setImplementation(class_getInstanceMethod([NSLocale class], @selector(currentLocale)), (IMP)spoofedImpl);?
    – user529758
    Commented Mar 13, 2013 at 21:18
  • 2
    Any way to do it without swizzling?
    – Mike
    Commented Mar 13, 2013 at 21:27
  • Have you tried creating a category for NSLocale to return the locale of your choosing for the class methods? Commented Mar 13, 2013 at 21:52
  • @MarcusAdams thats worser than method swizzling because override method on category have undefined behaviour
    – Bryan Chen
    Commented Mar 13, 2013 at 23:24
  • @Mike Become a software developer at Apple working on UIKit, create a secret/private method that allows you to do this, put it in the next release of iOS.
    – user529758
    Commented Mar 14, 2013 at 6:29

1 Answer 1

5

The least hacky way is via NSUserDefaults: The locale is determined by AppleLocale (the locale ID as a string) and the language is determined by the first match in AppleLanguages (an array of language IDs); your app can override these defaults for your app only. To check if it's overridden by your app, you can use something like [defaults persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]] objectForKey:@"AppleLocale"]. To remove the override, use -removeObjectForKey:.

Whichever way you do it, you'll probably have to relaunch the app when changing languages; OS X and iOS apps simply aren't designed to change language at runtime (on OS X, only newly launched apps see the language change; on iOS the system kills all apps to change language).

I added this feature to make switching languages easier for development/testing; I have no idea whether this will fail the App Store review process.

1
  • Thanks @tc. It sounds like this is probably something to stay away from, but I appreciate the detailed option.
    – Mike
    Commented Mar 20, 2013 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.