简体   繁体   English

如何本地化 Cordova iOS 项目?

[英]How to localize Cordova iOS projects?

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.下图说明了问题,我在 iOS 模拟器中测试了该应用程序(我将模拟器的语言设置更改为西班牙语),但输入中的上下文菜单或某些插件(如相机)仍然是英文的。 I changed the " Localization native development region " to " es " but still in English.我将“本地化本地开发区域”更改为“ es ”,但仍然是英文。 Also I have Localizable.strings in the es.lproj folder.另外我在es.lproj文件夹中有 Localizable.strings 。

上下文菜单

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.每个字符串都是您要本地化的语言。

It is also possible to do this without a hook or plugin by using <edit-config> in your config.xml .通过在config.xml使用<edit-config>也可以在没有钩子或插件的情况下做到这一点。 Here is an example:下面是一个例子:

<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是在Cordova 6.4.0中引入的。

The preferred answer is correct but has the drawback to be native, ie 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/2728710https://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.制作一个钩子 add-local-plugins.sh。 In it, install the specific plugin made在里面,安装特定的插件

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).就我而言,钩子不是强制性的,但我喜欢它带来的自由,并且能够记录程序所做的事情(回声部分)。

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.对于很多情况,这可能是最实用的解决方案。

How to use iOS device settings for language:如何使用 iOS 设备设置语言:

It seems CFBundleDevelopmentRegion is always set to en_US by default (or maybe this is because I build on a laptop that has these settings) thus showing context menus, file upload dialog etc in English.默认情况下,CFBundleDevelopmentRegion 似乎始终设置为 en_US(或者这可能是因为我在具有这些设置的笔记本电脑上构建)从而以英语显示上下文菜单、文件上传对话框等。

I found that setting CFBundleDevelopmentRegion to empty uses device settings for language.我发现将 CFBundleDevelopmentRegion 设置为空会使用语言的设备设置。

Note that some plugins seem to set language, so if you cannot get it to work, check your plugins.请注意,某些插件似乎会设置语言,因此如果您无法使用它,请检查您的插件。 This was tested in Ionic 5 / Angular / Cordova.这是在 Ionic 5 / Angular / Cordova 中测试的。

Put this in your config.xml ios section:把它放在你的 config.xml ios 部分:

    <platform name="ios">
       <config-file parent="CFBundleDevelopmentRegion" target="*-Info.plist">
            <array>
                <string />
            </array>
        </config-file>
    </platform>

Thanks to @maximillion-bartango answer for putting me on the right track with this感谢@maximillion-bartango 的回答让我走上正轨

I post the way that i worked:我发布了我的工作方式:

  1. Find in the x-code the folder Resources (is placed in root)在 x-code 中找到文件夹 Resources(放在 root 中)
  2. Select the folder Resources选择文件夹资源
  3. Then press the main menu File->New->File...然后按主菜单 File->New->File...
  4. Select in section "Resource" Strings File and press Next在“资源”字符串文件部分中选择,然后按下一步
  5. Then in Save As field write InfoPlist ONLY ("I" capital and "P" capital)然后在“另存为”字段中仅写入 InfoPlist(“I”大写和“P”大写)
  6. Then press Create然后按创建
  7. Then select the file InfoPlist.strings that created in Resources folder and press in the right menu the button "Localize"然后选择在 Resources 文件夹中创建的文件 InfoPlist.strings 并在右侧菜单中按“Localize”按钮
  8. Then you Select the Project from the Project Navigator and select the The project from project list然后从项目导航器中选择项目并从项目列表中选择项目
  9. In the info tab at the bottom you can as many as language you want (There is in section Localizations)在底部的信息选项卡中,您可以使用任意数量的语言(在本地化部分中有)
  10. The language you can see in Resources Folder您可以在资源文件夹中看到的语言
  11. 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要本地化 info.plist 文件中的值(“键”),您可以使用文本编辑器打开并获取要本地化的所有键
  12. You write any key as the example in any InfoPlist.strings like the above example您可以在任何 InfoPlist.strings 中编写任何键作为示例,如上例

    "NSLocationAlwaysAndWhenInUseUsageDescription"="blabla"; "NSLocationAlwaysUsageDescription"="blabla2";

That's all work and you have localize your info.plist file!这就是全部工作,您已经本地化了 info.plist 文件!

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>

Adding CFBundleLocalizations works, however, you still need to manually add, or drag, the InfoPlist.strings files to the Xcode project to work.添加 CFBundleLocalizations 工作,但是,您仍然需要手动添加或拖动 InfoPlist.strings 文件到 Xcode 项目才能工作。

I found this Cordova plugin completely takes over the process, https://github.com/kelvinhokk/cordova-plugin-localization-strings .我发现这个Cordova 插件完全接管了这个过程, https://github.com/kelvinhokk/cordova-plugin-localization-strings So I can simply run cordova prepare and it is all set.所以我可以简单地运行cordova prepare ,一切cordova prepare

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM