重新加载故事板以更改语言环境 [英] Reload storyboard to change locale

查看:23
本文介绍了重新加载故事板以更改语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用一个本地化的 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:

  • 每种语言都有不同的故事板文件(难以管理,我不想这样做)

  • 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 的基础翻译上,这意味着我只有一个 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:

  • 将每种语言的基本翻译转换为一个 Storyboard(您可以在 Xcode 中单击 Storyboard 并将类型设置为 Storyboard from Strings 时执行此操作).你可以在最后删除 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.

    • 现在每个字符串都必须正常工作.只剩下一件事:当您更改语言时,您在 Storyboards 中设置的图像会消失,因为 iOS 在捆绑包中找不到它们.对此的解决方案是将它们全部本地化(使用文件检查器中的本地化..."按钮).这显然会复制图像,但请放心,不会影响您的 .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

    语言更改代码只需在用户按下按钮时运行一次

    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.

    这篇关于重新加载故事板以更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆