Skip to content

TranslateCompiler API

After translations are loaded by the TranslateLoader, they are passed through the TranslateCompiler. The compiler prepares each message for faster lookup at translate time. For example, ngx-translate-messageformat-compiler uses this API to compile ICU MessageFormat strings into interpolation functions.

The matching provider helper is provideTranslateCompiler(). See Configuration → Provider helpers.

export abstract class TranslateCompiler {
abstract compile(value: string, lang: string): InterpolatableTranslation;
abstract compileTranslations(
translations: TranslationObject,
lang: string,
): InterpolatableTranslationObject;
}
  • compile() compiles a single translation value.
  • compileTranslations() iterates over a full translation object, calling compile() on each value.

When a single message is requested, the TranslateParser consumes the compiler’s result. The default parser behaves differently depending on the result type:

  • string → standard {{placeholder}} substitution.
  • InterpolateFunction → called with the interpolation parameters; expected to return a string:
const compiledTranslations = {
hello: (params: any) => `Hello ${params.name}!`,
};

If you ship translations that are already in their final interpolator-ready form (e.g. emitted by a build step), store them with setCompiledTranslation() to bypass the compiler.

app.config.ts
import { provideTranslateService, provideTranslateCompiler } from '@ngx-translate/core';
export const appConfig: ApplicationConfig = {
providers: [
provideTranslateService({
compiler: provideTranslateCompiler(MyCompiler),
}),
],
};

The factory form is also supported:

provideTranslateService({
compiler: provideTranslateCompiler(() => new MyCompiler({ icu: true })),
})

For a step-by-step guide see Custom Translation Compilers.

Imprint Privacy