繼承人我做了什麼。我猜訣竅是使用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