简体   繁体   English

更改Ionic2上的语言 - 更改所有页面语言的最有效方法是什么?

[英]Change language on Ionic2 - What is the most effective way to change all page language?

I am trying to use ng2-translate on Ionic2 to do the multi-language, and I added a button for changing the language of the Apps on the setting page . 我试图用NG2,翻译Ionic2做多语言,我增加了一个button用于改变的语言Appssetting page Basically, the function of changing language is work on the setting page only, but the other page will not happen anything. 基本上,更改语言的功能仅适用于设置页面,但其他页面不会发生任何事情。

And I have a stupid way to do the translation, that is add a function ,which change the language of the current page, on every pages and it will be executed when I click the button on the setting page . 我有一个愚蠢的方法来进行翻译,即在每个页面上添加一个改变当前页面语言的功能,当我点击setting page上的按钮时它将被执行。

But this solution less efficiency, how can do the same thing in a effective way? 但是这种解决方案效率较低,怎么能以有效的方式做同样的事情呢?

Here is a part of my code: 这是我的代码的一部分:

  1. Setting Page: 设置页面:

      import { Component, ViewChild } from '@angular/core'; import { NavController, List } from 'ionic-angular'; import {TranslateService, TranslatePipe, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate'; @Component({ selector: 'page-setting', templateUrl: 'setting.html' }) export class SettingPage { translate; @ViewChild(List) list: List; constructor(public navCtrl: NavController, translate: TranslateService) { translate.setDefaultLang('zh'); this.translate = translate; } //When the translation button is clicked click() { this.translate.use('en'); //Change all the pages language location.change('en');`enter code here` home.change('en'); } } 

i have the same, a settings page to change language. 我有相同的,一个更改语言的设置页面。

I have this on html: 我在html上有这个:

<ion-item>
  <ion-label class="primary-text-color">{{ 'Settings.settings.language' | translate }}</ion-label>
  <ion-select [(ngModel)]="language" (ionChange)="onChange($event)">
    <ion-option value="es">Español</ion-option>
    <ion-option value="en">English</ion-option>
  </ion-select>
</ion-item>

Then on my ts file: 然后在我的ts文件上:

import { TranslateService } from 'ng2-translate';

[...]

language: String;

constructor(public translate: TranslateService) {
    this.language = translate.currentLang;
    this.usermail = this.userdata.getEmail();
}

onChange(e) {
    this.translate.use(e);
}

This will change your language in all pages, just use the {{ 'Settings.info.mail' | translate }} 这将改变所有页面中的语言,只需使用{{ 'Settings.info.mail' | translate }} {{ 'Settings.info.mail' | translate }} pipe and it will change the translation when you pick another language with no more changes. {{ 'Settings.info.mail' | translate }}管道,当你选择另一种没有更多变化的语言时,它会改变翻译。

Hope it helps you 希望它能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM