如何以编程方式更改应用程序语言而无需重新启动我的应用程序? [英] How to change app language programmatically WITHOUT restarting my app?

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

问题描述

如果我只需要更改文本字符串,它将很简单,但是如何更改图像或Xib文件(针对每个本地化)?

If i would need change only text string it will simple, but How can I change images or xib files (which are for each localisation)?

设置本地化的代码:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"Ukrainian", @"English", nil] forKey:@"AppleLanguages"];

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects: @"English", @"Ukrainian", nil] forKey:@"AppleLanguages"];

(如果我已经完成了这段代码,并且应用程序已关闭,那么下次启动应用程序时,它将使用为所选语言设置的所有资源)

(if I have done this code, and app was closed so next time when app start it use all resources which set for selected language)

我该怎么做但又不重新启动应用程序?

How can I do the same but without restarting app?

我发现了很多示例,但是这些示例说明了如何使用文本,但是如何使用其他资源,以及如何更新(对于当前的选择语言)?

I found many examples, but these show how work with text, but how work with another resources, and how can I their update (for current select language)?

示例:即时更改iOS应用的语言

仅在iPhone上重启后语言更改

推荐答案

您不能这样做(没有越狱).您应该以某种方式对应用程序进行编码,以便在某些重要信息发生更改时可以重新开始,而无需重新启动应用程序.例如,您可以弹出所有视图控制器并取消分配它们,然后让用户使用正确的语言重新加载它们.

You can't do that (without jailbreaking). You should code your app in a way, that you can start over when some significant information changes, without restarting the app. For instance you could pop all the view controllers and deallocate them and then let the user reload them using the correct language.

但是请注意,通常您不应该那样做.在iOS上,以非常优雅的方式解决了本地化问题.您只需本地化.strings(以及可选的.nib和图像文件),系统就会为您其余的工作.当用户在设置"应用中更改语言时,系统将终止您的应用.下次用户启动该应用程序时,它将加载正确的语言.

Note however, that normally you shouldn't do anything like that. Localization is solved in a quite elegant way on iOS. You just localize the .strings (and optionally .nib and image files) and the system takes care for the rest for you. When the user changes the language in the Settings app, you app will be terminated by the system. The next time the user starts the app, it will load the correct language.

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

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