问题描述
我正在为我的应用程序使用单个本地化的Storyboard。该应用程序必须有一个选项来动态更改语言。到目前为止,我想出了如何更改应用程序启动期间使用的语言:
I am using a single, localized Storyboard for my application. The app has to have an option to change language on the fly. So far I figured out how to change the language which is used during the app launch:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
并设法通过删除密钥的所有子视图来重新加载我的整个应用程序窗口,从故事板重新加载初始视图控制器并使其成为新的根视图控制器。 (它有一些问题,比如管理开放资源,但可以做到。)
And managed to reload my entire application by removing all subviews of the key window, reloading the initial view controller from the storyboard and making it the new root view controller. (It has some problems, like managing open resources, but it can be done.)
我唯一想知道的是如何强制iOS重新加载新语言的故事板?似乎故事板以某种方式缓存,并且 -UIStoryboad:stroyboardWithName:fromNib
不会完全重新加载它,因此它以最后一种语言显示。
The only thing I couldn't figure out is how do I force iOS to reload the storyboard in the new language? It seems storyboards are cached somehow, and -UIStoryboad:stroyboardWithName:fromNib
does not reload it entirely, so it appears in the last language.
我已经考虑了其他两个选项:
I've already considered two other options:
-
为每种语言设置不同的故事板文件(它是unamanageable,我不想这样做)
having different storyboard files for each language (it is unamanageable, I don't want to do this)
在我的所有视图控制器中使用事件监听器来响应语言变化并设置新的字符串UI(每次从故事板中实例化任何界面元素时也必须调用它)。这个解决方案需要更多的时间,比我们想要花费更多的时间,并且还大大增加了错误的可能性。
using event listeners in all of my view controllers which respond to a language change and set the new strings on the UI (which is also has to be called every time any interface element gets instantiated from the storyboard). This solution requires more time, than we'd like to spend with this, and also highly increases the possibility of bugs.
有更聪明的方法吗?我不想保持我的应用程序的当前状态,所以重新启动它就像它被杀死和实现似乎是一个合理的解决方案。
Is there some more clever way? I don't want to keep the current state of my application, so restarting it like it was killed and realunched seems a reasonable resolution.
推荐答案
所以我发现问题出在我的Storyboard的Base翻译中,这意味着我只有一个Storyboard文件和一个.strings文件。使用Base翻译无法完成,Apple不支持。
So I found that the problem is with Base translation of my Storyboard, which means I had only one Storyboard file and one .strings file per language for it. It can't be done using Base translations, Apple does not support it.
所以我的解决方法如下:
So my workaround was the following:
-
将基本翻译转换为每种语言的一个故事板(当您单击故事板并将类型设置为字符串中的故事板时,您可以在Xcode中执行此操作)。你可以在最后删除Base。
Convert Base translations to one Storyboard each language (you can do it in Xcode when you click the Storyboard and set the type to Storyboard from Strings). You can delete the Base at the end.
翻译故事板中的字符串
在语言更改按钮的按钮处理程序中执行以下操作:
In the button handler of your language change button do the following:
(我使用制表符控制器作为根控制器,你应该把它改成你的根控制器类型。)
(I am using tab controller as root controller, you should change this to your root controller type.)
[[NSDefaults standardDefaults] setObject:[NSArray arrayWithObject:@"hu"] forKey:@"AppleLanguages"];
[[NSDefaults standardDefaults] synchronize];
GKAppDelegate *appDelegate = (GKAppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController* tabBar = (UITabBarController *)appDelegate.window.rootViewController;
// reload the storyboard in the selected language
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:appDelegate.lang ofType:@"lproj"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:bundle];
// reload the view controllers
UINavigationController *placesTab = (UINavigationController *)[storyBoard instantiateViewControllerWithIdentifier:@"placesTab"];
UINavigationController *informationTab = (UINavigationController *)[storyBoard instantiateViewControllerWithIdentifier:@"informationTab"];
// set them
NSArray *newViewControllers = @[placesTab, informationTab];
tabBar.viewControllers = newViewControllers;
- 这还不够。在此之后,字符串表单Storyboard应该更改语言,但代码中的字符串不会。为此你必须这样做:
在你的 appname.pch
文件中:
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
你应该改变所有这些:
NSLocalizedString(@"key", @"comment")
到此:
NSLocalizedStringFromTableInBundle(@"key", nil, currentLanguageBundle, @"");
请注意,我删除了第二个参数,评论。看起来非ASCII字母的注释可能会带来麻烦,所以最好在这一点上避免使用它们。
Please notice, that I removed the second parameter, the comment. It seems comments with non-ASCII letters can cause trouble, so it's better to avoid them at this point.
- 现在每个字符串都必须工作精细。只剩下一件事:当您更改语言时,您在Storyboard中设置的图像会消失,因为iOS在捆绑中找不到它们。解决方案是本地化所有这些(使用文件检查器中的Localize ...按钮)。这显然会复制图像,但不要担心,不会影响.ipa大小,因为ZIP处理重复很好。 (我只有800→850k大小的凹凸。)
这种方法的冒险:
-
您可以使用系统工具将字符串收集到.strings文件
you can use system tools to gather strings to .strings files
UI更改语言时不会闪烁
the UI will not flicker when you change language
您不必编写大量代码或使用大量网点
you don't have to write a lot of code or use a lot of outlets
当用户按下按钮时,语言changig代码只需运行一次
language changig code needs to be ran only once, when the user pushes the button
我希望这会有所帮助,也许你可以改进我的解决方案。
I hope this helps, and maybe you can improve my solution.
这篇关于重新加载故事板以更改区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!