Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Is there any way to fake the user's locale?

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?

like image 598
Mike Avatar asked Mar 13 '13 21:03

Mike


1 Answers

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.

like image 130
tc. Avatar answered Nov 11 '22 04:11

tc.