Make your files for many languages (locales) with it. You can use YAML or JSON dictionaries format. Also you can use it without translation.
First, install gulp-transformer
as a development dependency:
npm install --save-dev gulp-transformer
Then, add it to your gulpfile.js
:
var translate = require('gulp-transformer');
gulp.task('translate', function() {
var langs = ['en', 'ru', 'de'];
langs.forEach(function(lang){
gulp.src('src/file/path')
.pipe(translate (options))
.pipe(gulp.dest('dest/file/path/' + lang));
});
});
or better, handle errors:
gulp.task('translate', function() {
var langs = ['en', 'ru', 'de'];
langs.forEach(function(lang){
gulp.src('src/file/path')
.pipe(
translate(options)
.on('error', console.error.bind(console, lang))
)
.pipe(gulp.dest('dest/file/path/' + lang));
});
});
options
in translate
function can be:
String
- Path to locale file.Object
:.path
-String
. Optional. Path to locale file. Or you can use.lang
,.localeDirectory
..locale
-String
. Optional. Target language..directory
-String
. Optional. Directory with locale files. Default:__dirname+'/'
..ext
-String
. Optional. Extension of file. Default:''
..pattern
-RegExp
. 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
.transformSeparator
-String
. Optional. Something to split parts of transform. Default:'|'
..transform
-Object
. Optional. Every field is you transform function. First argument is ancontent
to transform it. Function should return transformed string orError
object with some message..keyPartSeparator
-String
. Optional. Some symbol to separate key parts (key likea.b.c
has'.'
as separator). Default:'.'
..strictDictionary
-Boolean
. Optional. If it istrue
will not use default dictionary as fallback if can not find kay in this dictionary. To search some key in default - set it tofalse
. Default:true
..defaultDictionary
-Object
. Optional. Can be aString
or anObject
. IfString
- it should Be path to default locale path. IfObject
contents fields likeoptions
:.path
- likeoptions.path
..locale
- likeoptions.locale
..directory
- likeoptions.directory
..ext
- likeoptions.ext
.
If no .path
specified, try construct it from .directory + .locale + .ext
.
If you specify your own pattern:
- create it with
g
flag (/pattern/g
). Example we want to transform something between'*~'
and'~*'
. Then it should be like/\*~\s?(\w\.\s?|\s?translate[\w\s\|]*)\s?~\*/g
. Also - you can remove translate. But I doesn't know why do you want it. - make some string to check. To test it fast, check,
that
'some string were your *~word|translate~* <-pattern is'.match(pattern)
returns[ 'word|translate' ]
without'*~'
and'~*'
.
Be careful! If you function is named like base transform, you will newer spawn this base transform function, and will use yours.
Specifying user transforms example:
options = {
path: 'some/path/to/locale',
transform: {
upperLowerRandom: function(string){
string.split('').reduce(function(result, letter){
result += Math.floor(Math.random()*2) ? letter.toUpperCase() : letter.toLowerCase();
return result;
}, '');
}
}
}
By default it using angular-like syntax. Expressions in {{}}
with | translate
transformer will be translated.
Following examples assume that "some" in locales equals "My bIg StRing tO trAnsfOrm. Let's Do "
Example:
'{{ some | translate }}'
transforms to "My bIg StRing tO trAnsfOrm. Let's Do "
You can use another base transforms. Or if you want - specify yours.
Base transforms are:
- translate:
'{{ some | translate }}'
transforms to "My bIg StRing tO trAnsfOrm. Let's Do " - uppercase:
'{{ some | translate | uppercase }}'
transforms to "MY BIG STRING TO TRANSFORM. LET'S DO " - lowercase:
'{{ some | translate | lowercase }}'
transforms to "my big string to transform. let's do " - capitalize:
'{{ some | translate | capitalize }}'
transforms to "My big string to transform. let's do " - capitalizeEvery:
'{{ some | translate | capitalizeEvery}}'
transforms to "My Big String To Transform. Let'S Do " - capitalizeDot:
'{{ some | translate | capitalizeDot}}'
transforms to "My big string to transform. Let's do " - trim:
'{{ some | translate | trim}}'
transforms to "My big string to transform. Let's Do" - reverse:
'{{ some | translate | reverse }}'
transforms to " oD s'teL .mrOfsnArt Ot gniRtS gIb yM"
Run tests
npm test
To check code coverage:
- Install istanbul global
sudo npm install -g istanbul
- Run npm script
npm run coverage
- Look into
path/to/project/coverage/lcov-report/index.html
Copyright (c) 2015 Konstantine Petryaev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.