Changing the language for NSLocalizedString () at runtime

I have an iPhone app that has a button for changing the display language at runtime. I looked at NSLocalizedString (), which will return the corresponding rows according to system preference. What are my options, rather than hard coding all the display strings and returning according to the user's language choice at runtime? Any pointers would be much appreciated.

+7
iphone cocoa localization
source share
4 answers

Based on the message from the user "object2.0", I have compiled several code samples that you can use in your application to change the language of the user interface on the fly.

The main localization class that does the hard work:

-(NSString *) localized:(NSString *) key { GameInfo *gameInfo = [GameInfo sharedInstance]; // langCode should be set as a global variable somewhere NSString *path = [[NSBundle mainBundle] pathForResource:langCode ofType:@"lproj"]; NSBundle* languageBundle = [NSBundle bundleWithPath:path]; return [languageBundle localizedStringForKey:key value:@"" table:nil]; } 

Assuming you have this function in a global class called utils, call this function with the following code (for example, to display the word "Settings".

 NSLog( [utils localized:@"Settings"] ); 

To change the language:

 [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:langCode, nil] forKey:@"AppleLanguages"]; 
+15
source share

Use to set the word order by force

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

then use

 NSLocalizedString(); 

to show a localized string ...

+5
source share

The trick to using a particular language by selecting it from the application is to force NSLocalizedString to use a specific package depending on the language selected,

here is the post i wrote for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

and here is the code for one example application https://github.com/object2dot0/Advance-Localization-in-ios-apps

+5
source share

The correct "user experience" is designed so that the user can select their language using the system preferences panel; not your application (or application settings panel, etc.). You cannot override this application, and you do not want any application to change the system settings.

+1
source share

All Articles