如何在打字稿中使用 i18n 服务进行复数翻译?

问题描述

在我的 Angular 项目的一个组件中,我使用 i18n 函数来国际化我的值(在模态中使用)。

description: this.i18n({
  value: `You are about to buy X articles`,id: 'cartModalDescription'
}),

在这种情况下,我需要将 articles 复数化。 我怎样才能使用 i18n 函数做到这一点?

文档只展示了 html 模板中的示例,但没有考虑使用 ts 文件中的函数

如有任何建议,我们将不胜感激。

解决方法

您可以尝试根据某些条件使用字符串连接,而不是使用 i18n 函数。像这样:

description: `You are about to buy ${this.selectedArticleArray.length} ${this.selectedArticleArray.length > 1 ? "article" : "articles"}`