xcode 如何在不重新启动我的应用程序的情况下以编程方式更改应用程序语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15786501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:07:13  来源:igfitidea点击:

How to change app language programmatically WITHOUT restarting my app?

iphoneiosobjective-cxcode

提问by Vit

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)?

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

code which set localisation:

设置本地化的代码:

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

or

或者

[[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)?

我找到了很多例子,但这些展示了如何使用文本,但如何使用其他资源,以及如何更新它们(对于当前选择的语言)?

examples: Change iOS app's language on the fly

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

language change only after restart on iphone

仅在 iphone 重启后才更改语言

回答by DrummerB

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.

你不能这样做(没有越狱)。您应该以某种方式编写应用程序,以便在某些重要信息发生变化时可以重新开始,而无需重新启动应用程序。例如,您可以弹出所有视图控制器并释放它们,然后让用户使用正确的语言重新加载它们。

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.

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

回答by Mihir Mehta

It's not possible,

这是不可能的,

Though there is one trick

虽然只有一招

1> From your application Open safari Browser with custom URL (e.g. com.yourcompany.yourApp)

1> 从您的应用程序打开带有自定义 URL 的 safari 浏览器(例如 com.yourcompany.yourApp)

2> register that Custom URL with your app(from plist file you can do that), which will open your application from safari when that custom url open in safari.

2> 在您的应用程序中注册该自定义 URL(从 plist 文件中您可以这样做),当该自定义 URL 在 safari 中打开时,它将从 safari 打开您的应用程序。

3> turn off your "Application should run in background" from your .plist file. (you can achieve this by adding the UIApplicationExitsOnSuspend key to your application's Info.plist file and setting its value to YES.)

3> 从 .plist 文件中关闭“应用程序应在后台运行”。(您可以通过将 UIApplicationExitsOnSuspend 键添加到应用程序的 Info.plist 文件并将其值设置为 YES 来实现此目的。)

回答by Lithu T.V

Why restart the application?I think it is not possible.But you can do whatever to change in the applicationDidBecomeActiveor applicationWillEnterForegroundMethod

为什么要重新启动应用程序?我认为这是不可能的。但是您可以在applicationDidBecomeActiveorapplicationWillEnterForeground方法中做任何更改

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}