A text that mixes words with data, for the player to translate: the
English source text (a template) and its arguments. It returns a plain
Text value, not a translation; pass it where the player shows
text (see Text) and the player shows it translated with this
plugin's catalog. vestra-plugin i18n extract collects source from
every call, so write it as a literal (or a conditional of literals, or
an as const table entry).
Arguments are data unless they are texts themselves, so a sentence can
be built from translated parts without composing it in TypeScript.
text does not use this: const { text, list } = plugin.i18n works.
English source text; {}, {0} or {name} for the
arguments, {{ and }} for braces.
Optionalargs: TemplateArgs
Values for its placeholders.
The drop-down lists "No Audio" and one "Track 7 · en" entry per track,
in the user's language. A [string] model may hold text() values: the
player translates each source text with the plugin's catalog and fills
in its arguments, so the plugin never composes translated strings.
const { text } = plugin.i18n;
const picker = plugin.windows.create("ui/clip.slint");
const tracks = [{ id: 7, title: "en" }];
// Each entry is translated, then filled in: "Track 7 · en".
picker.set({
labels: [text("No Audio"), ...tracks.map((t) => text("Track {} · {}", [t.id, t.title]))],
});
The OSD says "Download failed: Connection timed out", both parts in the
user's language. An argument made with text() is translated too,
while string arguments are shown as they are.
await plugin.osd.show("Download failed: {}", [plugin.i18n.text("Connection timed out")]);
Items joined the way the interface language joins them (see
ListStyle): list(["a.glsl", "b.glsl"]) shows "a.glsl, b.glsl"
in English and "a.glsl、b.glsl" in Chinese. Items are data, or texts the
player translates. Accepted where a Text is.
The OSD says "Removed missing shaders: a.glsl, b.glsl" in English and
joins the names with "、" in Chinese. list makes a value the player
joins per interface language; the names themselves are data and stay
as they are.
const missing = ["a.glsl", "b.glsl"];
await plugin.osd.show("Removed missing shaders: {}", [plugin.i18n.list(missing)]);
An Error whose words the player shows translated: throw it where the
player shows a plugin's failure to the user, such as from a file or web
resolver's resolve (FileResolverOptions,
WebResolverOptions) (also when a stream fails while it starts). The
player shows text translated with this plugin's catalog (as
player.state.error and the failure message); message is the English
rendering, for logs. Elsewhere it is an ordinary Error.
English source text, as I18n.text's.
Optionalargs: TemplateArgs
Values for its placeholders.
Opening a .ch8 file the plugin cannot load shows "The program file was
not found." in the user's language. The player shows the message of an
error thrown from resolve; i18n.error makes an Error whose text the
player translates with this plugin's catalog, while its message stays
English for logs.
plugin.resolvers.add({
id: "chip8",
extensions: ["ch8"],
async resolve(path, signal) {
const program = await readFile(path, { signal }).catch(() => {
// Shown as the open's failure, translated (locales/<lang>.po).
throw plugin.i18n.error("The program file was not found.");
});
return startEmulator(program);
},
});
The string a text shows in this run's language, for text that leaves the player: what the plugin copies to the clipboard or writes to a file ("Copy info", "Save as…"). Translated with this plugin's catalog (a string as a whole, a Text or TextList with its arguments), the way the player would show it.
Only for text leaving the player. Never use it for the interface (a
window, the menu, the OSD, a dialog): pass the value itself there, so
the player translates it and vestra-plugin i18n check can see the
sentence is whole.
Source text, or a text value.
The translated string.
"Copy info" puts two translated lines on the clipboard. Text that
leaves the player cannot be translated by it, so resolve turns the
values into the string the player would show, using this plugin's
catalog; inside the player, pass the values themselves instead.
const { text, list, resolve } = plugin.i18n;
const lines = list([text("Version {}", ["1.0"]), text("Cores: {}", [8])], "lines");
await plugin.clipboard.writeText(resolve(lines));
ReadonlylocaleThe interface language of this run, chosen at start-up from the saved
Appearance.language or the system's languages; English when
neither names a Locale. Players before 0.4.3 report "en".
The interface language of this run.
plugin.i18n.Remarks
Plugins write no translation code. Every user-visible text is English source text in one of three places, and the player translates it with the plugin's own catalog (
locales/<lang>.po, compiled into the package byvestra-plugin build):name,punchline,descriptionandtags;@tr("…")in the plugin's.slintfiles;osd.show("Speed {}×", [rate])), never a sentence composed in TypeScript.Where words and data cannot be separated that way (a menu item with a number, a
[string]model, a sentence made of translated parts), the plugin passes a text value (I18n.text, I18n.list): data the player translates, never a translated string.The language is fixed when the player starts: I18n.locale never changes during a run. Read it for what is not text, such as the order of names or a date format.
vestra-plugin i18n extractcollects the texts intolocales/messages.pot; see the i18n guide.Example: Format a date the way the interface language writes it
The OSD shows today's date in the user's language and date format. The sentence is a template the player translates; only the data in it, the date, is formatted by the plugin, with
i18n.locale(the language the player runs in, fixed for the whole run).Since
1.5.9