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

查看:25
本文介绍了在 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 的我们来说,我知道当您的 AppDelegate 时,可以在 main.m 处设置运行时语言没有初始化,但是一旦初始化(特别是你的视图控制器被创建),改变它直到下一次restart

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) 重新本地化从 nibs 自动加载的资源.(例如,如果您从 nib 动态实例化另一个自定义 UIView,旧"语言字符串和设置(图像、文本方向)仍将被加载)(2) 重新本地化当前显示在屏幕上的字符串.(3) 开发者(你)在程序代码中插入的字符串的重新本地化.

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天全站免登陆