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
How to set language other than English as the development language and Base localization?
提问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 internationalization
enabled by default, and there are 2 files localized for the Development Language
which is English
by default (one for Main.storyboard
and one for LaunchScreen.storyboard
, both of which reside in the Base.lproj
directory). See below the screenshot:
新建一个iOS app的Xcode工程时,在project(not target)设置中我们可以看到它Use Base internationalization
默认是开启的,并且有2个文件Development Language
是English
默认本地化的(一个forMain.storyboard
和一个for LaunchScreen.storyboard
,两者都是驻留在Base.lproj
目录中)。请看下面的截图:
(For simplicity, I will only mention Main.storyboard
from now on.)
(为简单起见,我Main.storyboard
从现在开始只提及。)
And in the localization section of Main.storyboard
's right-side panel, we can see that the Base
localization is checked by default and there is also an English
localization which is unchecked by default. See below the screenshot:
在Main.storyboard
右侧面板的本地化部分,我们可以看到Base
默认English
选中本地化,还有一个默认未选中的本地化。请看下面的截图:
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
。请参阅下面的屏幕截图:
If I understand it correctly, with these default settings, developer can just write English in the Base
localization of Main.storyboard
and leave the English
localization 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.storyboard
in the popped up dialog. See below the screenshot:
如果我理解正确,使用这些默认设置,开发人员可以只在Base
本地化中编写英语,Main.storyboard
并且English
不选中本地化。如果app需要适配其他语言,开发者可以在项目本地化设置中添加本地化,Main.storyboard
在弹出的对话框中选择。请看下面的截图:
Take Chinese (Simplified)
as an example, this will result in a newly created directory zh-Hans.lproj
with a Main.strings
file inside it. (For simplicity, I will refer to it as Chinese
instead of Chinese (Simplified)
.) Developer just needs to translate the English strings inside this file into Chinese (the newly created Main.strings
in the zh-Hans.lproj
directory by default has all the English texts duplicated from the Main.storyboard
in Base.lproj
). With these settings, the Language
field in the description of this app on AppStore will list English (from the Base
localization, because English is the development language
) and Chinese (from the Chinese
localization). 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 Base
localization; for Chinese, use the Chinese
localization). For all other language preference, English will act as the fallback language because CFBundleDevelopmentRegion
is en
, so Base
localization is used.
以Chinese (Simplified)
为例,这将导致一个新创建的目录zh-Hans.lproj
与Main.strings
里面的文件。(为简单起见,我将其称为Chinese
而不是Chinese (Simplified)
。)开发人员只需要将此文件中的英文字符串翻译成中文(默认情况下Main.strings
,zh-Hans.lproj
目录中新创建的所有英文文本都是从Main.storyboard
in复制的Base.lproj
)。通过这些设置,Language
AppStore 上这个应用程序的描述字段将列出英文(来自Base
本地化,因为英文是development language
)和中文(来自Chinese
本土化)。在最终用户的设备上,如果系统语言为英文/中文(或英文/中文为首选语言),则应用程序将使用相应的语言资源(英语,使用Base
本地化;中文,使用Chinese
本地化)。对于所有其他语言偏好,英语将作为后备语言,因为CFBundleDevelopmentRegion
是en
,因此使用Base
本地化。
So my first question will be, is the above understanding correct? To summarize, with the development language
being English, we don't need to enable English
localization for storyboard files. Just use the Base
localization 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 English
localization. If English
localization is enabled, then we need to maintain both the texts in Base.lproj/Main.storyboard
and en.lproj/Main.strings
.)
所以我的第一个问题是,上述理解是否正确?总而言之,由于development language
是英语,我们不需要English
为故事板文件启用本地化。只需使用Base
本地化并直接在故事板文件中编写英文即可。我们只需要为英语以外的语言添加本地化。(实际上,如果我们使这似乎麻烦的English
定位。如果English
定位被启用,那么我们需要保持在两个文本Base.lproj/Main.storyboard
和en.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 Chinese
as the development language
and write Chinese in the Base
localization, 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 language
in the project setting in order to claim that the Base
localization in the project is Chinese
rather than English
.
如果上述理解正确,我的第二个问题将是,如何逆向实现国际化?也就是说,如果我想在本地化中使用Chinese
asdevelopment 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:
要回答有关更改开发语言的问题,这不是一件容易的事,但可以做到。具体来说:
- Add the language you want to be your Base language first. Uncheckall of the files that Xcode offers to localize for you.
- 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. - Close your project in Xcode. In another code editor, open
projectname.xcodeproj/project.pbxproj
and search fordevelopmentRegion
. You should see a line likedevelopmentRegion = English;
. Change this to reference the same language you put in yourInfo.plist
file. - 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.
- 首先添加要作为基本语言的语言。取消选中Xcode 为您提供本地化的所有文件。
- 在
Info.plist
(如您所见)中,将开发区域更改为您希望作为基本语言的语言。请注意,该属性的名称有点错误,因为它的值应该是语言代码(带有可选的国家/地区代码),而不是地区或国家/地区代码。 - 在 Xcode 中关闭您的项目。在另一个代码编辑器中,打开
projectname.xcodeproj/project.pbxproj
并搜索developmentRegion
. 你应该看到像developmentRegion = English;
. 将此更改为引用您放入Info.plist
文件中的相同语言。 - 在 Xcode 中重新打开项目。浏览所有可本地化的文件并选中英语旁边的框以生成可本地化的资源。请注意,对于故事板和 xib,Xcode 可能会创建故事板而不是字符串文件。如果发生这种情况,只需将文件类型更改为字符串文件。
Here's an example of the result for me using fr
as the Base language:
这是我fr
用作基本语言的结果示例: