views:

1742

answers:

7

I'm working on a localized app and everything is working fine. The problem is I want to allow the user to specifically select the lenguage for the specific app, in the app settings folder. This should users that their phone is set to one language (e.g. french) to set the app to work in English.

I'm currently using NSLocalizedString to get localized string but looking through all variation of the macro I can't find one that will let me specify the language.

Any ideas on how to do it?

A: 

I'm looking for the same information. How did you manage to fix this?

Thanks

Geraud.ch
I didn't. If you find something let me know.
Ron Srebro
A: 

I would like to know this too, please post your answer if you know.

Mark
A: 

Have you tried using initWithLocaleIdentifier: to set the locale yourself? For example, to set the locale to Netherlands you can initialize a locale using:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_BE"];
Dimitris
I doubt this would work, since it would just create a NSLocale object for a specified locale and won't affect the "current"/"active" locale
Ron Srebro
+1  A: 

The correct "User experience" is for the user to select their language via the system preference panel; not your app (or your app's settings panel, etc.). There is no way to override this per-app and we wouldn't want any app changing the system wide setting.

geowar
It is a little odd to want to run one app in a different language than all the other apps. I wonder what the rationale is?
Mark Bessey
I agree that generally speaking you are correct, however in a multilingual countries or for users speaking more than one language, there are sometimes case where I'll like to use one language for a specific app while my rest of the phone is set on english. A good example might be a site like this stack overflow, which for me because of the content here I'll always want to see in English as opposed to maybe Facebook where I'll stick with my native language.
Ron Srebro
Also, allowing an in-app language switch is useful in kiosk-mode scenarios (which make most sense with iPad apps, not iPhone ones. But still…).
Raphael Schweikert
+6  A: 

There are three issues here:

  • Strings
  • Other resources (including NIBs)
  • System messages

The last is almost certainly not fixable, so we will leave it be. They're going to show up in the device language.

The other two are solvable, but you will need to do more things by hand. For strings, instead of creating a single Localizable.strings and then localizing it, create completely separate tables (English.strings, French.strings, etc.) Then, use NSLocalizedStringFromTable(), passing the language as the table.

For NIBs, there are two approaches. You can put each set of localized NIBs into its own Bundle and then pass that Bundle rather than nil to -initWithNibName:bundle:. Alternately, you can hand-load the NIBs after finding them with [NSBundle -pathForResource:ofType:inDirectory:forLocalization:].

Rob Napier
Thanks, that is only response here that is actually an answer to what I asked. Your suggestion seems reasonable enough and leverages the built in framework for localization. Thanks
Ron Srebro
This is a fantastic answer.
Brandon
I notice that the question doesn't say anything about doing this at runtime. The approved answer specifically notes that it requires an app restart.This answer allows you to change locale at runtime.This is exactly what I've been looking for. Thank you.
Brandon
+5  A: 

There is a better way to do this. You can force the language like so:

[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

And undo this setting by:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];

NB. you will normally have to restart the app for this to take affect.

Consider if you need to call [[NSUserDefaults standardUserDefaults] synchronize];

I agree there is little need to allow the user to specify a language. However the one exception is being able to override the language and set it to the developer's native language. If the user can speak the developer's language (e.g. English for me) then they may wish to use the App in that language, if the translations are incorrect.

I reference this answer: http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language/1670524#1670524 (the answer doesn't actually work for me, but following the ideas in the comments did. The undo stuff I worked out.

William Denniss
Finally. Thanks
Ron Srebro