在iOS中更改应用语言,而无需重新启动应用 [英] Change app language in iOS without restarting the app

查看:341
本文介绍了在iOS中更改应用语言,而无需重新启动应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有些应用程式可以在应用程式内部更改语言,而不需要重新启动应用程式,我想知道如何实现。

I have seems some apps can change the language internally within the app without the need of restarting the app, I am wondering how they are implemented.

例如,对于我们使用 NSLocalizedString ,我知道可以在运行时设置语言 main.m 当你的 AppDelegate 未初始化,但是一旦初始化(特别是创建了视图控制器),更改不会生效,直到下一个重新启动

For example, for us using NSLocalizedString, I know it is possible to set the language at runtime at main.m when your AppDelegate is not initialized, but once it is initialized (particularly your view controller is created), change it has not effect until the next restart

[[NSUserDefaults standardUserDefaults] 
    setObject:[NSMutableArray arrayWithObjects:language, nil] 
    forKey:@"AppleLanguages"];

任何人都知道这些动态语言更改可以在不重新启动应用程序的情况下完成吗?

Anyone have idea how those dynamic language change can be done without restarting the app?

推荐答案

这里有一些其他方法的讨论,特别是基于通知的方法:

There's some discussion of other approaches here, in particular a notification based approach:

iOS:如何更改应用程式

在我看来,这里真的有三个任务:
(1)资源自动重新定位从笔尖。 (例如,如果从一个nib动态实例化另一个自定义UIView,仍然会加载旧语言字符串和设置(图像,文本方向))
(2)重新定位当前显示在屏幕上的字符串。
(3)重新定位开发者(you)在程序代码中插入的字符串。

In my view there are really three tasks here: (1) re-localization of resources automatically loaded from nibs. (for example if you dynamically instantiate another custom UIView from a nib, the "old" language strings and settings (images, text direction) will still be loaded) (2) re-localization of strings currently displayed on the screen. (3) re-localization of strings inserted by the developer (you) in program code.

让我们从第(3)开始。如果你寻找的定义,你会注意到NSLocalizedString是一个宏。所以如果你不想改变现有的代码太多,你可以通过创建一个新的头文件解决问题(3)。在该头文件中, #undef 然后重新 - #define NSLocalizedString 从合适的位置选择本地化的字符串 - - 不是iOS默认的,但是一个你跟踪在一些全局变量(例如,在一个应用程序委托ivar)。如果你不想重新定义NSLocalizedString但是你仍然使用自己的替代方法,你应该可能仍然 #undef NSLocalizedString 如果你不希望未来的开发者意外调用它的宏替换它。

Let's start with (3). If you look for the definition you will notice that NSLocalizedString is a macro. So if you don't want to change existing code too much, you can probably solve the problem of (3) by creating a new header file. In that header file, #undef and then re-#define NSLocalizedString to pick the localized string from the appropriate place--not the one that iOS defaults to, but one that you keep track of in some global variable (e.g., in an app delegate ivar). If you don't want to redefine NSLocalizedString but you still make your own alternative , you should probably still #undef NSLocalizedString if you don't want future developers to accidentally call it instead of the macro you replace it with. Not an ideal solution, but maybe the most practical.

至于(1),如果你没有在Interface Builder中进行本地化,而是动态地在viewDidLoad等,没有问题。您可以使用刚刚讨论的相同行为(即,修改的NSLocalizedString等)。否则,您可以(a)实现上面链接(复杂)中所述的通知系统,或(b)考虑将本地化从IB移动到viewDidLoad,或(c)尝试覆盖 initWithNibName:,并换出用旧语言资源加载的对象,其中一个加载了新的语言资源。这是Mohamed在本次讨论最底层提到的一种方法: http ://learning-ios.blogspot.ca/2011/04/advance-localization-in-ios-apps.html 。他声称它导致问题(viewDidLoad不被调用)。

As for (1), if you haven't done your localization in Interface Builder, but rather you do it dynamically in viewDidLoad, etc., no problem. You can use the same behavior just discussed (i.e., the modified NSLocalizedString, etc.). Otherwise you can either (a) implement a notification system as described in the link above (complicated), or (b) consider moving localization from IB to viewDidLoad, or (c) try overriding initWithNibName: and swap out the object loaded with the old language resources, with one loaded with the new language resources. This was an approach mentioned by Mohamed at the very bottom of this discussion: http://learning-ios.blogspot.ca/2011/04/advance-localization-in-ios-apps.html. He claims it causes problems (viewDidLoad isn't called). Even if it doesn't work, trying it out might point you towards something that does.

最后,(2)可能是最简单的任务:只需删除并重新启动即可,添加当前视图(或在某些情况下,只是重绘它)。

Finally, (2) is presumably the easiest task: just remove and re-add the current view (or in some cases, just redraw it).

这篇关于在iOS中更改应用语言,而无需重新启动应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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