user763657
user763657

Reputation: 31

internationalization in iphone application

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

Upvotes: 1

Views: 637

Answers (1)

Jano
Jano

Reputation: 63667

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: enter image description here

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.

Upvotes: 1

Related Questions