简体   繁体   English

使用 angular ngx-translate 翻译 json 数组

[英]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:更清洁的解决方案:

  • avoids creating unhanded subscriptions,避免创建未经处理的订阅,
  • is reusable across different components可在不同组件之间重用

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.

相关问题 找不到 json 语言文件 ngx-translate angular-cli - json language files are not found ngx-translate angular-cli 解析json文件Angular 5 ngx-translate期间Http失败 - Http failure during parsing for json file Angular 5 ngx-translate Angular5-ngx将Pharse值从json转换为lang.json - Angular5 - ngx-translate Pharse value from json to lang.json Ngx-translate:如何使用键引用直接在 Angular 中访问 JSON 数组值 - Ngx-translate: How to access JSON array value directly in Angular with key reference 如果 json 文件之一中缺少密钥,则 ngx-translate 替换为其他 json 文件相同的密钥 - ngx-translate if a key is missing in one of the json file replace with other json file same key JsonReaderException:解析值ngx-translate时遇到意外字符 - JsonReaderException: Unexpected character encountered while parsing value ngx-translate 离子。 如何在字符串上使用 ngx-translate? - Ionic. How to use ngx-translate on string? 如何在ngx-translate中将TranslateHttpLoader与服务URL一起使用 - How to use TranslateHttpLoader with service url in ngx-translate Angular翻译多个json文件 - Angular translate multiple json files 如何加载JSON数组以使用Angular-Translate转换AngularJS网页 - How to load JSON array to translate AngularJS webpage with Angular-Translate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM