Skip to main content

Module i18n

Module i18n 

Source
Expand description

Localisation of eov and its plugins.

eov’s interface is available in the LANGUAGES listed here. Every text is written in English, the source language, and translated through gettext catalogs (.po files) that sit beside the code that uses them:

translations/
├── <domain>.pot                      the template: every text, untranslated
├── de/LC_MESSAGES/<domain>.po        German
├── pt-BR/LC_MESSAGES/<domain>.po     Portuguese (Brazil)
└── zh-Hant/LC_MESSAGES/<domain>.po   Chinese (Traditional)

Folders are named by the language’s BCP 47 tag (Language::tag); the spellings other gettext tools use (pt_BR, zh_TW, nb_NO) are found too (Language::catalog_folders). A text that a catalog lacks, or that is marked fuzzy, is shown in English: a missing translation never shows as an empty or raw text.

§Texts in .slint files

Mark them with Slint’s @tr("…"). eov compiles a plugin’s surfaces with the plugin’s catalogs (see the manifest’s [i18n] table), so they follow the language of the interface and change with it.

§Texts made in Rust

Use tr! for every text a user reads: notifications, status texts, menu items, tooltips, errors. It has the syntax of the tr crate’s macro, so its xtr tool extracts the texts, and the placeholders of @tr ({}, {0}, {name}, {n} for the count of a plural text; {{ and }} for braces):

use eov_plugin_api::i18n::tr;
let file = "case-12.svs";
let count = 3;
assert_eq!(tr!("Opened {}", file), "Opened case-12.svs");
let _ = tr!("Toolbar" => "Open"); // with a context
assert_eq!(tr!("One region" | "{n} regions" % count), "3 regions");
let _ = tr!("Saved {count} regions to {file}", count = count, file = file);
// Translators: a comment for translators, extracted with the text.
let _ = tr!("Measure");

eov loads a plugin’s catalog for the language of the interface before it calls Plugin::create, and again before it delivers HostEvent::LanguageChanged, so tr! always answers in the current language. Texts a plugin handed to eov earlier (toolbar tooltips, tool names) keep their language until the plugin hands them over again: do that when the language changes.

Do not translate what is read by programs or other people’s tools: file formats, JSON, CSV, log messages, identifiers, or data such as file names and slide properties.

§The same text for the user and for the log

A problem is often both shown to the user and written to eov’s log, which stays English. Write it once, with tr!, and get it in both languages from shown_and_english; in_english runs any code with this thread’s texts written in English:

use eov_plugin_api::i18n::{self, tr};
let name = "case-12.svs";
let (shown, english) = i18n::shown_and_english(|| tr!("{} could not be read", i18n::isolate(name)));
assert_eq!(english, "case-12.svs could not be read");
// host.info(english); host.notify(NotifyLevel::Warning, shown);

§Writing direction and numbers

Arabic, Persian and Hebrew are written from right to left. Wrap values that are inserted into a translated text, and that are written from left to right whatever the language (file names, paths, numbers with units), with isolate, so the text around them keeps its order. Show decimal numbers with format_decimal and read numbers that users type with parse_decimal; both follow the language’s decimal separator, as Slint does in the interface.

Macros§

tr
Translates a text written in English: see the module documentation. Translates a text written in English, in the current language of this library (see the module documentation):

Structs§

Catalog
The translations of one domain (eov, or a plugin) into one language, read from a gettext .po file.
CatalogError
Why a catalog could not be read.
CatalogReport
What check_catalog found in a catalog.
Language
A language of eov’s interface: one of LANGUAGES. Minor releases may add fields, so code outside this crate reads them and does not build or destructure a Language in full.
PluralRule
A gettext plural rule: the C expression of a Plural-Forms header (n%10==1 && n%100!=11 ? 0 : …) that gives the form a number takes.

Enums§

Direction
The writing direction of a language.

Constants§

SOURCE_TAG
The tag of the source language. Catalogs translate from it; it has none.

Statics§

LANGUAGES
Every language of the interface: English (the source) first, then the translations by tag. Plural forms follow the Unicode CLDR as gettext writes them; decimal separators are the CLDR’s.

Functions§

by_tag
The language with exactly this tag (letter case and _ for - do not matter: pt_br is pt-BR). Use negotiate for locale names such as pt_BR.UTF-8 or zh-TW.
catalog_path
Where the catalog of domain for language belongs: <dir>/<tag>/LC_MESSAGES/<domain>.po.
check_catalog
Checks a catalog of language (the text of its .po file) against what eov needs to show it, and, when given, its template (.pot):
current
The language of the interface (English until eov says otherwise), and English while this thread runs in_english.
empty_catalog
A catalog without translations for language: just the header, with the language’s plural forms. eov puts one in for each language a plugin has no catalog for, so that every bundle lists the same languages.
find_catalog
The catalog of domain for language in dir: in the tag’s folder (pt-BR), else under another spelling gettext tools use (Language::catalog_folders: pt_BR, zh_TW). English has none.
format
Fills the placeholders of a translated text, as Slint’s @tr does: {} takes the next argument, {0} the first, {name} the one with that name; {{ and }} are braces. A placeholder without its argument stays as it is.
format_decimal
value with decimals digits after the decimal separator of the current language (1,50 in German, 1.50 in English).
format_decimal_for
format_decimal for a given language.
in_english
Runs write with this thread’s texts written as in English: tr! gives the English text (filled in, and with the English plural form), and everything that follows current does as for English: isolate adds no marks, format_decimal writes and parse_decimal reads numbers as in English, is_rtl is false; join_sentences joins as it does in every language. For the English line of eov’s log or of a file, from the code that writes the shown text; see shown_and_english for both at once.
is_rtl
Whether the interface is written from right to left.
isolate
text for insertion into a translated text: in a right-to-left language it is isolated (U+2068 … U+2069), so that a file name, a path or a number with its unit keeps its own order and does not reorder the text around it. In left-to-right languages it is returned as it is.
isolate_for
isolate for a given language.
join_sentences
Separately translated sentences as one message, one after the other: with a space between them, except after a sentence that ends in full-width punctuation (。, !, ?, a full-width closing bracket), which Chinese and Japanese follow without a space. Empty sentences are left out.
negotiate
The language of the interface for a locale name of the system or the user: a BCP 47 tag (pt-BR, zh-Hant-TW, es-419) or a POSIX locale (de_AT.UTF-8). None when none of the LANGUAGES fits, and for C and POSIX.
negotiate_first
The first of locales (in order of preference) that negotiate fits.
ntranslate
The form of a text with a number n in the current language (in English inside in_english).
parse_decimal
Reads a number a user typed: with the current language’s decimal separator, or with ., , or the Arabic decimal sign (people type what they are used to), in Western, Arabic-Indic or Persian digits.
parse_decimal_for
parse_decimal for a given language.
placeholders
The placeholders of a text ({}, {0}, {name}; not {{ or }}).
reading_catalog
Whether this thread is reading a .po file. rspolib, the reader Slint uses too, panics on some malformed files; eov catches those panics and reports a bad catalog, and its panic hook asks this so that it does not record them as crashes.
set_catalog
Makes catalog the one tr! translates with, and its language the current one; None shows the English texts. eov and the plugin SDK call this when the language changes.
set_current
Makes language this library’s current language for current, isolate, format_decimal and parse_decimal, without changing the catalog (see set_catalog).
shown_and_english
The text write makes in the interface’s language (to show) and in English (for eov’s log, files, batch output), written by the same code:
source
English, the source language.
translate
The translation of msgid (with the context msgctxt), or msgid (always inside in_english).
translations
The translations: every language but English.