Skip to content

TranslateParser API

This content is for v15. Switch to the latest version for up-to-date documentation.

The TranslateParser is responsible for formatting the translated messages and using parameters that are passed in.

Usually, you would not use the TranslateParser directly. It’s called in the background when a translation string is requested in your app.

It’s also most likely that you don’t have to create your own parser at all.

The default parser

The TranslateParser service has 2 methods which might sometimes be useful:

interpolate

interpolate(expr: string | Function, params?: any): string

Interpolates a string to replace parameters or calls the interpolation function with the parameters.

Example:

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

Returns This is a banana!

Example:

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

Returns This is a banana!

getValue

getValue(target: any, key: string): any

Gets a value from an object by composed key. The . is interpreted as sub-object:

Example:

parser.getValue({ key1: { keyA: 'value' }}, 'key1.keyA')

Returns value.

Custom parser

A TranslateParser implements this interface - you can create your own parser and plug in into the TranslateModule using the config.

export abstract class TranslateParser
{
abstract interpolate(expr: string | Function, params?: any): string | undefined;
abstract getValue(target: any, key: string): any
}
Imprint Privacy