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 TranslateParser service has 2 methods which might sometimes be useful:
interpolate(expr: string | Function, params?: any): stringInterpolates 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(target: any, key: string): anyGets a value from an object by composed key. The . is interpreted as sub-object:
Example:
parser.getValue({ key1: { keyA: 'value' }}, 'key1.keyA')Returns value.
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}