在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bitjson/gulp-l10n开源软件地址(OpenSource Url):https://github.com/bitjson/gulp-l10n开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-l10nA gulp plugin that wraps s18n and provides simple tooling for localizing html. Perfect for static generated sites and applications. By default, this plugin generates localized html files and outputs them to a subdirectory for each locale, a popular pattern for localizing web content. For example: when Usagevar gulp = require('gulp');
var l10n = require('gulp-l10n');
// Prior to localization, pipe your locales to the setLocales method
gulp.task('load-locales', function () {
return gulp.src('locales/*.json')
.pipe(l10n.setLocales());
});
// Files piped to the plugin are localized and cloned to a separate subdirectory
// for each locale. e.g.: 'index.html' > 'de/index.html'
gulp.task('localize', ['load-locales'], function () {
return gulp.src('src/**/*.html')
.pipe(l10n())
.pipe(gulp.dest('dist'))
});
gulp.task('default', ['localize']); Extracting Locales for TranslationThe Extract method accepts an var gulp = require('gulp');
var l10n = require('gulp-l10n');
var opts = {
elements: ['title', 'p', 'h1'],
attributes: ['alt', 'title'],
directives: 'translate=yes',
attributeSetter: 'translate-attrs'
};
gulp.task('extract-locales', function () {
return gulp.src('src/**/*.html')
.pipe(l10n.extract(opts))
.pipe(gulp.dest('locales'));
}); Continuous LocalizationTo automatically extract new strings for translation, simply add your locale extraction task to your build process. var gulp = require('gulp');
var l10n = require('gulp-l10n');
gulp.task('extract-locales', function () {
return gulp.src('app/**/*.html')
.pipe(l10n.extract())
.pipe(gulp.dest('locales'));
});
gulp.task('load-locales', ['extract-locales'], function () {
return gulp.src('locales/*.json')
.pipe(l10n.setLocales());
});
gulp.task('localize', ['load-locales'], function () {
return gulp.src('app/**/*.html')
.pipe(l10n())
.pipe(gulp.dest('dist'));
});
gulp.task('prepare', ['extract-locales']);
gulp.task('default', ['localize']); Enforcing LocalizationThe enforce option of the ...
gulp.task('load-locales', ['extract-locales'], function () {
return gulp.src('locales/*.json')
.pipe(l10n.setLocales({
native: 'en',
enforce: 'warn'
}));
});
... Example:
{
"37b51d19": "bar",
"acbd18db": "foo"
}
{
"37b51d19": "bår"
} Will warn: WARN: locale `de` is missing: `acbd18db`, native string: `foo` Testing LocalizationTo simulate translation (for testing purposes), you can use the s18n CLI's
Rewriting |
Mode | Description |
---|---|
'silent' |
Do not enforce localization. |
'warn' |
Warn about missing localizations. |
'strict' |
Throw error on missing localizations. |
Gulp plugin to localize html files using locales previously set with the setLocales method.
// inside a gulp task:
var options = {
cacheId: 'test',
hrefRewrite: function(href, locale){
if(href.charAt(0) === '/'){
return '/' + locale + href;
}
else {
return href;
}
}
};
return gulp.src('**/*.html')
.pipe(l10n( options ))
.pipe(gulp.dest('dist'));
The l10n()
options object accepts all s18n localization options, as well as the gulp-l10n
-specific options below.
'default'
Set the locale cache used in localizing html. Allows for multiple distinct websites or applications to be separately translated by the same instance of gulp-l10n.
href
, locale
), null
null
A function to transform all href
s in each html document localized. The value of each href will be replaced by the value returned by this function. Href
s will not be rewritten if this option is set to null
.
Parameter | Description |
---|---|
href |
The original value of the href attribute currently being transformed. |
locale |
The locale code of the locale currently being applied to the html document. This is also the name of the current locale's directory. (Eg: en , de , etc.) |
base
, path
, localeId
), string
return path.replace(base, base + localeId + '/');
A function to compute a custom output path for localized files.
Example for files of the form index.de.html
gulp.task('localize', function () {
return gulp.src('*.html')
.pipe(l10n({
outPath: function (base, path, localeId) {
return path.slice(0, -4) + localeId + ".html";
}
}))
.pipe(gulp.dest(pkg.release));
});
The default Gulp task watches all files and runs tests and code coverage.
$ npm install -g gulp
$ gulp
This module strives to maintain passing tests with 100% coverage in every commit, and tests are run pre-commit. If you prefer, you can always skip this check with git commit --no-verify
and squash WIP commits for pull requests later.
If you're unsure or would like help writing tests or getting to 100% coverage, please don't hesitate to open up a pull request so others can help!
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论