Skip to content

Commit

Permalink
fix: lazily resolve translate service in translate compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Jan 12, 2022
1 parent 892a1ef commit 01d8680
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/app/core/utils/translate/pwa-translate-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Injectable, Injector, isDevMode } from '@angular/core';
import { TranslateCompiler, TranslateService } from '@ngx-translate/core';
import { once } from 'lodash-es';

import { Translations } from './translations.type';

const cache: Record<string, Function> = {};

@Injectable()
export class PWATranslateCompiler implements TranslateCompiler {
constructor(private injector: Injector) {}

private static MAX_COMPILATION_LENGTH = 1000;

/**
Expand Down Expand Up @@ -52,6 +51,13 @@ export class PWATranslateCompiler implements TranslateCompiler {
*/
private static SIMPLE_VARIABLE_REGEX = /\{\{\s*(\w+)\s*\}\}/g;

private translate: () => TranslateService;

constructor(injector: Injector) {
// set cyclic dependency
this.translate = once(() => injector.get(TranslateService));
}

private checkIfCompileNeeded(value: string | Function): boolean {
return (
typeof value === 'string' &&
Expand Down Expand Up @@ -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);
Expand Down

1 comment on commit 01d8680

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure Demo Servers are available:

Please sign in to comment.