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 byIntl.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. setLanguageswitches 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 ​
new Translator(options?): Translator;Defined in: src/i18n/Translator.ts:41
Parameters ​
| Parameter | Type |
|---|---|
options | TranslatorOptions |
Returns ​
Translator
Properties ​
source ​
readonly source: string;Defined in: src/i18n/Translator.ts:33
Accessors ​
language ​
Get Signature ​
get language(): string;Defined in: src/i18n/Translator.ts:47
Returns ​
string
languages ​
Get Signature ​
get languages(): string[];Defined in: src/i18n/Translator.ts:52
Languages with a catalog, plus the source.
Returns ​
string[]
Methods ​
addCatalog() ​
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 ​
| Parameter | Type |
|---|---|
language | string |
catalog | Catalog |
Returns ​
void
has() ​
has(key): boolean;Defined in: src/i18n/Translator.ts:76
Whether the current language has its own line for key.
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
boolean
setLanguage() ​
setLanguage(language): void;Defined in: src/i18n/Translator.ts:63
Parameters ​
| Parameter | Type |
|---|---|
language | string |
Returns ​
void
subscribe() ​
subscribe(fn): () => void;Defined in: src/i18n/Translator.ts:70
Calls fn with the language after every switch; the returned function stops it.
Parameters ​
| Parameter | Type |
|---|---|
fn | (language) => void |
Returns ​
() => void
tr() ​
readonly tr(key, ...args): string;Defined in: src/i18n/Translator.ts:80
Parameters ​
| Parameter | Type |
|---|---|
key | string |
...args | unknown[] |
Returns ​
string
trn() ​
readonly trn(
one,
other,
count,
...args
): string;Defined in: src/i18n/Translator.ts:89
Parameters ​
| Parameter | Type |
|---|---|
one | string |
other | string |
count | number |
...args | unknown[] |
Returns ​
string