xcode 如何将非英语语言设置为开发语言和Base本地化?

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

How to set language other than English as the development language and Base localization?

iosobjective-cxcodelocalizationinternationalization

提问by goodbyeera

When an Xcode project of iOS app is newly created, in the project (not target) setting we can see that it has Use Base internationalizationenabled by default, and there are 2 files localized for the Development Languagewhich is Englishby default (one for Main.storyboardand one for LaunchScreen.storyboard, both of which reside in the Base.lprojdirectory). See below the screenshot:

新建一个iOS app的Xcode工程时,在project(not target)设置中我们可以看到它Use Base internationalization默认是开启的,并且有2个文件Development LanguageEnglish默认本地化的(一个forMain.storyboard和一个for LaunchScreen.storyboard,两者都是驻留在Base.lproj目录中)。请看下面的截图:

Xcode default project localization setting

Xcode 默认项目本地化设置

(For simplicity, I will only mention Main.storyboardfrom now on.)

(为简单起见,我Main.storyboard从现在开始只提及。)

And in the localization section of Main.storyboard's right-side panel, we can see that the Baselocalization is checked by default and there is also an Englishlocalization which is unchecked by default. See below the screenshot:

Main.storyboard右侧面板的本地化部分,我们可以看到Base默认English选中本地化,还有一个默认未选中的本地化。请看下面的截图:

storyboard default localization setting

故事板默认本地化设置

And in the target's Info.plist, there is a key named Localization native development region(i.e. CFBundleDevelopmentRegion), and its default value is en. See below the screen shot:

并且在目标的 中Info.plist,有一个名为Localization native development region(即CFBundleDevelopmentRegion)的键,其默认值为en。请参阅下面的屏幕截图:

Localization native development region

本地化原生开发区

If I understand it correctly, with these default settings, developer can just write English in the Baselocalization of Main.storyboardand leave the Englishlocalization as unchecked. If the app needs to adapt to some other language, developer can add a localization in the project localization setting, and select Main.storyboardin the popped up dialog. See below the screenshot:

如果我理解正确,使用这些默认设置,开发人员可以只在Base本地化中编写英语,Main.storyboard并且English不选中本地化。如果app需要适配其他语言,开发者可以在项目本地化设置中添加本地化,Main.storyboard在弹出的对话框中选择。请看下面的截图:

add localization

添加本地化

Take Chinese (Simplified)as an example, this will result in a newly created directory zh-Hans.lprojwith a Main.stringsfile inside it. (For simplicity, I will refer to it as Chineseinstead of Chinese (Simplified).) Developer just needs to translate the English strings inside this file into Chinese (the newly created Main.stringsin the zh-Hans.lprojdirectory by default has all the English texts duplicated from the Main.storyboardin Base.lproj). With these settings, the Languagefield in the description of this app on AppStore will list English (from the Baselocalization, because English is the development language) and Chinese (from the Chineselocalization). On end-user's device, if the system language is English/Chinese (or English/Chinese is among the preferred language), the app will use the corresponding language resource (for English, use the Baselocalization; for Chinese, use the Chineselocalization). For all other language preference, English will act as the fallback language because CFBundleDevelopmentRegionis en, so Baselocalization is used.

Chinese (Simplified)为例,这将导致一个新创建的目录zh-Hans.lprojMain.strings里面的文件。(为简单起见,我将其称为Chinese而不是Chinese (Simplified)。)开发人员只需要将此文件中的英文字符串翻译成中文(默认情况下Main.stringszh-Hans.lproj目录中新创建的所有英文文本都是从Main.storyboardin复制的Base.lproj)。通过这些设置,LanguageAppStore 上这个应用程序的描述字段将列出英文(来自Base本地化,因为英文是development language)和中文(来自Chinese本土化)。在最终用户的设备上,如果系统语言为英文/中文(或英文/中文为首选语言),则应用程序将使用相应的语言资源(英语,使用Base本地化;中文,使用Chinese本地化)。对于所有其他语言偏好,英语将作为后备语言,因为CFBundleDevelopmentRegionen,因此使用Base本地化。

So my first question will be, is the above understanding correct? To summarize, with the development languagebeing English, we don't need to enable Englishlocalization for storyboard files. Just use the Baselocalization and directly write English in the storyboard files. We only need to add localization for languages other than English. (Actually it seems troublesome if we enable the Englishlocalization. If Englishlocalization is enabled, then we need to maintain both the texts in Base.lproj/Main.storyboardand en.lproj/Main.strings.)

所以我的第一个问题是,上述理解是否正确?总而言之,由于development language是英语,我们不需要English为故事板文件启用本地化。只需使用Base本地化并直接在故事板文件中编写英文即可。我们只需要为英语以外的语言添加本地化。(实际上,如果我们使这似乎麻烦的English定位。如果English定位被启用,那么我们需要保持在两个文本Base.lproj/Main.storyboarden.lproj/Main.strings)。

If the above understanding is correct, my second question will be, how to achieve internationalization in a reverse way? That is to say, if I would like to use Chineseas the development languageand write Chinese in the Baselocalization, and only add localization for languages other than Chinese, is it possible and how to do that in Xcode? I can't find a way to change the development languagein the project setting in order to claim that the Baselocalization in the project is Chineserather than English.

如果上述理解正确,我的第二个问题将是,如何逆向实现国际化?也就是说,如果我想在本地化中使用Chineseasdevelopment language和写中文Base,并且只为 以外的语言添加本地化Chinese,是否可以以及如何在 Xcode 中做到这一点?我找不到一种方法来更改development language项目设置中的 ,以声称项目中的Base本地化是Chinese而不是English.

回答by lensovet

Your understanding is mostly correct. One small clarification: if the user is running in some third language, like Russian, it is not necessarily true that the app will run in English. Instead, the app will run in the first language it supports that the user prefers. As of iOS 8 and later, users can have a listof preferred languages, such as "Russian, French, Chinese, English". In the case of this hypothetical user, the app would actually run in Chinese, since it is earlier in the list than English.

你的理解大部分是正确的。一个小小的说明:如果用户使用第三种语言(例如俄语)运行,则该应用程序不一定会以英语运行。相反,该应用程序将以其支持的用户喜欢的第一种语言运行。从 iOS 8 及更高版本开始,用户可以获得首选语言列表,例如“俄语、法语、中文、英语”。在这个假设用户的情况下,该应用程序实际上会以中文运行,因为它在列表中比英文早。

To answer your question regarding changing the development language, this isn't straightforward to do but can be done. Specifically:

要回答有关更改开发语言的问题,这不是一件容易的事,但可以做到。具体来说:

  1. Add the language you want to be your Base language first. Uncheckall of the files that Xcode offers to localize for you.
  2. In the Info.plist(as you observed), change the development region to the language that you want to be your base language. Note that the property is a bit misnamed, because its value should be a language code(with an optional country code), rather than a region or country code.
  3. Close your project in Xcode. In another code editor, open projectname.xcodeproj/project.pbxprojand search for developmentRegion. You should see a line like developmentRegion = English;. Change this to reference the same language you put in your Info.plistfile.
  4. Reopen the project in Xcode. Go through all your localizable files and check the boxes next to English to generate the localizable resources. Note that for storyboards and xibs, Xcode might create a storyboard instead of a strings file. If that happens, just change the filetype to strings file.
  1. 首先添加要作为基本语言的语言。取消选中Xcode 为您提供本地化的所有文件。
  2. Info.plist(如您所见)中,将开发区域更改为您希望作为基本语言的语言。请注意,该属性的名称有点错误,因为它的值应该是语言代码(带有可选的国家/地区代码),而不是地区或国家/地区代码。
  3. 在 Xcode 中关闭您的项目。在另一个代码编辑器中,打开projectname.xcodeproj/project.pbxproj并搜索developmentRegion. 你应该看到像developmentRegion = English;. 将此更改为引用您放入Info.plist文件中的相同语言。
  4. 在 Xcode 中重新打开项目。浏览所有可本地化的文件并选中英语旁边的框以生成可本地化的资源。请注意,对于故事板和 xib,Xcode 可能会创建故事板而不是字符串文件。如果发生这种情况,只需将文件类型更改为字符串文件。

Here's an example of the result for me using fras the Base language:

这是我fr用作基本语言的结果示例:

Project info

项目信息

File info

文件信息