Posted By

zamza726 on 02/14/11


Tagged

strings localization Localizable


Versions (?)

Print different Localizable string in App


 / Published in: Objective C
 

URL: http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language

  1. //1. System change
  2. /* Example call: NSLocalizedString(@"Key", @"");
  3.   Below should be implemented in main. Because the language needs to be set "sometime early in your application's startup"
  4. */
  5. [[NSUserDefaults standardUserDefaults]
  6. setObject:[NSArray arrayWithObject:@"korea"]
  7. forKey:@"AppleLanguages"];
  8.  
  9. //2. Get information through bundle -- Useful !!!
  10. #define MyLocalizedString(key, alt) [Language get:key alter:alt]
  11. @implementation Language
  12.  
  13. static NSBundle *bundle = nil;
  14.  
  15. +(void)initialize {
  16. NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
  17. NSArray* languages = [defs objectForKey:@"AppleLanguages"];
  18. NSString *current = [[languages objectAtIndex:0] retain];
  19. [self setLanguage:current];
  20.  
  21. }
  22.  
  23. /*
  24.   example calls:
  25.   [Language setLanguage:@"it"];
  26.   [Language setLanguage:@"de"];
  27.   MyLocalizedString(@"Key", @"");
  28. */
  29. +(void)setLanguage:(NSString *)l {
  30. NSLog(@"preferredLang: %@", l);
  31. NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
  32. bundle = [[NSBundle bundleWithPath:path] retain];
  33. }
  34.  
  35. +(NSString *)get:(NSString *)key alter:(NSString *)alternate {
  36. return [bundle localizedStringForKey:key value:alternate table:nil];
  37. }
  38.  
  39. @end

Report this snippet  

You need to login to post a comment.