Return to Snippet

Revision: 41236
at February 14, 2011 20:29 by zamza726


Updated Code
//1. System change
/* Example call: NSLocalizedString(@"Key", @"");
  Below should be implemented in main. Because the language needs to be set "sometime early in your application's startup"
*/
[[NSUserDefaults standardUserDefaults]
     setObject:[NSArray arrayWithObject:@"korea"]
     forKey:@"AppleLanguages"];
	
//2. Get information through bundle  -- Useful !!!
 #define MyLocalizedString(key, alt) [Language get:key alter:alt]
@implementation Language

static NSBundle *bundle = nil;

+(void)initialize {
 NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 NSArray* languages = [defs objectForKey:@"AppleLanguages"];
 NSString *current = [[languages objectAtIndex:0] retain];
 [self setLanguage:current];

}

/*
  example calls:
  [Language setLanguage:@"it"];
  [Language setLanguage:@"de"];
  MyLocalizedString(@"Key", @"");
*/
+(void)setLanguage:(NSString *)l {
 NSLog(@"preferredLang: %@", l);
 NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
 bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
 return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

Revision: 41235
at February 14, 2011 20:25 by zamza726


Initial Code
//1. System change
[[NSUserDefaults standardUserDefaults]
     setObject:[NSArray arrayWithObject:@"korea"]
     forKey:@"AppleLanguages"];
	
//2. Get information through bundle
 #define MyLocalizedString(key, alt) [Language get:key alter:alt]
@implementation Language

static NSBundle *bundle = nil;

+(void)initialize {
 NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 NSArray* languages = [defs objectForKey:@"AppleLanguages"];
 NSString *current = [[languages objectAtIndex:0] retain];
 [self setLanguage:current];

}

/*
  example calls:
  [Language setLanguage:@"it"];
  [Language setLanguage:@"de"];
  MyLocalizedString(@"Key", @"");
*/
+(void)setLanguage:(NSString *)l {
 NSLog(@"preferredLang: %@", l);
 NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
 bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
 return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

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

Initial Description

                                

Initial Title
Print different Localizable string in App

Initial Tags

                                

Initial Language
Objective C