【问题标题】:Angular how to use translate i18n key as a function parameter in htmlAngular 如何使用 translate i18n 键作为 html 中的函数参数
【发布时间】:2021-06-24 10:35:25
【问题描述】:

我正在尝试将 i18n 翻译服务密钥作为参数函数传递给 html 组件。

我尝试了以下方法,但得到的却是文本,它得到了密钥

我在 component.ts 中分配了一个带有标题的变量

import { TranslateService } from '@ngx-translate/core';

constructor(public translate: TranslateService,) {

}
public title= this.translate.instant('title-key');

在component.html中。我有这个变量作为参数函数

<a class="nav-link" (click)="functionName(title)" > {{'title-key' | translate}}</a>

应该发送的标题是tasks 而是发送密钥 -> title-key

functionName(tabSelected) {
  switch(tabSelected) {
      case this.title:
        this.tab = true;
      break;
      default:
   }
}

    <kendo-tabstrip #tabstrip [keepTabContent]="true">
        <kendo-tabstrip-tab [title] = "title" *ngIf="tab" [selected]="true">
            <ng-template kendoTabContent *loadOnDemand>
              <app-component></app-component>
            </ng-template>
        </kendo-tabstrip-tab>
   <kendo-tabstrip>

【问题讨论】:

  • 对不起,我没有达到你的目标。你想达到什么目标?请描述工作流程。您只想在 UI 中显示一些翻译吗?
  • @Lynx242 是的,我正在尝试显示所选语言的选项卡。请查看已编辑的问题。

标签: angular internationalization i18next angular-translate


【解决方案1】:

click() 直接应用于&lt;a&gt; 标记总是会遇到问题。试试这个方法:

<a href="javascript: void(0)">
    <i class="nav-link" (click)="function(getTitle())">{{'title-key' | translate}}</i>
</a>

您确实会使用点击功能,而不是锚点的 href-event。

您的组件

public title: string;

constructor(private translate: TranslateService,) { 
}

getTitle(): string {
   return this.translate.instant('title-key');
}

【讨论】:

  • 感谢您的建议。函数名称只是一个示例我现在将编辑问题。但是这不起作用..
  • 它会调用你的函数吗?如果是这样,console.log() 说明了什么?
  • 它打印“title-key”而不是“tasks”
  • 好的,现在我知道你的问题是什么了! ngx-translate 不够快!当组件以翻译后的键启动时,您尝试直接初始化变量。这是服务的早期。尝试不同。我将更新我的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多