[使用ngx提取器更新提取的翻译文件

问题描述 投票:1回答:1

我们正在使用Visual Studio Code在Angular 6中编写网站。该网站将被翻译成多种语言,但目前我们将其保留为瑞典语和英语,并以英语为默认语言。我们使用xi18n命令从HTML文件中提取字符串,并使用ngx-extractori18n-polyfill命令从打字稿文件中提取字符串。

我的问题是,我想在编码过程中自然而然地进行翻译,而不是事后才在项目结束时添加它。我想同时使用HTML和打字稿字符串更新翻译文件。虽然可以在HTML的帮助下对xliff文件执行此操作,但是我下次使用该命令时,会删除typescript字符串。

我在package.json中声明的命令:

"scripts": {
        "i18n-extract": "ng xi18n --output-path=locale --out-file messages.xlf --i18n-locale en && xliffmerge",
        "ngx-extract": "ngx-extractor -i ./src/**/*.ts -f xlf -o ./src/locale/messages.xlf --i18n-locale en && xliffmerge"
    },
    "xliffmergeOptions": {
        "srcDir": "src/locale",
        "genDir": "src/locale",
        "i18nFile": "messages.xlf",
        "i18nBaseFile": "messages",
        "encoding": "UTF-8",
        "defaultLanguage": "en",
        "languages": [
            "sv",
            "en"
        ],
        "removeUnusedIds": true,
        "supportNgxTranslate": false,
        "ngxTranslateExtractionPattern": "@@|ngx-translate",
        "useSourceAsTarget": true,
        "targetPraefix": "",
        "targetSuffix": "",
        "beautifyOutput": false,
        "allowIdChange": false,
        "autotranslate": false,
        "apikey": "",
        "apikeyfile": "",
        "verbose": true,
        "quiet": false
    },

[如果执行npm run i18n-extract,我得到三个文件:messages.xlf, messages.en.xlf, messages.sv.xlf。如果然后执行npm run ngx-extract,三个文件将以打字稿字符串结尾进行更新。此后再次执行npm run i18n-extract时,由于i18nxliffmerge认为真正的打字稿字符串是已从HTML代码中删除的HTML字符串,因此从message * .xlf中删除了所有打字稿字符串。我现在的解决方法是将打字稿字符串提取到另一个文件(messages.ts.xlf),但这只会生成通用文件。我将代码复制到messages_ts.en.xlfmessages_ts.sv.xlf,然后将<target>标签添加到所有<trans-unit>标签,然后将其复制到messages.en.xlfmessages.sv.xlf文件。这真的很乏味。

问题:如何连续从HTMLtypescript文件中提取字符串以进行翻译?有没有一种方法可以先运行xi18nngx-extractor命令,然后将其与xliff合并?最好在一个命令行上,我可以添加到scripts中的packages.json部分,以减少错过任何步骤的风险。

我只是无法在i18n食谱或polyfill自述文件中找到任何有帮助的内容。这是我在堆栈溢出中找到的内容:

[Updates to i18n translation files in Angular废弃xliff命令,但是我已经知道该文章中的所有内容。

[angular-i18n Angular 6 Internationalisation : How to deal with variables提供了有关ngx-extractor的出色帮助,但没有提供如何连续合并几种语言的帮助。

angular typescript internationalization
1个回答
0
投票

您可以使用xliffmerge附带的工具this package

我们正在使用以下命令:

# Generate the xliff files from html
ng xi18n --i18nFormat xlf --output-path i18n --i18n-locale en 

# Update the generated xliff files with translations from typescript
&& ngx-extractor --input src/**/*.ts --format xlf --outFile src/i18n/messages.xlf 

# OPTIONAL: We remove the context as it clutters the file and brings no benefits for us. Have a look to the linked script
&& node src/i18n/remove-context.js src/i18n/messages.xlf 

# Merge the newly generated file with the existing translation files.
&& xliffmerge --profile xliffmerge.json`

我们的xliffmerge配置看起来像这样:

{
  "xliffmergeOptions": {
    "srcDir": "src/i18n",
    "genDir": "src/i18n",
    "defaultLanguage": "en",
    "languages": [
      "en",
      "de"
    ]
  }
}

您可以在操作中看到它here

© www.soinside.com 2019 - 2024. All rights reserved.