angular - 如何在 ngx-translate 中翻译另一个翻译中的 key

标签 angular ngx-translate

我有一个翻译 JSON 文件,我想在不同翻译的值中翻译另一个翻译。

{
    "COMPANY_NAME": "Apple",
    "WELCOME_TEXT": "{{ COMPANY_NAME }} welcomes you to California!"
}

我看不到如何使用 ngx-translate 执行此操作在 Angular 9 中,谁能给我一个指针?

最佳答案

我设法通过实现自定义 TranslateCompiler 来实现这一点。如下:

我的 app.module.ts :

// ...
imports: [
  TranslateModule.forRoot({
    compiler: { provide: TranslateCompiler, useClass: CustomTranslationCompiler }
  })
]
/// ...

我的 CustomTranslationCompiler.ts :
import { TranslateCompiler } from '@ngx-translate/core';

export class CustomTranslationCompiler implements TranslateCompiler {
  /**
   * This function is needed to implement the interface, but doesn't
   * actually seem to be used anywhere
   *
   * @param value The translation value
   * @param lang The current language
   */
  public compile(value: string, lang: string): string | Function {
    return value;
  }

  /**
   * Look at every translation and pre-translate any nested translation keys within them
   *
   * @param translations All of the translations for the app to be compiled
   * @param lang The current language
   */
  public compileTranslations(translations: any, lang: string): any {
    for (const key in translations) {
      if (translations.hasOwnProperty(key)) {
        translations[key] = this.translateNestedTranslation(translations[key], translations);
      }
    }

    return translations;
  }

  /**
   * Use a regex to search for and replace translations inside translations
   * with their translated value
   *
   * @param value The translation value
   * @param translations All of the translations for the app
   */
  private translateNestedTranslation(value: string, translations: Object): string {
    const searchRegex  = /{{\s([A-Z_:]*)\s?}}/g;
    const replaceRegex = /({{\s?[A-Z_:]*\s?}})/g;

    const matches = searchRegex.exec(value);
    if (matches && matches.length > 0) {
      const searchKey = matches[1];

      if (translations.hasOwnProperty(searchKey)) {
        // Replace the full translate syntax with the translated value
        value = value.replace(replaceRegex, translations[searchKey]);
      } else {
        // If we can't find the value, display only the missing key instead of the full translate syntax
        value = value.replace(replaceRegex, searchKey);
        console.error(`Error: Unable to find translation '${searchKey}'!`)
      }
    }

    return value;
  }
}


一些注意事项:
  • 使用这种方法,翻译值中定义的任何翻译参数都必须是小写的,否则搜索正则表达式
  • 不匹配。
  • 搜索和替换正则表达式不同
  • 我不知道为什么 compile()方法永远不会被调用。我的翻译是作为一个对象到达的,所以也许这就是为什么...
  • 关于angular - 如何在 ngx-translate 中翻译另一个翻译中的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60772119/

    相关文章:

    angular - 无法升级到 Angular 6,范围无效

    json - 使用 angular ngx-translate 翻译 json 数组

    angular - 使用@ngx-translate/core同时翻译不同语言的两个字符串?

    angular - Angular 是否有标签输入组件?

    http - 类型 'Http' 的参数不可分配给 Ionic ngx-translate 中类型 'Http' 的参数

    javascript - 当 Assets 发生变化时更新 service worker

    angular - ng2-translate:无法读取 TranslatePipe.transform 处未定义的属性 'subscribe'

    angular - 为什么 tslint 中不允许使用按位运算符?

    Angular 5 Observable,完成后返回

    javascript - Observables "retryWhen"延迟