gulp-i18n-leverage
Merge changes in default JSON into localized JSON for i18n-behavior
Project template available at polymer-starter-kit-i18n. On Github Pages (https://t2ym.github.io/polymer-starter-kit-i18n)
Features
- Merge changes in next default JSON from current default JSON into next localized JSON with the help of rfc6902
- Put meta information on merged changes in meta property in JSON for ease of translation
- Output bundles object to merge all the UI texts into JSON per locale
- Export the merged localized JSON as files
Install
npm install --save-dev gulp-i18n-leverage
Workflow
Build tasks from source to dist:
gulp-i18n-preprocess
1. Scan task withgulp-i18n-preprocess
2. Preprocess task withxliff-conv
3. (Optional) Import XLIFF task with4. Leverage task
- Update localized JSON files by merging differences in default JSON from the previous build
- Put them in dist
- Merge all the UI texts into bundles object
fs.writeFileSync()
5. Bundles task with - Generate default bundled JSON file
bundle.json
from the bundles object - Generate per-locale bundled JSON files
bundle.*.json
from the bundles object - Put them in dist
xliff-conv
6. (Optional) Export XLIFF task withgulp-i18n-preprocess
7. Feedback task with- Update default and localized JSON files in source to commit them later by a developer or a build system
Usage
Default options
Sample to show default options:
var gulp = ; var i18nLeverage = ; gulp;
Scan task
i18n-behavior.html directly.
Note: Target HTMLs must importInput:
- Custom element HTMLs in source
Output:
- attributesRepository object in gulpfile.js
var gulp = ; var i18nPreprocess = ; // Global object to store localizable attributes repository var attributesRepository = {}; // Scan HTMLs and construct localizable attributes repository gulp;
Preprocess task
i18n-behavior.html directly.
Note: Target custom element HTMLs must importInput:
- Custom element HTMLs
- Non-custom-element HTMLs in source
Output:
- Preprocessed HTMLs and default JSON files in dist
var gulp = ; var merge = ; var i18nPreprocess = ; // Global object to store localizable attributes repository var attributesRepository; // constructed attributes repository // Other standard pipes such as crisper / minification / uglification are omitted for explanation gulp;
xliff-conv
Import XLIFF task withLeverage task with unbundle to pick up outputs of this task.
Note: This task has to be processed beforeInput:
- Next XLIFF files in source
- Current bundle JSON files in source (as output templates)
Output:
- Overwritten bundle JSON files in source
var gulp = ; var JSONstringify = ; var stripBom = ; var through = ; var XliffConv = ; // Import bundles.{lang}.xlf gulp;
Leverage task
Input:
- Current localized JSON files in source
- Current default JSON files in source
- Next default JSON files in dist
Output:
- Next localized JSON files in dist
- Bundles object in gulpfile.js
var gulp = ; var i18nLeverage = ; var bundles = {}; gulp;
Leverage task with unbundle
feedback task.
Note: If translation is done in locales/bundle.*.json in source for all the elements, contents of per-element json are discarded and replaced with those in the bundle. The per-element json files will be translated inInput:
- Current localized bundle JSON files in source
- Current localized JSON files in source (contents are discarded)
- Current default JSON files in source
- Next default JSON files in dist
Output:
- Next localized JSON files in dist
- Bundles object in gulpfile.js
var gulp = ; var i18nLeverage = ; var through = ; // for unbundle var stripBom = ; // for unbundle var bundles = {}; gulp;
Bundles task
Input:
- Bundles object in gulpfile.js
Output:
- Bundles JSON files in dist
var gulp = ; var fs = ; var JSONstringify = ; var bundles; // constructed bundles gulp;
xliff-conv
Export XLIFF task withtodo
items in JSON files are removed, the corresponding trans-unit
s are treated as approved="yes"
and state="translated"
.
Note: If the Input:
- Next bundles object in gulpfile.js
Output:
- bundle.{lang}.xlf XLIFF in DEST_DIR/xliff
var gulp = ; var through = ; var XliffConv = ; var bundles; // bundles object generated by preprocess and leverage tasks // Generate bundles.{lang}.xlf gulp;
Feedback task
i18n-behavior.html directly.
Note: Target custom element HTMLs must importInput:
- Next localized JSON files in dist
- Next localized XLIFF files in dist
- Custom element HTMLs
- Non-custom-element HTMLs
Output:
- Overwritten localized JSON files in source
- Overwritten default JSON files in source
- Overwritten bundle JSON files in source if translation is done in bundles
- Overwritten bundle XLIFF files in source if XLIFF import/export are setup
Outputs are ready to commit in the repository
var gulp = require('gulp');
var merge = require('merge-stream');
var i18nPreprocess = require('gulp-i18n-preprocess');
// Only applicable to development builds; Skip it in production builds
gulp.task('feedback', function () {
// Copy from dist
var locales = gulp.src([
'dist/**/locales/*.json',
'dist/**/xliff/bundle.*.xlf', // Add this item if xliff import and export are enabled
//'!dist/locales/bundle.*.json' // Remove this item if translation is done in bundles
])
.pipe(gulp.dest('app'));
// Regenerate default JSON files
var elementDefault = gulp.src([ 'app/elements/**/*.html' ])
.pipe(i18nPreprocess({
replacingText: false,
jsonSpace: 2,
srcPath: 'app',
dropHtml: true,
attributesRepository: attributesRepository
}))
.pipe(gulp.dest('app/elements'));
// Regenerate default JSON files for non-custom-element HTMLs, i.e., i18n-dom-bind
var appDefault = gulp.src([ 'app/**/*.html', '!app/{elements,test}/**/*.html' ])
.pipe(i18nPreprocess({
replacingText: false,
jsonSpace: 2,
srcPath: 'app',
force: true,
dropHtml: true,
attributesRepository: attributesRepository
}))
.pipe(gulp.dest('app'));
return merge(locales, elementDefault, appDefault)
.pipe($.size({title: 'feedback'}));
});
polymer-build
library (highly experimental)
Integrate with Polymer CLI project templates by Notes:
- As of
polymer-build 0.4.0
,polymer-build
library is pre-release and subject to change. - As of
Polymer CLI 0.13.0
, the private APIuserTransformers
is deprecated and no longer available.
package.json
and the dependent packages of the following gulpfile.js
Set up npm init # if package.json is missing npm install --save-dev gulp gulp-debug gulp-grep-contents \ gulp-i18n-add-locales gulp-i18n-leverage gulp-i18n-preprocess \ gulp-
Gulp Filters:
- scan - Scan HTMLs and construct localizable attributes repository
- basenameSort - Sort source files according to their base names; Bundle files come first.
- dropDefaultJSON - Drop default JSON files to avoid overwriting new ones
- preprocess - Preprocess Polymer templates for I18N
- tmpJSON - Store extracted JSON in the temporary folder .tmp
- importXliff - Import XLIFF into JSON
- leverage - Merge changes in default JSON into localized JSON
- exportXliff - Generate bundles and export XLIFF
- feedback - Update JSON and XLIFF in sources
- debug - Show the list of processed files including untouched ones
- size - Show the total size of the processed files
Gulp Tasks:
gulp locales --targets="{space separated list of target locales}"
gulp default
- Build withpolymer-build
library forgulp
gulpfile.js: Put it in the root folder of the project
'use strict'; var gulp = ; var gutil = ; var debug = ; var gulpif = ; var gulpignore = ; var gulpmatch = ; var sort = ; var grepContents = ; var size = ; var merge = ; var through = ; var path = ; var stripBom = ; var JSONstringify = ; var i18nPreprocess = ; var i18nLeverage = ; var XliffConv = ; var i18nAddLocales = ; const logging = ; const mergeStream = ; const isPolymerCLI = global_babelPolyfill; // Global object to store localizable attributes repository var attributesRepository = {}; // Bundles object var prevBundles = {}; var bundles = {}; var title = 'I18N transform'; var tmpDir = '.tmp'; var xliffOptions = {}; // Scan HTMLs and construct localizable attributes repository var scan = ; var basenameSort = ; var dropDefaultJSON = ; var preprocess = ; var tmpJSON = ; var unbundleFiles = ; var importXliff = through; var leverage = ; var bundleFiles = ; var exportXliff = through; var feedback = ; var config = // list of target locales to add locales: gutilenvtargets ? gutilenvtargets : // Gulp task to add locales to I18N-ready elements and pages // Usage: gulp locales --targets="{space separated list of target locales}" gulp; if isPolymerCLI moduleexports = transformers: scan basenameSort dropDefaultJSON preprocess tmpJSON importXliff leverage exportXliff feedback ; else const polymer = ; //const optimize = require('polymer-build/lib/optimize').optimize; //const precache = require('polymer-build/lib/sw-precache'); const PolymerProject = polymerPolymerProject; const fork = polymerforkStream; const polymerConfig = ; //logging.setVerbose(); let project = root: process entrypoint: polymerConfigentrypoint shell: polymerConfigshell ; gulp;
API
i18nLeverage(options)
options
object
- jsonSpace: Number, default: 2 - JSON stringification parameter for formatting
- srcPath: String, default: 'app' - Path to source root
- distPath: String, default: 'dist' - Path to dist root to fetch next default JSON files
- finalize: Boolean, default: false - Empty meta information if true
- bundles: Object, default {} - Output bundles object
Quick Tour
polymer-starter-kit-i18n
Quick deployment of npm install -g polymer-cli
npm install -g generator-polymer-init-i18n-starter-kit
mkdir i18n-starter-kit
cd i18n-starter-kit
polymer init i18n-starter-kit
# Add Locales
npm run build locales -- --targets="de es fr ja zh-Hans"
# Build
npm run build
# Translate XLIFF ./xliff/bundle.*.xlf
# Build and Merge Translation
npm run build
# App with Run-time I18N on http://localhost:8080
polymer serve
# App with Build-time I18N on http://localhost:8080
polymer serve build/bundled
Change language
1. Press F12 to open debugger console on the browser
2. Navigate to the elements or DOM tab in the debugger
lang
attribute of html
element from "en" to other locales such as "ja"
3. Change
Update UI strings
1. Change any UI strings in the following HTMLs
polymer-starter-kit-i18n/src/*.html
2. Merge changes into JSON files
cd polymer-starter-kit-i18n
npm run build
3. Check diffs
git diff