Language translation in iphone
I am trying to build an app which consis开发者_运维技巧ts of a label which displays the text in English by default. The user gets a list to select his/her language and after selecting the language, the text changes to that particular language. Any idea as to how to do it? I had tried
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"];
but it is not working.
You can do that with :
-[NSBundle localizedStringForKey:value:table:]
There is a small sample in the docs. Basically what you need to do is create a MyTable.strings file with the localizations you need. Create one file per language you need. Then do:
NSBundle *bundle = [NSBundle mainBundle];
NSString *localizedString = [bundle localizedStringForKey:@"TheKeyYouWantToLocalize"]
value:@"TheDefaultValue"
table:@"MyTable"];
This method will look for the key: @"TheKeyYouWantToLocalize"
in MyTable.strings file , if its is found then it will return that otherwise it will return @"TheDefaultValue"
FYI, This is the same process the system uses when localizing an app. (Heard of NSLocalizedString
?) but now you have to do it manually since you are asking the user the language to show and not relying in the system language.
精彩评论