【发布时间】:2019-10-03 21:05:37
【问题描述】:
是否有任何工具可以扫描 aurelia 项目源(html、js)文件并在translation.json 文件中创建(更新)密钥?
特别是我想从使用TBindingBehavior 和TValueConverter 翻译风格的HTML 文件中收集密钥。
【问题讨论】:
是否有任何工具可以扫描 aurelia 项目源(html、js)文件并在translation.json 文件中创建(更新)密钥?
特别是我想从使用TBindingBehavior 和TValueConverter 翻译风格的HTML 文件中收集密钥。
【问题讨论】:
免责声明:建议的软件包由我的雇主公司开发。
以下是此过程中涉及的主要步骤。
以下是极简的 gulp 任务
const gulp = require("gulp");
const path = require("path");
const updateLocalizationIds = require('gulp-i18n-update-localization-ids');
const i18nExtract = require('gulp-i18n-extract');
const i18nCompile = require('gulp-i18n-compile2');
const src = path.resolve(__dirname, "src"),
json = path.resolve(src, "*.r.json"),
html = path.resolve(src, "*.html"),
translations = path.resolve(__dirname, "translations/i18n.json"),
locales = path.resolve(__dirname, "locales"),
i18nGlobalPrefixes = new Map();
const generateI18nKeys = function () {
return gulp.src(html)
.pipe(updateLocalizationIds({
emit: 'onChangeOnly',
ignore: [{ content: v => v.startsWith('${') && v.endsWith('}') }],
idTemplate: updateLocalizationIds.prefixFilename(i18nGlobalPrefixes),
whitelist: [
{ tagName: 'h2' },
{
tagName: 'another-custom-el',
attrs: ['some-other-value1', 'some-other-value2']
}
]
}))
.pipe(gulp.dest(src));
}
const i18nExtractOptions = {
plugIns: [
new i18nExtract.html(),
new i18nExtract.json()
],
markUpdates: true,
defaultLanguages: ['de', "fr"] // add more language here as per your need
};
const extractI18n = function () {
return gulp.src([html, json])
.pipe(i18nExtract.extract(translations, i18nExtractOptions))
.pipe(gulp.dest("."));
}
const compileOptions = {
fileName: "translation.json",
defaultLanguage: "en"
};
const compileI18n = function () {
return gulp.src(translations)
.pipe(i18nCompile(compileOptions))
.pipe(gulp.dest(locales));
}
gulp.task("i18n", gulp.series(generateI18nKeys, extractI18n, compileI18n));
假设您拥有src 目录下的所有html 文件。您还可以在src 下拥有一些普通的 json 文件作为外部资源。虽然不是真的需要,但在此示例中,我为此使用了扩展名 *.r.json(r 表示资源)。
第一个任务generateI18nKeys 为 html 模板生成 i18n 键。例如,它转换以下edit.html
...
<!--edit.html-->
<h2>some text</h2>
<another-custom-el some-other-value1="value1" some-other-value2="value2"></another-custom-el>
...到下面
<!--edit.html-->
<h2 t="edit.t0">some text</h2>
<another-custom-el some-other-value1="value1" some-other-value2="value2"
t="[some-other-value1]edit.t1;[some-other-value2]edit.t2"></another-custom-el>
在此任务的配置选项中使用whitelist 属性,为密钥生成目标标记元素和属性。
在下一步中,将键和对应的值提取到一个json文件中,如下所示。
{
"edit": {
"content": {
"edit.t0": {
"content": "some text",
"lastModified": "2019-05-26T16:23:42.306Z",
"needsUpdate": true,
"translations": {
"de": {
"content": "",
"lastModified": ""
},
"fr": {
"content": "",
"lastModified": ""
}
}
},
"edit.t1": {
"content": "value1",
"lastModified": "2019-05-26T16:23:42.306Z",
"needsUpdate": true,
"translations": {
"de": {
"content": "",
"lastModified": ""
},
"fr": {
"content": "",
"lastModified": ""
}
}
},
"edit.t2": {
"content": "value2",
"lastModified": "2019-05-26T16:23:42.306Z",
"needsUpdate": true,
"translations": {
"de": {
"content": "",
"lastModified": ""
},
"fr": {
"content": "",
"lastModified": ""
}
}
}
},
"src": "src\\edit.html"
}
}
请注意,将为任务中指定的 localeId 生成空内容。您可以手动更改此文件以添加已配置的每种语言的翻译。
最后,compileI18n 任务从最后一个 json 生成每种语言的文件,如下所示。
{
"edit": {
"t0": "some text",
"t1": "value1",
"t2": "value2"
}
}
请注意,此文件可以直接由 aurelia-i18n 插件使用。有关更多详细信息,请查看包的特定文档。
希望这会有所帮助。
【讨论】: