简体   繁体   English

NGX 翻译 in.Ts 文件 - Angular

[英]NGX Translate in .Ts File - Angular

Currently I have this method in one of my.ts files:目前我在 my.ts 文件之一中有这个方法:

 clearFavourites() {
    if (this.language === 'en') {
      this.dialogs.confirm('Do you want to clear your favourite apps?', 'Clear Favourites', ['Yes', 'No'])
        .then(val => {
          console.log('Dialog dismissed' + val);
          if (val === 1) {
            this.resetFavIcons();
            this.storage.remove('FavAppList');
            this.storage.set('FavHasChanged', 'yes');
          }
        })
        .catch(e =>
          console.log('Error displaying dialog', e)
        );
    } else if (this.language === 'mt') {
      this.dialogs.confirm('Trid tneħħi l-apps tiegħek mill-favoriti?', 'Neħħi minn Favoriti', ['Iva', 'Le'])
        .then(val => {
          console.log('Dialog dismissed' + val);
          if (val === 1) {
            this.resetFavIcons();
            this.storage.remove('FavAppList');
            this.storage.set('FavHasChanged', 'yes');
          }
        })
        .catch(e =>
          console.log('Error displaying dialog', e)
        );
    }
  }
}

I already have ngx trasnlate installed and i am already using the translate pipe in html.我已经安装了 ngx trasnlate,并且我已经在 html 中使用了 translate pipe。 I would like to use the same for this method to remove the if and else for checking the language and just have something similar to:我想对这种方法使用相同的方法来删除 if 和 else 来检查语言,并且只具有类似于以下内容的内容:

clearFavourites() {
    this.dialogs.confirm('SettingsPage.RemoveFav' | translate, 'SettingsPage.ClearFav' | translate, ['SettingsPage.Yes' | translate, 'SettingsPage.No' | translate])
        .then(val => {
          console.log('Dialog dismissed' + val);
          if (val === 1) {
            this.resetFavIcons();
            this.storage.remove('FavAppList');
            this.storage.set('FavHasChanged', 'yes');
          }
        })
        .catch(e =>
          console.log('Error displaying dialog', e)
        );
    }
}

The above method is not working for me, is there another way I can use the ngx translate pipe in a.ts file similar to the method above?上面的方法对我不起作用,有没有另一种方法可以在 a.ts 文件中使用 ngx translate pipe 类似于上面的方法?

Based upon this answer, you can inject the service like following:根据这个答案,您可以注入如下服务:

constructor(private localizationSvc: LocalizationService) {
}

and then you can use the getResource method to get your resources in component:然后您可以使用getResource方法在组件中获取资源:

const translatedText = await this.localizationSvc.getResource('SettingsPage.cancelText', 'en');

Note : This answer is based upon last answer i referenced above.注意:此答案基于我上面引用的最后一个答案。 So make sure to copy code from there.所以一定要从那里复制代码。

So I managed to find an answer to my question.所以我设法找到了我的问题的答案。

I changed my code to:我将代码更改为:

  clearFavourites() {
    this.dialogs.confirm(this.translate.instant('SettingsPage.AskFavs'), this.translate.instant('SettingsPage.ClearFavs'), [this.translate.instant('SettingsPage.Yes'), this.translate.instant('SettingsPage.No')])
      .then(val => {
        console.log('Dialog dismissed' + val);
        if (val === 1) {
          this.resetFavIcons();
          this.storage.remove('FavAppList');
          this.storage.set('FavHasChanged', 'yes');
        }
      })
      .catch(e =>
        console.log('Error displaying dialog', e)
      );
  }
}

and managed to get a dynamic text translation in a ts file.并设法在 ts 文件中获得动态文本翻译。

Hope this helps others as well.希望这对其他人也有帮助。

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

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