Skip to content

Class: DefaultTranslation

Default implementation of TranslationAdapter.

Implements

  • TranslationAdapter

Signature

ts
declare class DefaultTranslation implements TranslationAdapter

Constructors

Constructor

ts
new DefaultTranslation(options: TranslationAdapterFactoryOptions): DefaultTranslation;

Creates a new instance of DefaultTranslation.

Parameters

NameTypeDescription
optionsTranslationAdapterFactoryOptionsOptions for the translation adapter, see TranslationAdapterFactoryOptions

Returns

DefaultTranslation

Methods

getMessage()

ts
getMessage(locale: string, key: string): string | undefined;

Get a message of locale.

Parameters

NameTypeDescription
localestringA locale of message (BCP 47 language tag)
keystringA key of message resource

Returns

string | undefined — A message of locale. If message not found, return undefined.


getResource()

ts
getResource(locale: string): Record<string, string> | undefined;

Get a resource of locale.

Parameters

NameTypeDescription
localestringA locale of resource (BCP 47 language tag)

Returns

Record<string, string> | undefined — A resource of locale. If resource not found, return undefined.


setResource()

ts
setResource(locale: string, resource: Record<string, string>): void;

Set a resource of locale.

Parameters

NameTypeDescription
localestringA locale of resource (BCP 47 language tag)
resourceRecord<string, string>A resource of locale

Returns

void


translate()

ts
translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;

Translate a message.

Parameters

NameTypeDescription
localestringA locale of message (BCP 47 language tag)
keystringA key of message resource
valuesRecord<string, unknown>A values to interpolate in the message (optional)

Returns

string | undefined — A translated message, if message is not translated, return undefined.

Released under the MIT License.