[英]How to extract text from components in Angular 6 for translation (ngx-translate)
I'm trying to find a way to fetch all text strings in my Angular 6 projects files (ts and styles and html files) so that the nested JSON Objects are automagically generated using the files path.我试图找到一种方法来获取我的 Angular 6 项目文件(ts 和样式以及 html 文件)中的所有文本字符串,以便使用文件路径自动生成嵌套的 JSON 对象。 Eg in component html file:
例如在组件 html 文件中:
app/shared/forms/categories/categories.component.html: app/shared/forms/categories/categories.component.html:
<a id="input-categories">This string</a>
<a class="btn accept">This button</a>
after extraction, in en.json:提取后,在 en.json 中:
{
"shared": {
"forms": {
"categories":{
"a-input-categories": "This string",
"a-btn-accept": "This button"
}
}
}
}
I've had a look at ngx-translate-extract , but it will only extract already translatable {{ name.space.here | translate }}
我看过ngx-translate-extract ,但它只会提取已经可翻译的
{{ name.space.here | translate }}
{{ name.space.here | translate }}
pipes etc. {{ name.space.here | translate }}
管道等。
If this is impossible, is there a way to "mark" each string using some default marker such as trans
in this fashion:如果这是不可能的,有没有办法以这种方式使用一些默认标记(例如
trans
“标记”每个字符串:
<a id="input-categories" trans>This string</a>
<a class="btn accept" trans>This button</a>
My problem is that the front end dev has hardcoded everything, and now we are looking at a 4 digit number of strings that is required to be pulled out of the markup and code for translation.我的问题是前端开发人员已经对所有内容进行了硬编码,现在我们正在查看 4 位数的字符串,这些字符串需要从标记和代码中提取出来进行翻译。
you can ngx-translate-extractor你可以 ngx-translate-extractor
https://github.com/biesbjerg/ngx-translate-extract https://github.com/biesbjerg/ngx-translate-extract
ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json
You can use text2locale .您可以使用text2locale 。
npm install -g github:juliandavidmr/text2locale
Extract and generate JSON files:提取并生成 JSON 文件:
text2locale input "./example/src" -o "./example/locale/" -l en -t es,de,zh
A simple solution that might help you is:一个可能对您有帮助的简单解决方案是:
<(.+)( .*)?>(.*[a-zA-Z]+.*)<\/\1>
Write give the following regex to a Replace tool in a text editor, it will catch all tags with text inside them.将以下正则表达式写入文本编辑器中的替换工具,它将捕获其中包含文本的所有标签。
Then replace the first tag's contents, with (for VS Code)然后用(对于VS Code)替换第一个标签的内容
<$1 i18n $2>$3</$1>
or maybe for other editors:或者对于其他编辑器:
<\1 i18n \2>\3<\/\1>
This will convert every <tag attribute="value" etc>stuff</tag>
to <tag i18n attribute="value" etc>stuff</tag>
.这会将每个
<tag attribute="value" etc>stuff</tag>
为<tag i18n attribute="value" etc>stuff</tag>
。
Then you can use ng xi18n
to automatically extract all text for translation to XLIFF.然后您可以使用
ng xi18n
自动提取所有文本以翻译为 XLIFF。
This assumes that your html templates are formatted like:这假设您的 html 模板格式如下:
<tag>
<subtag> text </subtag>
</tag>
Which means only one tag with text per line ( <tag><subtag>
in the same line might create an issue), and that text is in the same line as its enclosing tags.这意味着每行只有一个带有文本的标签(同一行中的
<tag><subtag>
可能会产生问题),并且该文本与其封闭标签在同一行中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.