【问题标题】:Using ngx-translate to display images使用 ngx-translate 显示图片
【发布时间】:2021-12-13 09:48:23
【问题描述】:

我想在我的 Angular 应用程序中使用 ngx-translate 来显示特定语言的图像。在我的例子中,它是一个徽标,根据使用的语言以不同的语言和样式显示。

我已经试过了

        <img style="width: 100px" [attr.src]="'branding.logo' | translate" alt="" class="src" />

branding.logo 指的是网络中特定徽标的 URL。

但 Chrome 显示,它无法在以下位置找到请求的资源:“https://localhost:8100/branding.logo”。

我做错了什么?

【问题讨论】:

    标签: javascript angular typescript ionic-framework ngx-translate


    【解决方案1】:

    [attr.src]="'branding.logo' | translate" 导致使用文字 branding.logo。如果你想从中获取值,那么它应该是[attr.src]="branding.logo",但它不支持翻译。

    为了实现翻译,您可能希望将其映射为branding.logoMapByLocale,然后使用当前语言环境获取实际值。

    您的代码将如下所示:

    (如何从this question获取语言环境)

    import { LOCALE_ID, Inject } from '@angular/core';
    
    ...
    
    constructor(
      @Inject(LOCALE_ID) public locale: string
    ) {
    
      this.logoMapByLocale = {
        'en': "https://some/path/en.png",
        'fr': "https://some/path/fr.png",
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-26
      • 2018-10-27
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      相关资源
      最近更新 更多