开发者

internationalization in iphone application

开发者 https://www.devze.com 2023-03-09 04:42 出处:网络
my application run vary well but now when UISwitch button is on at that time i have to开发者_如何学Python convet whole application in spanish when off then convert in to english how it possible plz gi

my application run vary well but now when UISwitch button is on at that time i have to开发者_如何学Python convet whole application in spanish when off then convert in to english how it possible plz give any replay for that.


i18n

Create the following structure:

resources/i18n/en.lproj/Localizable.strings
resources/i18n/es.lproj/Localizable.strings

Create an additional directory with the corresponding two letter code for each additional language supported.

It's recommended to encode Localized.strings in UTF-16. You can convert between encodings in the inspector pane of XCode.

If the files are recognized as i18n resources, they will be presented like this:

internationalization in iphone application

A sample file has the following content:

"hello"="hola";

Then use the following in your program:

NSString *string = NSLocalizedString(@"hello", nil);

Choose language dynamically

To change the language for your application dynamically use this code:

@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:@"es"];
[Language setLanguage:@"en"];
*/
+(void)setLanguage:(NSString *)code {
    NSLog(@"preferredLang: %@", code);
    NSString *path = [[ NSBundle mainBundle ] pathForResource:code ofType:@"lproj" ];
    // Use bundle = [NSBundle mainBundle] if you
    // dont have all localization files in your project.
    bundle = [[NSBundle bundleWithPath:path] retain];
}

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

@end

Then translate your strings like this:

NSString *hello [Language get:@"hello", nil, nil];

The code above was originally posted by Mauro Delrio as an answer to How to force NSLocalizedString to use a specific language.

0

精彩评论

暂无评论...
验证码 换一张
取 消