apla / me.apla.cordova.app-preferences

App preferences plugin for cordova
Apache License 2.0
201 stars 210 forks source link

Localization support #104

Open maximilianloy opened 7 years ago

maximilianloy commented 7 years ago

This adds the ability to localise the strings prepended with "_@"

Here is an example:

app-settings.json:

[{
  "title": "_@pref_notif_title",
  "description": "_@pref_notif_summary",
  "type": "radio",
  "key": "threshold",
  "default": "2",
  "items": [{
    "value": "1",
    "title": "_@always"
  }, {
    "value": "2",
    "title": "_@level_2"
  }]
}

Will generate e.g. for Android:

values/apppreferences.xml:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string-array name="apppreferences_threshold">
        <item>@string/always</item>
        <item>@string/level_2</item>
    </string-array>
    <string-array name="apppreferences_thresholdValues">
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

xml/apppreferences.xml:

<?xml version='1.0' encoding='utf-8'?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference android:defaultValue="2" android:entries="@array/apppreferences_threshold" android:entryValues="@array/apppreferences_thresholdValues" android:key="threshold" android:summary="@string/pref_notif_summary" android:title="@string/pref_notif_title" />
</PreferenceScreen>

Translations are stored in Settings.bundle/[locale].lproj/Root.strings and res/values/pref-strings.xml files which are copied (recursively) by the following script (not part of pull request) to the projects platform folders: hooks/after_prepare/020_copy_res.js:

#!/usr/bin/env node
// Copy platform resource folders
'use strict';
var ncp = require('ncp');
ncp('resources/android/res', 'platforms/android/res', function(err) {
  if (err) {
    return console.error(err);
  }
});
ncp('resources/ios/Settings.bundle', 'platforms/ios/Settings.bundle', function(err) {
  if (err) {
    return console.error(err);
  }
});
pke commented 7 years ago

Good one. I'd prefer to have the translations in a simple json and the plugin to write the entries to the platforms appropriate resources files as part of the prepare step.