离子应用程序中无插件的多语言支持 [英] Multi language support without plugin in ionic app

查看:74
本文介绍了离子应用程序中无插件的多语言支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的恒定文件,在其中设置离子应用程序的语言.

Following is my constant file in which I am setting language of ionic app.

@Injectable()
export class Constant {
  public static selectedLang :string = 'EN';

  static setLanguage(languageCode : string){
    Constant.selectedLang = languageCode;

  }
  readonly data: any = {
    "EN": {
      //declared English variables used to be all over the application
             FORGOT_PASSWORD : "Forgot Password",
          }
    "MR": {
             //marathi variables
             FORGOT_PASSWORD : "पासवर्ड विसरलात",
           }
    "HI":{
             //Hindi variables
             FORGOT_PASSWORD : "पासवर्ड भूल गए",
         }
   LANG: any = this.data[Constant.selectedLang];
 }

我通过以下方式在HTML中使用此变量:

I use this variables in HTML in following way:

<div>
   <h2>{{CON.LANG.FORGOT_PASSWORD}}</h2>
   <p>{{CON.LANG.FORGOT_PASSWORD}}</p>
</div>

我在其中设置语言的

ts文件:

ts file where I set language:

import {Constant} from "../../../../constants";
class ForgotPassword {
    constructor(public CON: Constant){}

    setLanguage(languageCode : string){
       Constant.setLanguage(languageCode);
    }
 }

在这里,我想保留英语作为默认语言.当用户通过调用函数显式更改语言时,则仅应更改语言.但是问题是,即使用户调用该函数来更改语言,它也不会更改.应用程序始终显示英语变量.

Here, I want to keep English as my default language. When user explicitly changes the language by calling the function, then only language should be changed. But the issue is Even if user calls the function to change language, its not changed. App is always showing English variables.

我们非常感谢您的帮助.

Your help is appreciated.

推荐答案

我之前做错了什么.我将常量文件的'data'和'LANG'变量设置为正常而非静态.并如下更改setLanguage函数.

I have got what I was doing wrong earlier. I made 'data' and 'LANG' variables of Constant file normal instead of static. And changed the setLanguage function as follows.

 setLanguage(languageCode : string){
    this.selectedLang = languageCode;
    this.LANG = this.data[this.selectedLang]; //line added
  }

另外,我将我的selectedLanguage变量从组件存储到会话存储中,这样,在我关闭并再次打开该应用程序后,语言将不会更改.

Also, I stored my selectedLanguage variable from component to session storage so that, language will not be changed after I close and open the app again.

 setLanguage(languageCode : string){
    this.CON.setLanguage(languageCode);
    this.sessionProvider.setLanguage(languageCode);
  }

这篇关于离子应用程序中无插件的多语言支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆