Skip to content

1681-engine / index / Translator

Class: Translator ​

Defined in: src/i18n/Translator.ts:32

Translation with the English source as the key: tr('Open the gate') shows the English until a catalog for the current language has a line for it, so writing a new string never breaks the game, it only shows untranslated.

  • Placeholders are positional: tr('{0} joined', name).
  • The same English with two meanings gets a context after |: tr('Close|window'), tr('Close|near'). The context is a separate key for translators and never shown.
  • Counts: trn('{0} coin', '{0} coins', n) picks a form by Intl.PluralRules; the key is the plural form, and a catalog may give every category its language has ({ one, few, many, other }). {0} is the count.
  • setLanguage switches at once and tells subscribers, so a UI re-renders instead of reloading: read labels when they are shown, never once at import.

tr and trn are bound, so they can be handed around (const { tr } = translator).

Constructors ​

Constructor ​

ts
new Translator(options?): Translator;

Defined in: src/i18n/Translator.ts:41

Parameters ​

ParameterType
optionsTranslatorOptions

Returns ​

Translator

Properties ​

source ​

ts
readonly source: string;

Defined in: src/i18n/Translator.ts:33

Accessors ​

language ​

Get Signature ​

ts
get language(): string;

Defined in: src/i18n/Translator.ts:47

Returns ​

string


languages ​

Get Signature ​

ts
get languages(): string[];

Defined in: src/i18n/Translator.ts:52

Languages with a catalog, plus the source.

Returns ​

string[]

Methods ​

addCatalog() ​

ts
addCatalog(language, catalog): void;

Defined in: src/i18n/Translator.ts:57

Adds (or merges into) a language's catalog; subscribers hear of it when it is the language shown.

Parameters ​

ParameterType
languagestring
catalogCatalog

Returns ​

void


has() ​

ts
has(key): boolean;

Defined in: src/i18n/Translator.ts:76

Whether the current language has its own line for key.

Parameters ​

ParameterType
keystring

Returns ​

boolean


setLanguage() ​

ts
setLanguage(language): void;

Defined in: src/i18n/Translator.ts:63

Parameters ​

ParameterType
languagestring

Returns ​

void


subscribe() ​

ts
subscribe(fn): () => void;

Defined in: src/i18n/Translator.ts:70

Calls fn with the language after every switch; the returned function stops it.

Parameters ​

ParameterType
fn(language) => void

Returns ​

() => void


tr() ​

ts
readonly tr(key, ...args): string;

Defined in: src/i18n/Translator.ts:80

Parameters ​

ParameterType
keystring
...argsunknown[]

Returns ​

string


trn() ​

ts
readonly trn(
   one, 
   other, 
   count, 
   ...args
): string;

Defined in: src/i18n/Translator.ts:89

Parameters ​

ParameterType
onestring
otherstring
countnumber
...argsunknown[]

Returns ​

string

Released under the MIT License.