本文介绍了NGX在.Ts文件中翻译-角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的.ts文件之一中有此方法:

Currently I have this method in one of my .ts files:

 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)
        );
    }
  }
}

我已经安装了ngx trasnlate,并且已经在html中使用转换管道.我想在此方法中使用相同的方法,以删除if和else来检查语言,并且只有类似以下内容:

I already have ngx trasnlate installed and i am already using the translate pipe in html.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:

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)
        );
    }
}

上述方法对我不起作用,是否还有另一种方法可以像上述方法一样在.ts文件中使用ngx转换管道?

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?

推荐答案

所以我设法找到了问题的答案.

So I managed to find an answer to my question.

我将代码更改为:

  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)
      );
  }
}

并设法在ts文件中获得了动态文本翻译.

and managed to get a dynamic text translation in a ts file.

希望这对其他人也有帮助.

Hope this helps others as well.

这篇关于NGX在.Ts文件中翻译-角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:18