Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ngx translateservice翻译typescript中的方法_Typescript_Ngx Translate - Fatal编程技术网

如何使用ngx translateservice翻译typescript中的方法

如何使用ngx translateservice翻译typescript中的方法,typescript,ngx-translate,Typescript,Ngx Translate,在这个问题中,我试图用所有这些示例来解决我的任务,但我不知道如何将TranslateService连接到我的方法 瑞典语翻译文件se.json(无键) 英文翻译文件en.json(无键) 打字稿: 从“@ngx translate/core”导入{TranslateService}” 我要转换的方法: setColumns(): void { this.loggedInUserType = Meteor.user().profile.user_type; if (this.type ==

在这个问题中,我试图用所有这些示例来解决我的任务,但我不知道如何将TranslateService连接到我的方法

瑞典语翻译文件
se.json
(无键)

英文翻译文件
en.json
(无键)

打字稿:

从“@ngx translate/core”导入{TranslateService}”

我要转换的方法:

setColumns(): void {
  this.loggedInUserType = Meteor.user().profile.user_type;
  if (this.type === "history") {
    this.columns = [
      { header: "NAME" },
      { header: "TOTALSUM" },
      { header: "VAT" },
      { header: "INVOICES" },
    ];
  } else if (this.type === "clients") {
    this.columns = [
      { header: "NAME" },
      { header: "ORGANIZATIONNUMBER" },
      { header: "CITY" },
      { header: "COUNTRY" },
      { header: "ACTIONS" },
   ];
    this.getClients();
  }
}
  • app.module.ts

    导出函数HttpLoaderFactory(httpClient:httpClient){ 返回新的TranslateHttpLoader(httpClient,'assets/i18n/','.json'); }

    导出类TranslateHandler实现MissingTranslationHandler{ 句柄(参数:MissingTranslationHandlerParams){ /*一些逻辑*/ } }

    导出函数appInitializerFactory(translateService:translateService,injector:injector):()=>Promise{ //tslint:禁用下一行:无任何 return()=>新承诺((解析:any)=>{ const locationInitialized=injector.get(LOCATION_INITIALIZED,Promise.resolve(null)); locationInitialized。然后(()=>{ translateService.use(localStorage.getItem(“语言”)| | window.navigator.language)//在这里,您可以在重新加载之前更改加载的语言 .管道(取(1)) .subscribe(()=>{}, err=>console.error(err),()=>resolve(null)); }); }); }

    @NGD模块({ 进口:[ ..., TranslateModule.forRoot({ 加载器:{ 提供:TranslateLoader, 使用工厂:(HttpLoaderFactory), deps:[HttpClient] }, 孤立:假, missingTranslationHandler:[{提供:missingTranslationHandler,useClass:TranslateHandler}] }), ] 供应商:[ ..., { 提供:应用程序初始化器, useFactory:appInitializerFactory, 副总裁:[翻译服务,注入器], 多:真的 } ] }) 导出类AppModule{}

  • 在组件的模块中添加TranslationModule

  • 在组件构造函数中(公共ts:TranslateService){}

  • 现在你可以使用:

    this.ts.instant('YOUR_KEY');//如果未建立翻译,则返回翻译字符串或密钥,但可以在TranslateHandler中更改逻辑

  • 存储在资产/i18n/中的所有翻译。例如en.json

  • {
        NAME: Name
    }
    
    constructor {
     private translate: TranslateService
    } {
        this.translate.use(localStorage.getItem("language")); // Get selected language
    }
    
    setColumns(): void {
      this.loggedInUserType = Meteor.user().profile.user_type;
      if (this.type === "history") {
        this.columns = [
          { header: "NAME" },
          { header: "TOTALSUM" },
          { header: "VAT" },
          { header: "INVOICES" },
        ];
      } else if (this.type === "clients") {
        this.columns = [
          { header: "NAME" },
          { header: "ORGANIZATIONNUMBER" },
          { header: "CITY" },
          { header: "COUNTRY" },
          { header: "ACTIONS" },
       ];
        this.getClients();
      }
    }