xcode 更改 iOS 中的应用程序语言设置而不是整个设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11578298/
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 00:57:23  来源:igfitidea点击:

change application language setting in iOS not the whole device

objective-cxcodeios5

提问by Elnaz

I want to have an option in my application to change my application language (just application language not the whole system), would you please give me some hints?

我想在我的应用程序中有一个选项来更改我的应用程序语言(只是应用程序语言而不是整个系统),你能给我一些提示吗?

Thanks in advance,

提前致谢,

I know about localisation!

我知道本地化!

采纳答案by Abizern

You can do this by using custom build flags that you set up from Xcode. This way you can run an application under a localisation rather than changing all the settings for the device.

您可以使用从 Xcode 设置的自定义构建标志来完成此操作。通过这种方式,您可以在本地化下运行应用程序,而不是更改设备的所有设置。

I wrote a blog post about it at http://abizern.org/2012/03/18/simple-localisation-testing/

我在http://abizern.org/2012/03/18/simple-localisation-testing/写了一篇关于它的博客文章

And there is an example project on Github that you can use to see it in action: https://github.com/Abizern/SimpleLocalisationTesting

Github 上有一个示例项目,您可以使用它来查看它的实际运行情况:https: //github.com/Abizern/SimpleLocalisationTesting

回答by Bernd Rabe

I was actually thinking about another idea on how to change the language in your apps ui, though it forces you to 1. set all strings manually a 2. provide a method (notification) that updates all ui elements when the language setting has changed. The idea is based on the same method iOS uses to find the localized version of a string. So as a prerequisite you have to provide a bundle (e.g. AppNameStrings.bundle) with a folder structure language_id.lproj where language_id is the designator for the languages you want to provide. You can create such a bundle by creating a settings bundle, open it in Finder and the delete the root.plist file. Next you need a function you can use to read from your bundle. Here are 2 methods I use to read localized strings. Put the following methods in your GlobalDefinitions.h file

我实际上正在考虑如何更改应用程序 ui 中的语言的另一个想法,尽管它强制您 1. 手动设置所有字符串 2. 提供一种方法(通知),在语言设置更改时更新所有 ui 元素。这个想法基于 iOS 用来查找字符串的本地化版本的相同方法。因此,作为先决条件,您必须提供具有文件夹结构 language_id.lproj 的包(例如 AppNameStrings.bundle),其中 language_id 是您要提供的语言的指示符。您可以通过创建设置包来创建这样的包,在 Finder 中打开它并删除 root.plist 文件。接下来,您需要一个可用于从包中读取的函数。这是我用来读取本地化字符串的 2 种方法。将以下方法放在您的 GlobalDefinitions.h 文件中

#ifdef __OBJC__  
NSBundle *yourNameBundle(void);
NSString *YourNameLocalized(NSString *stringToken, NSString *language);
#endif

and this code in your GlobalDefinitions.m file

以及您的 GlobalDefinitions.m 文件中的此代码

NSBundle *yourNameBundle(void) {
static NSBundle* bundle = nil;
  if (!bundle) {
    NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YouNameBundle.bundle"];
    bundle = [NSBundle bundleWithPath:path];
  }
   return bundle;
}
NSString *YourNameLocalized(NSString *stringToken, NSString *language) {
  if (yourNameBundle()) {
    return [yourBundle localizedStringForKey:stringToken value:@"" table:@"Root" language:language];
  } else {
    return stringToken;
  }
}

You have to create a category on NSBundle for

你必须在 NSBundle 上创建一个类别

[yourBundle localizedStringForKey:stringToken value:@"" table:@"Root" language:language]

as the standard method misses the language parameter. In your method you open the corresponding language.lproj/Root.strings file and scan it for the stringToken. If you can't find it return the token itself otherwise the text string. Whenever you need a localized version of some text you write e.g. label.text = YourNameLocalized(@"Token", [self provideLanguageIdentifier); and you are done. I haven't done it so far but it should work.

因为标准方法错过了语言参数。在您的方法中,您打开相应的 language.lproj/Root.strings 文件并扫描它以查找 stringToken。如果找不到它,则返回令牌本身,否则返回文本字符串。每当您需要编写某些文本的本地化版本时,例如 label.text = YourNameLocalized(@"Token", [self provideLanguageIdentifier); 你就完成了。到目前为止我还没有这样做,但它应该可以工作。

回答by Pavel Oganesyan

I've made custom localization made by .plist filled with array of dictionaries. Like this:

我制作了由 .plist 制作的自定义本地化,其中填充了字典数组。像这样:

kYes
  en = @"Yes"
  ru = @"Да"
kNo
  en = @"No"
  ru = @"Нет"

In my project I made a singleton class for reading this strings. In options user selects needed language (at first start app select language from current system state or sets default one). Selected language must be saved in file or in settings to use on the next start. In singleton I have +getStringWithKey:. So I pass there @"kYes" and get string for current localization for app only. Also, you will need to add standard localization files to project anyway, even empty, because if you don't, all languages you support will not be shown in iTunes.

在我的项目中,我创建了一个单例类来读取这些字符串。在选项中,用户选择所需的语言(首先启动应用程序从当前系统状态选择语言或设置默认语言)。所选语言必须保存在文件或设置中,以便下次启动时使用。在单身我有+getStringWithKey:。所以我通过 @"kYes" 并获取仅用于应用程序当前本地化的字符串。此外,您无论如何都需要将标准本地化文件添加到项目中,即使是空的,因为如果不这样做,您支持的所有语言都不会显示在 iTunes 中。