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?

Mike
  • 1,112
  • 1
  • 13
  • 20
  • 1
    Whoa-whoo. `method_setImplementation(class_getInstanceMethod([NSLocale class], @selector(currentLocale)), (IMP)spoofedImpl);`? –  Mar 13 '13 at 21:18
  • 2
    Any way to do it without swizzling? – Mike Mar 13 '13 at 21:27
  • Have you tried creating a category for NSLocale to return the locale of your choosing for the class methods? – Marcus Adams Mar 13 '13 at 21:52
  • @MarcusAdams thats worser than method swizzling because override method on category have undefined behaviour – Bryan Chen Mar 13 '13 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. –  Mar 14 '13 at 06:29

1 Answers1

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.

Community
  • 1
  • 1
tc.
  • 33,468
  • 5
  • 78
  • 96
  • Thanks @tc. It sounds like this is probably something to stay away from, but I appreciate the detailed option. – Mike Mar 20 '13 at 15:26