Angular 使用ngx提取器更新提取的翻译文件
我们正在用Visual Studio代码编写一个Angular 6网站。该网站将被翻译成几种语言,但目前我们仍将其保留为瑞典语和英语,默认语言为英语。我们使用Angular 使用ngx提取器更新提取的翻译文件,angular,typescript,internationalization,Angular,Typescript,Internationalization,我们正在用Visual Studio代码编写一个Angular 6网站。该网站将被翻译成几种语言,但目前我们仍将其保留为瑞典语和英语,默认语言为英语。我们使用xi18n命令从HTML文件中提取字符串,并使用i18n polyfill中的ngx提取器命令从typescript文件中提取字符串 我的问题是,我想在编码过程中将翻译作为一个自然步骤,而不是在项目结束时作为事后考虑添加它。我想同时用HTML和typescript字符串更新翻译文件。虽然我可以在xliff的帮助下对HTML文件执行此操作,但
xi18n
命令从HTML文件中提取字符串,并使用i18n polyfill
中的ngx提取器
命令从typescript文件中提取字符串
我的问题是,我想在编码过程中将翻译作为一个自然步骤,而不是在项目结束时作为事后考虑添加它。我想同时用HTML和typescript字符串更新翻译文件。虽然我可以在xliff
的帮助下对HTML
文件执行此操作,但在下次运行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运行i18n extract
,我会得到三个文件:messages.xlf、messages.en.xlf、messages.sv.xlf
。如果我随后执行npm run ngx extract
,则这三个文件将使用末尾的typescript字符串进行更新。当我执行npm之后再次运行i18n extract
时,所有的typescript字符串都会从messages*.xlf中删除,因为i18n
和xliffmerge
认为真正的typescript字符串是从HTML
代码中删除的HTML字符串。我现在的解决方法是将typescript字符串提取到另一个文件(messages.ts.xlf
)中,但这只生成通用文件。我将代码复制到messages\u ts.en.xlf
和messages\u ts.sv.xlf
并将
标记添加到所有
标记中,然后将其复制到messages.en.xlf
和messages.sv.xlf
文件中。这真的很乏味
问题:如何从HTML
和typescript
文件中连续提取字符串进行翻译?有没有办法先运行xi18n
和ngx提取器
命令,然后将它们与xliff
合并?最好在一个命令行上,我可以添加到packages.json
中的scripts
部分,以减少遗漏任何步骤的风险
我只是在i18n食谱或polyfill自述中找不到任何有帮助的东西。这是我在堆栈溢出上发现的:
不讨论xliff
命令,但我已经了解了那篇文章中的所有内容
提供有关ngx提取器的极好帮助,但不包括如何连续合并多种语言。您可以使用它附带的工具
xliffmerge
我们正在使用以下命令:
# 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"
]
}
}
你可以在行动中看到它你解决过这个问题吗?