• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kelvinhokk/cordova-plugin-localization-strings: Cordova Plugin for Localization ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kelvinhokk/cordova-plugin-localization-strings

开源软件地址(OpenSource Url):

https://github.com/kelvinhokk/cordova-plugin-localization-strings

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

Cordova Localization Strings Plugin

This plugin helps you manage string files that you need natively, namely Localizable.strings and InfoPlist.strings on iOS, and strings.xml on Android.

This plugin also lets you localize your app name on both iOS and Android.

How to Use

Install the plugin

$ cordova plugin add cordova-plugin-localization-strings

Modify your project root to have the following structure:

Cordova Project Root
  |
  |__ translations
           |
           |__ app
                |
                |__  en.json
                |__  es.json
                |__  ja.json

A JSON file may look like this (Note: Breaking change from 1.0.0 onwards - new JSON format).

{
    "config_ios": {
        "NSCameraUsageDescription": "Take pictures",
        "CFBundleDisplayName": "Some App Name",
        "CFBundleName": "Some App Name"
    },
    "config_android": {
        "app_name": "Some App Name"
    },
    "app": {
        "HAVE_MAIL_TITLE": "You have mail.",
        "HAVE_MAIL_MSG": "%1$@ has you a message titled \\\"%2$@\\\""
    }
}

By default, the language for the Localizable.strings, InfoPlist.strings or strings.xml is taken from the filename.

For example, if the filename is es.json, the language is hence "es", and the plugin will create "/Resources/es.lproj/Localizable.strings" or "/res/values-es/strings.xml".

Install iOS or Android platform

cordova platform add ios
cordova platform add android

Run the code

cordova prepare ios
cordova prepare android

Platform Specific Localizations

There are some platform specific localizations which differ for Android and iOS, for example for Android:

  • zh-rCN
  • zh-rHK
  • zh-rTW

and for iOS:

  • zh-Hans
  • zh-Hans-CN
  • zh-Hant
  • zh-Hant-TW

In this case, you can add the "locale" in the JSON file to specify the platform localizations as in the following examples.

N.B. The "locale" key is optional (if platform localization is not required).

  • zh-Hans.json
{
    "locale": {
        "ios": ["zh-Hans"],
        "android": ["zh-rCN"]
    },
    "config_ios": {
        "NSCameraUsageDescription": "扫描二维码",
        "CFBundleDisplayName": "应用程序名称",
        "CFBundleName": "应用程序名称"
    },
    "config_android": {
        "app_name": "应用程序名称"
    },
    "app": {
        "HAVE_MAIL_TITLE": "你收到了邮件",
        "HAVE_MAIL_MSG": "%1$@给您发送了封邮件,标题为\\\"%2$@\\\""
    }
}
  • zh-Hant.json
{
    "locale": {
        "ios": ["zh-Hant"],
        "android": ["zh-rTW", "zh-rHK"]
    },
    "config_ios": {
        "NSCameraUsageDescription": "掃描二維碼",
        "CFBundleDisplayName": "應用程序名稱",
        "CFBundleName": "應用程序名稱"
    },
    "config_android": {
        "app_name": "應用程序名稱"
    },
    "app": {
        "HAVE_MAIL_TITLE": "你收到了郵件",
        "HAVE_MAIL_MSG": "%1$@給您發送了封郵件,標題為\\\"%2$@\\\""
    }
}

Dependencies

This plugin works on cordova >= 6.1.0 and node >= 10.

Use Cases

Typically, in a Cordova application, localization is performed on the javascript layer. There are several libraries to help do so like angular-translate on Ionic 1 and ngx-translate on Ionic 2+, with the help of plugins like cordova-plugin-globalization to retrieve the locale or preferred language natively.

This plugin helps for native localization in the following use cases:

Localizing App Name

The plugin will help localize the app name if you require it to be named differently in different languages. Use the following JSON file format.

{
    "config_ios": {
        "CFBundleDisplayName": "Some App Name",
        "CFBundleName": "Some App Name"
    },
    "config_android": {
        "app_name": "Some App Name"
    }
}

Localizing iOS Permissions

This plugin will help localize the iOS permission descriptions, for example NSCameraUsageDescription. A full list of iOS permissions and other InfoPlist strings that can be found here.

Example usage:

{
    "config_ios": {
        "NSCameraUsageDescription": "Take pictures",
        "NSLocationUsageDescription": "Need Location for Some Purpose"
    }
}

iOS Settings localizations

This plugin can localizae iOS Settings bundles.

Example usage:

{
    "settings_ios": {
        "Root": {
            "App version": "App version"
        }
    }
}

In the example shown, it would create a file such as "platforms/ios//Resources/Settings.bundle/.lproj/Root.strings" with the expected localizations for that language.

Push notifications messages

Typically, there are 2 main ways push notifications can be localized:

  • your app saves your user's selected language on the server, and pushes a localized string in the push notification to your user.
  • your server pushes a key to the phone, and the app displays a localized version based on the key in the localization bundle, as determined from the user's phone OS's language.

This plugin helps in the latter approach.

More information about the respective string localizations and formatting here:

Example usage:

{
    "app": {
        "HAVE_MAIL_TITLE": "You have mail.",
        "HAVE_INVITE_MSG": "%1$@ has invited you to game room %2$@"
    }
}

The plugin will automatically generate Localizable.strings file using the following entry on iOS:

"HAVE_MAIL_TITLE" = "You have mail.";
"HAVE_INVITE_MSG" = "%1$@ has invited you to game room %2$@";

And on Android, the respective locale's strings.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resources>
    <string name="HAVE_MAIL_TITLE">You have mail.</string>
    <string name="HAVE_INVITE_MSG">%1$s has invited you to game room %2$s</string>
</resources>

Details for iOS

The plugin reads the assumed directory structure. It then reads from all the fields in "config_ios" and writes into the InfoPlist.strings, which will be placed in the respective locale.lproj directory. The rest of the strings in "app" will be placed in the Localizable.strings file inside the locale directory.

Details for Android

The plugin reads the assumed directory structure. It then combines all properties in "config_android" and "app" to insert them into the strings.xml of the locale's /res/values-<xx>/strings.xml.

Note that for the app's default language the strings are stored in "/res/values/strings.xml" (directory name without -<xx> suffix). This plugin considers 'en' for the default value, matching your en.json file if it exists, but to change this you should define your own value through the defaultlocale attribute on your <widget> in config.xml. For example <widget id="" defaultlocale="es">…</widget>.

Overriding platform specific translations:

{
    "app": {
        "HAVE_MAIL_TITLE": "Sie haben Post. This should be overwritten by platform-specific string.",
        "HAVE_MAIL_MSG": "%1$@ has you a message titled \\\"%2$@\\\""
    },
    "app_ios": {
        "HAVE_MAIL_TITLE": "Sie haben Post in iOS.",
        "Key with Spaces": "Schlüssel mit Leerzeichen"
    },
    "app_android": {
        "HAVE_MAIL_TITLE": "Sie haben Post in Android.",
        "ONLY_ON_ANDROID": "Testmeldung nur unter Android."
    }
}

If "app_ios" or "app_android" are detected, they will be used and override keys from "app".




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lab11/polypoint: Indoor localization based on the DecaWave DW1000.发布时间:2022-08-15
下一篇:
robotics-upo/dll: DLL: Direct Lidar Localization发布时间:2022-08-15
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap