继承人我做了什么。我猜诀窍是使用NSLocalizedStringFromTableInBundle而不是NSLocalizedString。
的所有字符串,用这个
someLabel.text = NSLocalizedStringFromTableInBundle(@"Your String to be localized, %@",nil,self.localeBundle,@"some context for translators");
要改变语言,运行这段代码
NSString * language = @"zh-Hans"; //or whatever language you want
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
if (path) {
self.localeBundle = [NSBundle bundleWithPath:path];
}
else {
self.localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"] ];
}
在此之后,你可能会想打电话给任何更新代码更新字符串新语言,例如再次运行这个
someLabel.text = NSLocalizedStringFromTableInBundle(@"Your String to be localized, %@",nil,self.localeBundle,@"some context for translators");
那就是所有。无需重启应用程序。与系统设置兼容(如果您通过iOS设置设置语言,它也会起作用)。不需要外部库。不需要越狱。它也适用于起跳线。
当然,你还是应该做平常的应用设置中坚持:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
(和你的viewDidLoad或东西做一次检查)
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
if (path) {
self.localeBundle = [NSBundle bundleWithPath:path];
}
else {
self.localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"] ];
}
检查这个答案它的工作原理http://stackoverflow.com/questions/5912018/language-change-only-after-restart- iphone- – 2013-05-01 15:43:15