如何在运行时刷新 localizable.strings 或 NSLocalizedString(key, comment) ios
how to refresh localizable.strings or NSLocalizedString(key, comment) ios during runtime
我正在开发一款支持多种语言的应用程序。一切进行得都很顺利。但是我需要在运行时更改语言。
我是这样做的-
NSArray* languages = [NSArray arrayWithObjects:@"es",@"en" nil];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
它运行良好。
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
langID
显示的是正确的语言。但是应用程序在重新启动之前不会更改其语言。
有没有办法刷新localizable.strings文件或NSLocalizedString(key, comment)?或任何其他无需重新启动的方法?
这个问题已经在 Whosebug 上被问过很多次了(搜索 iOS change language at runtime
之类的东西)。
使用 NSLocalizedString
时无法在运行时更改语言。如果您真的非常需要它,则需要使用自定义本地化系统。更好的是:根本不要那样做:这是出乎意料的。毕竟,用户可以在系统设置中设置 his/her 首选语言(及其顺序)。
Someone even asked an Apple engineer 这是回复:
In general, you should not change the iOS system language (via use of the AppleLanguages pref key) from within your application. This goes against the basic iOS user model for switching languages in the Settings app, and also uses a preference key that is not documented, meaning that at some point in the future, the key name could change, which would break your application.
If you want to switch languages in your application, you can do so via manually loading resource files in your bundle. You can use NSBundle:pathForResource:ofType:inDirectory:forLocalization: for this purpose, but keep in mind that your application would be responsible for all loading of localized data.
要在运行时更改应用程序语言,我通过创建两个不同的故事板手动完成了此操作。我在 NSUserDefaults
中保存了语言首选项,但没有使用键 AppleLanguages
,然后将 AppDelegate 的 didFinishLaunchingWithOptions
方法调用到 select 故事板。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainEnglish" bundle:nil];
UINavigationController *navigationcontroller=[[UINavigationController alloc]init];
RegistorViewController *registor=[storyboard instantiateViewControllerWithIdentifier:@"Registor"];
[self.window setRootViewController:navigationcontroller];
[self.window makeKeyAndVisible];
[navigationvontroller pushViewController:registor animated:NO];
我正在开发一款支持多种语言的应用程序。一切进行得都很顺利。但是我需要在运行时更改语言。
我是这样做的-
NSArray* languages = [NSArray arrayWithObjects:@"es",@"en" nil];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
它运行良好。
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
langID
显示的是正确的语言。但是应用程序在重新启动之前不会更改其语言。
有没有办法刷新localizable.strings文件或NSLocalizedString(key, comment)?或任何其他无需重新启动的方法?
这个问题已经在 Whosebug 上被问过很多次了(搜索 iOS change language at runtime
之类的东西)。
使用 NSLocalizedString
时无法在运行时更改语言。如果您真的非常需要它,则需要使用自定义本地化系统。更好的是:根本不要那样做:这是出乎意料的。毕竟,用户可以在系统设置中设置 his/her 首选语言(及其顺序)。
Someone even asked an Apple engineer 这是回复:
In general, you should not change the iOS system language (via use of the AppleLanguages pref key) from within your application. This goes against the basic iOS user model for switching languages in the Settings app, and also uses a preference key that is not documented, meaning that at some point in the future, the key name could change, which would break your application.
If you want to switch languages in your application, you can do so via manually loading resource files in your bundle. You can use NSBundle:pathForResource:ofType:inDirectory:forLocalization: for this purpose, but keep in mind that your application would be responsible for all loading of localized data.
要在运行时更改应用程序语言,我通过创建两个不同的故事板手动完成了此操作。我在 NSUserDefaults
中保存了语言首选项,但没有使用键 AppleLanguages
,然后将 AppDelegate 的 didFinishLaunchingWithOptions
方法调用到 select 故事板。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainEnglish" bundle:nil];
UINavigationController *navigationcontroller=[[UINavigationController alloc]init];
RegistorViewController *registor=[storyboard instantiateViewControllerWithIdentifier:@"Registor"];
[self.window setRootViewController:navigationcontroller];
[self.window makeKeyAndVisible];
[navigationvontroller pushViewController:registor animated:NO];