xcode 如何本地化 Cordova iOS 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26324669/
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 localize Cordova iOS projects?
提问by xmarston
I've been searching on the Internet but none of the solutions I found seems to work so, my question is with Xcode 6, how we could localize a Cordova app?
我一直在互联网上搜索,但我找到的解决方案似乎都不起作用,我的问题是使用 Xcode 6,我们如何本地化 Cordova 应用程序?
The image below illustrates the problem, I tested the app in the iOS Simulator (I changed the language settings of the simulator to Spanish) but the context menu in inputs or some plugin like camera are still in English. I changed the "Localization native development region" to "es" but still in English. Also I have Localizable.strings in the es.lprojfolder.
下图说明了这个问题,我在 iOS 模拟器中测试了该应用程序(我将模拟器的语言设置更改为西班牙语),但输入中的上下文菜单或某些插件(如相机)仍然是英文的。我将“本地化本地开发区域”更改为“ es”,但仍然是英文。另外我在es.lproj文件夹中有 Localizable.strings 。
回答by xmarston
Finally I figured out after some digging and with the help of a guy that greatly assisted me in other forum, you have to put this in the project .plist this:
最后,经过一番挖掘,并在一个在其他论坛上为我提供极大帮助的人的帮助下,您必须将其放入项目 .plist 中:
<key>CFBundleLocalizations</key>
<array>
<string>es</string>
</array>
Each string is the language you want to localize.
每个字符串都是您要本地化的语言。
回答by pom421
The preferred answer is correct but has the drawback to be native, i. e. you have to modify the Info*.plist after cordova prepare.
首选答案是正确的,但有一个缺点是原生的,即您必须在cordova 准备之后修改Info*.plist。
If you want to stick with the Cordova's style (which i recommend), you can use a hook or a plugin for this.
如果你想坚持 Cordova 的风格(我推荐),你可以为此使用钩子或插件。
I did it with a plugin because a plugin has (from scratch) the ability to modify the native configuration's files (AndroidManifest.xml or Info*.plist).
我是用插件完成的,因为插件具有(从头开始)修改本机配置文件(AndroidManifest.xml 或 Info*.plist)的能力。
See https://stackoverflow.com/a/27947343/2728710
见https://stackoverflow.com/a/27947343/2728710
What I've done :
我所做的:
- make a new specific plugin name "cordova-plugin-config-ios"
- 创建一个新的特定插件名称“cordova-plugin-config-ios”
localPlugins/cordova-plugin-config-ios/plugin.xml
localPlugins/cordova-plugin-config-ios/plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin id="cordova-plugin-config-ios" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0">
<name>CRM Factory Cordova Localization iOS Plugin</name>
<description>A label translate example</description>
<!-- ios -->
<platform name="ios">
<config-file target="*-Info.plist" parent="CFBundleDevelopmentRegion">
<array>
<string>French</string>
</array>
</config-file>
<config-file target="*-Info.plist" parent="CFBundleLocalizations">
<array>
<string>fr_FR</string>
</array>
</config-file>
</platform>
</plugin>
- make a hook add-local-plugins.sh. In it, install the specific plugin made
- 制作一个钩子 add-local-plugins.sh。在里面,安装特定的插件
add-local-plugins.sh
添加本地插件.sh
echo "Install specific plugin for modify Info*.plist"
cordova plugin add cordova-plugin-config-ios --searchpath ${projectDir}/localPlugins/cordova-plugin-config-ios
- call the hook via config.xml
- 通过 config.xml 调用钩子
config.xml
配置文件
<hook src="hooks/add-local-plugins.sh" type="before_prepare" />
In my case, the hook was not mandatory but I like the freedom brought by it and to be able to log what the program did (echo part).
就我而言,钩子不是强制性的,但我喜欢它带来的自由,并且能够记录程序所做的事情(回声部分)。
回答by Dexter
It is also possible to do this without a hook or plugin by using <edit-config>
in your config.xml
. Here is an example:
也可以在没有钩子或插件的情况下通过<edit-config>
在您的config.xml
. 下面是一个例子:
<platform name="ios">
<edit-config target="CFBundleLocalizations" file="*-Info.plist" mode="overwrite">
<array>
<string>en</string>
<string>es</string>
</array>
</edit-config>
</platform>
Usage of <edit-config>
in config.xml
was introduced in Cordova 6.4.0.
的使用<edit-config>
中config.xml
在引入科尔多瓦6.4.0。
回答by Costas Bakoulias
I post the way that i worked:
我发布了我的工作方式:
- Find in the x-code the folder Resources (is placed in root)
- Select the folder Resources
- Then press the main menu File->New->File...
- Select in section "Resource" Strings File and press Next
- Then in Save As field write InfoPlist ONLY ("I" capital and "P" capital)
- Then press Create
- Then select the file InfoPlist.strings that created in Resources folder and press in the right menu the button "Localize"
- Then you Select the Project from the Project Navigator and select the The project from project list
- In the info tab at the bottom you can as many as language you want (There is in section Localizations)
- The language you can see in Resources Folder
- To localize the values ("key") from info.plist file you can open with a text editor and get all the keys you want to localize
You write any key as the example in any InfoPlist.strings like the above example
"NSLocationAlwaysAndWhenInUseUsageDescription"="blabla"; "NSLocationAlwaysUsageDescription"="blabla2";
- 在 x-code 中找到文件夹 Resources(放在 root 中)
- 选择文件夹资源
- 然后按主菜单 File->New->File...
- 在“资源”字符串文件部分中选择,然后按下一步
- 然后在“另存为”字段中仅写入 InfoPlist(“I”大写和“P”大写)
- 然后按创建
- 然后选择在 Resources 文件夹中创建的文件 InfoPlist.strings 并在右侧菜单中按“Localize”按钮
- 然后从项目导航器中选择项目并从项目列表中选择项目
- 在底部的信息选项卡中,您可以使用任意数量的语言(在本地化部分中有)
- 您可以在资源文件夹中看到的语言
- 要本地化 info.plist 文件中的值(“键”),您可以使用文本编辑器打开并获取要本地化的所有键
您可以在任何 InfoPlist.strings 中编写任何键作为示例,如上例
"NSLocationAlwaysAndWhenInUseUsageDescription"="blabla"; "NSLocationAlwaysUsageDescription"="blabla2";
That's all work and you have localize your info.plist file!
这就是全部工作,您已经本地化了 info.plist 文件!
回答by Maximillion Bartango
Go to the .plist file of any plugin and comment out:
转到任何插件的 .plist 文件并注释掉:
<!-- <key>CFBundleDevelopmentRegion</key>
<string>nl</string> -->
Then the plugin will use the systems set language and region. This is likely the most practical solution for a lot of cases.
然后插件将使用系统设置的语言和区域。对于很多情况,这可能是最实用的解决方案。
回答by hvaughan3
You can do this within the app code itself using cordova-custom-config:
您可以使用cordova-custom-config在应用程序代码本身中执行此操作:
<custom-config-file parent="CFBundleLocalizations" target="*-Info.plist" mode="replace">
<array>
<string>en</string>
</array>
</custom-config-file>