Skip to content

TranslateParser API

The TranslateParser is responsible for interpolating translated messages with the parameters you pass in. You usually do not call it directly; it runs in the background each time a translation is requested.

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

export abstract class TranslateParser {
abstract interpolate(
expr: InterpolateFunction | string,
params?: InterpolationParameters,
): string | undefined;
}

The interpolate() method takes either a string or a function expression and optional parameters, and returns the interpolated string.

With string expressions:

parser.interpolate('This is a {{key}}!', { key: 'banana' });
// 'This is a banana!'

With function expressions:

parser.interpolate((params) => `This is a ${params.key}`, { key: 'banana' });
// 'This is a banana!'

If you are not using a compiler, expr is always a string.

The parser is invoked on every translation lookup, so keep it lightweight. If parsing logic is expensive, move it into a compiler that runs once at load time.

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

Factory form (e.g. when the parser needs configuration from inject()):

provideTranslateService({
parser: provideTranslateParser(() => new MyParser({ strict: true })),
})

For a complete example see Write Your Own Parser.

Imprint Privacy