diff --git a/src/app/core/utils/translate/pwa-translate-compiler.ts b/src/app/core/utils/translate/pwa-translate-compiler.ts index 1e1582625c..2620a48a27 100644 --- a/src/app/core/utils/translate/pwa-translate-compiler.ts +++ b/src/app/core/utils/translate/pwa-translate-compiler.ts @@ -1,5 +1,6 @@ import { Injectable, Injector, isDevMode } from '@angular/core'; import { TranslateCompiler, TranslateService } from '@ngx-translate/core'; +import { once } from 'lodash-es'; import { Translations } from './translations.type'; @@ -7,8 +8,6 @@ const cache: Record = {}; @Injectable() export class PWATranslateCompiler implements TranslateCompiler { - constructor(private injector: Injector) {} - private static MAX_COMPILATION_LENGTH = 1000; /** @@ -52,6 +51,13 @@ export class PWATranslateCompiler implements TranslateCompiler { */ private static SIMPLE_VARIABLE_REGEX = /\{\{\s*(\w+)\s*\}\}/g; + private translate: () => TranslateService; + + constructor(injector: Injector) { + // cache TranslateService reference to avoid repeated calls to injector + this.translate = once(() => injector.get(TranslateService)); + } + private checkIfCompileNeeded(value: string | Function): boolean { return ( typeof value === 'string' && @@ -105,7 +111,7 @@ export class PWATranslateCompiler implements TranslateCompiler { if (rename) { args[rename] = args[variable]; } - const delegate = this.injector.get(TranslateService).instant(key, args); + const delegate = this.translate().instant(key, args); const result = `${match[1]}${delegate}${match[4]}`; return this.recurse(result, args); @@ -140,6 +146,9 @@ export class PWATranslateCompiler implements TranslateCompiler { } compileTranslations(translations: Translations): Translations { + // be sure translate dependency is initialized on the first run + this.translate(); + // This implementation is mutable by intention // eslint-disable-next-line guard-for-in for (const key in translations) {