Almost like string replace but using locales Now you can use both .json and .yml files.
First, install gulp-translator as a development dependency:
npm install --save-dev gulp-translatorThen, add it to your gulpfile.js:
var translate = require('gulp-translator');
gulp.task('translate', function() {
var translations = ['pl', 'en'];
translations.forEach(function(translation){
gulp.src('app/views/**/*.html')
.pipe(translate(options))
.pipe(gulp.dest('dist/views/' + translation));
});
});or better, handle errors:
gulp.task('translate', function() {
var translations = ['pl', 'en'];
translations.forEach(function(translation){
gulp.src('app/views/**/*.html')
.pipe(
translate(options)
.on('error', function(){
console.error(arguments);
})
)
.pipe(gulp.dest('dist/views/' + translation));
});
});options in translate function is:
StringPath to locale file.Object.localePathString. Optional. Path to locale file. Or you can use.lang,.localeDirectory..langString. Optional. Target language..localeDirectoryString. Optional. Directory with locale files. If no.localePathspecified, try construct it from.localeDirectory + .lang..localeExtString. Optional. If you specify path to file will transformnewLocalePath = oldLocalePath + .localExt..patternRegExp. Optional. Pattern to find strings to replace. You can specify your own pattern. To transform strings without translate. Default:/\{{2}([\w\.\s\"\']+\s?\|\s?translate[\w\s\|]*)\}{2}/g.patternSplitterString. Some to split parts of transform. Default:'|'..transformObject. Every field is you transform function. First argument is ancontentto transform it. Second is an dictionary, that you specified. Function should return transformed string orErrorobject with some message.
I'm using angular-like syntax. Expressions in {{}} with | translate
filter will be translated.
Following examples assume that "title" in locales equals "new TITLE"
Example:
{{ title | translate }} will be change to "new TITLE"
If you'd like to use filters(look at the bottom to check available filters) just pass them after like that:
{{ title | translate | lowercase }} will be change to "new title"
{{ title | translate | uppercase }} will be change to "NEW TITLE"
If you're still not sure, please look at tests.
gulp-translator is called with a string
Type: String
The string is a path to a nameOfTheFile.yml with your locales. Please look at test/locales for examples.
- lowercase
- uppercase
- capitalize to capitalize only first word.
- capitalizeEvery to capitalize every word.
- reverse
You also can specify your own filters.
Just add them to .transform parameter of options.
First argument is an content to transform it.
Second is an dictionary, that you specified.
Function should return transformed string or Error object with some message.
- refactor tests
- work on matchers (sigh...)
MIT