Skip to content

Instantly share code, notes, and snippets.

@mundstein
Created August 27, 2012 21:01
Show Gist options
  • Save mundstein/3492187 to your computer and use it in GitHub Desktop.
Save mundstein/3492187 to your computer and use it in GitHub Desktop.
Easy way to localize independent of user setting
// The main localization class that does the hard work
-(NSString *) localized:(NSString *) key
{
// 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 output the word Settings
NSLog( [utils localized:@"Settings"] );
// To change the language
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:langCode, nil] forKey:@"AppleLanguages"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment