Aurelia I18N:扫描html源代码以查找新键并更新translation.json文件

Aurelia I18N:扫描html源代码以查找新键并更新translation.json文件,aurelia,i18next,Aurelia,I18next,是否有任何工具可以扫描aurelia项目源文件(html、js)并在translation.json文件中创建(更新)键 特别是我想从使用TBindingBehavior和TValueConverter翻译风格的HTML文件中收集密钥。免责声明:建议的软件包由我的雇主公司开发 以下是此过程中涉及的主要步骤 使用为html模板生成i18n键 使用将键和值提取到外部资源 手动转换不同语言的值 编译翻译以生成不同语言的语言环境文件,使用 以下是最低限度的吞咽任务 const gulp = requir

是否有任何工具可以扫描aurelia项目源文件(html、js)并在
translation.json
文件中创建(更新)键


特别是我想从使用
TBindingBehavior
TValueConverter
翻译风格的HTML文件中收集密钥。

免责声明:建议的软件包由我的雇主公司开发

以下是此过程中涉及的主要步骤

  • 使用为html模板生成i18n键
  • 使用将键和值提取到外部资源
  • 手动转换不同语言的值
  • 编译翻译以生成不同语言的语言环境文件,使用
  • 以下是最低限度的吞咽任务

    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));
    
    这里发生了什么事? 假设所有html文件都在
    src
    目录下。您还可以在
    src
    下使用一些普通json文件作为外部资源。虽然实际上并不需要它,但在本例中,我使用了扩展名
    *.r.json
    r
    表示资源)

    第一个任务
    generateI18nKeys
    为html模板生成i18n键。例如,它转换以下
    edit.html

    
    一些文本
    
    。。。以下

    
    一些文本
    
    使用此任务配置选项中的
    白名单
    属性标记密钥生成目标的元素和属性

    在下一步中,键和相应的值被提取到一个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
    task
    从最后一个json为每种语言生成文件,如下所示

    {
      "edit": {
        "t0": "some text",
        "t1": "value1",
        "t2": "value2"
      }
    }
    
    请注意,此文件可以直接由aurelia-i18n插件使用。有关更多详细信息,请查看特定于软件包的文档


    希望这能有所帮助。

    免责声明:建议的软件包是由我的雇主公司开发的

    以下是此过程中涉及的主要步骤

  • 使用为html模板生成i18n键
  • 使用将键和值提取到外部资源
  • 手动转换不同语言的值
  • 编译翻译以生成不同语言的语言环境文件,使用
  • 以下是最低限度的吞咽任务

    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));
    
    这里发生了什么事? 假设所有html文件都在
    src
    目录下。您还可以在
    src
    下使用一些普通json文件作为外部资源。虽然实际上并不需要它,但在本例中,我使用了扩展名
    *.r.json
    r
    表示资源)

    第一个任务
    generateI18nKeys
    为html模板生成i18n键。例如,它转换以下
    edit.html

    
    一些文本
    
    。。。以下

    
    一些文本
    
    使用此任务配置选项中的
    白名单
    属性标记密钥生成目标的元素和属性

    在下一步中,键和相应的值被提取到一个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
    task
    从最后一个json为每种语言生成文件,如下所示

    {
      "edit": {
        "t0": "some text",
        "t1": "value1",
        "t2": "value2"
      }
    }
    
    请注意,此文件可以直接由aurelia-i18n插件使用。有关更多详细信息,请查看特定于软件包的文档

    希望这有帮助