[英]Translate json arrays with angular ngx-translate
I have a long text to be translated in an Ionic 4 app.我有一个很长的文本要在 Ionic 4 应用程序中翻译。 I am using angular ngx-translate (@ngx-translate v11.0.1).
我正在使用有角度的 ngx-translate (@ngx-translate v11.0.1)。
To improve readability I would like to have the translation in multiples lines instead of one.为了提高可读性,我希望翻译成多行而不是一行。
I have changed my i18n json, from this (en-US.json):我已经从这个 (en-US.json) 更改了我的 i18n json:
"common-questions-content" : "<b>Question 1?</b> Answer 1 <br> <b>Question 2?</b> Answer 2 <b>Question 3?</b> Answer 3",
To this:对此:
"common-questions-content" : [
"<b>Question 1?</b> Answer 1 <br>",
"<b>Question 2?</b> Answer 2 <br>",
"<b>Question 3?</b> Answer 3"
],
Unexpectedly this works!没想到竟然这么管用! But, it puts commas between every value of the array:
但是,它在数组的每个值之间放置逗号:
I load the translation service in my app.component.ts:我在 app.component.ts 中加载翻译服务:
import {TranslateService} from '@ngx-translate/core';
...
private translateService: TranslateService,
...
this.translateService.use('en-US');
Finally I use it in my html page like that:最后我在我的 html 页面中像这样使用它:
{{ 'common_questions' | translate }}
Is it possible to change this behavior and just show all the text without commas?是否可以更改此行为并仅显示不带逗号的所有文本?
I would recommend you to have one single input per statement and without html tags int the translations such as :我建议您为每个语句输入一个输入,并且在翻译中没有 html 标签,例如:
in your en.JSON :在你的 en.JSON 中:
"QUESTION_1":"blabla",
"QUESTION_2":"blabla",
"QUESTION_3":"blabla",
"ANSWER_1":"blabla",
"ANSWER_2":"blabla",
"ANSWER_3":"blabla",
Then in your component, create two class properties of type array like so:然后在您的组件中,创建两个数组类型的类属性,如下所示:
public questions : string[];
pulbic answers : string[];
constructor (private translate: TranslateService) {
translate.get(["QUESTION_1", "QUESTION_2", "QUESTION_3"]).subscribe(
values => {
this.questions = Object.keys(values).map(key => values[key]);
}
);
translate.get(["ANSWER_1", "ANSWER_2", "ANSWER_3"]).subscribe(
values => {
this.answers = Object.keys(values).map(key => values[key]);
}
);
}
Then in your html display, customize, add click events or whatever you need :然后在您的 html 显示中,自定义、添加点击事件或您需要的任何内容:
<ion-grid>
<ion-row>
<ion-col col-6>
<ion-grid>
<ion-row *ngFor="let q of questions"><b>{{q}}</b></ion-row>
</ion-grid>
</ion-col>
<ion-col col-6>
<ion-grid>
<ion-row *ngFor="let a of answers">{{a}}</ion-row>
</ion-grid>
</ion-col>
</ion-row>
</ion-grid>
This is basic html implementation, but you see how much potential is left for you to use.这是基本的 html 实现,但您会看到还有多少潜力可供您使用。 you can define click events, animations, colors, selected items and so on..
您可以定义单击事件、动画、颜色、选定项目等。
A more cleaner solution which:更清洁的解决方案:
is to create a custom pipe:是创建一个自定义管道:
import { Pipe, PipeTransform } from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
@Pipe({
name: 'translateList',
})
export class TranslateListPipe extends TranslatePipe implements PipeTransform {
transform(key: any, length: number): any[] {
return [...Array(length).keys()].map((i) => super.transform(`${key}.${i}`));
}
}
And use it like so in any template with help of NgFor
:并在
NgFor
帮助下像这样在任何模板中使用它:
<div *ngFor="let item of 'TRANSLATION_KEY' | translateList:2">{{ item }}</div>
Where TRANSLATION_KEY
is key that hols an array of 2 strings:其中
TRANSLATION_KEY
是包含 2 个字符串的数组的键:
"TRANSLATION_KEY": ["hello", "world"]
This custom pipe extends ngx-translate
's own TranslatePipe
and uses super.transform(...)
call under the hood, so all translation heavy lifting is still handled by ngx-translate
.这个自定义管道扩展了
ngx-translate
自己的TranslatePipe
并在引擎盖下使用super.transform(...)
调用,因此所有翻译繁重的工作仍然由ngx-translate
处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.