i18n: the text an application shows, in its user's language
#import "trame:i18n". Pure Formula, no platform code, any target. Every text the user reads goes through i18n.tr, so an
application is translated without going through its code again. The English text is the key.
The calls
tr is a procedure group: the compiler picks the form from the arguments.
label := i18n.tr("Open in a new tab") // a text
close := i18n.tr("tab strip", "Close") // a translator's context, then the text
text := fmt.tprintf(i18n.tr(count, "%d request", "%d requests"), count) // the form for a count
TITLE := i18n.later("Rounding") // translated later, where it is shown: tr(TITLE)| Call | For |
|---|---|
tr(text) | a plain label or sentence |
tr(context, text) | the same English word with two meanings (tr("tab", "Close") vs tr("dialog", "Close")); the context is for the translator and is never shown |
tr(count, one, other) | a count-dependent form; the caller never writes count == 1, because the choice of form belongs to the language (Polish, Russian and Arabic have several; French reads 0 as singular) |
later(text) | a text kept before the language is known (a setting's title, registered before the settings are read): the English as it is, marked for the tool; the place that shows it calls tr on it |
Write each text whole, with its format verbs: tr("Saved %s"), never tr("Saved") + " " + name (a language puts the name
elsewhere). The texts are literals: the tool reads them from the code. A tr of a variable shows a text marked where it is declared
(i18n.later); a text made at run time is not translated.
The toolkit translates where it shows: a program passes its English (i18n.later) to the toolkit's widgets, never a
translated copy. The settings page calls tr on each setting's title, description, unit, choice labels and group title,
and on the pages' titles; the command palette and the key sheet on each command's label and group; the flowgraph widgets
on the catalog's node titles, port names and parameters' words; a table on its columns' titles. Their searches match the
text shown and the English. A program's data (a file name, a name the user gave) is never passed to tr. A setting's
description that names the program is a format (Def.program fills its %s), formatted where it is shown.
A language
A catalogue is the texts of one language, a file of the repository embedded by #load: the toolkit's is
src/i18n/lang/<language>.txt (its menus, dialogs and settings), a program's is its own (i18n/<language>.txt).
# comments
language fr
ctx tab strip
en Close
fr Fermer l'onglet
en %d request
en+ %d requests
fr %d requête
fr+ %d requêtesA text is a block: ctx (its context, optional), en (the key), en+ (the English plural, for the translator), then the
language's forms: fr, fr+, fr+2... as many as the language's rule has (i18n.plural_rule: CLDR's). A blank line ends
a block. A translation left empty is the English. \n, \t and \\ are escapes.
i18n.load(tag, program) makes the language of tag ("fr", "fr-CA", "fr_FR.UTF-8") from the toolkit's catalogue and the
program's of its primary subtag, a table for each (their domains, below); English, or a language no catalogue has, is nil.
i18n.use(l) puts it in use (nil: English). A program does both through its setting:
PROGRAM_CATALOGUES :: []i18n.Catalog{{"fr", #load("../i18n/fr.txt", string)}}
// after the settings are read, before the first build
prefs_page.language_start(&app.prefs, PROGRAM_CATALOGUES)prefs_page.language_start reads general.language ("system": platform.system_language(), the POSIX locale on Linux,
the user's locale on Windows, read there once), loads the language, logs the lines a catalogue could not read, and puts it in
use. It also watches the setting: a change of it puts the new language in use at once (see below).
Whose catalogue: the domains
Each catalogue is a domain (i18n.Domain: Program, Toolkit). A tr takes its caller's place (#caller_location, a
default argument nobody writes): called from a file under the toolkit's src/ (i18n.domain_of(file),
i18n.toolkit_root()), it looks in the toolkit's catalogue first; called from a program's file, in the program's first.
Each then looks in the other: a program's text that the toolkit's widgets show (a setting's title, i18n.later), a
toolkit's text a program shows.
So a program's catalogue never changes the toolkit's words. A program that translates "Paste" as "Collage" (a page of
its own) does not change the toolkit's field menu, which says "Coller"; "Off" in a program's setting does not change the
toolkit's switches. A text the toolkit shows for the program (i18n.later passed to a widget) takes the toolkit's
translation when the toolkit has the same English: a program that means another thing by it gives it a context
(tr("ctx", text)). The cache of each thread keys an answer by its domain too: the linker may merge the toolkit's literal
"Paste" with the program's.
Typography
A language's typography is set once, when its catalogues are loaded (i18n.typeset), never by hand in an entry. In
French the space a translation has before : ; ? ! % » and after « becomes a no-break space (U+00A0,
i18n.NO_BREAK_SPACE): a line never starts with the sign ("…\n: elle s'affiche"). Only a space the text has changes:
"12:30", "http://" and a format verb ("de %s") stay. The tool that writes the catalogues reads them as written
(load(..., as_written = true)).
A program's name in a French sentence: write it as the subject, or after a colon ("Installation : %s", "%s cherche une nouvelle version"), never after "de", "que", "le" or "la" ("de Inspecteur", "que opencode native"): French elides them before a vowel, and the name is not known when the sentence is written. A sentence never starts with a name either: a name may be written in lower case ("cliche").
Numbers
A number a person reads is written as the language in use writes it, as a text goes through tr. In
i18n/numbers.fm, any thread:
| Call | Gives |
|---|---|
i18n.decimal(value, decimals) | value with that many digits after the decimal mark: decimal(7.84, 1) is "7.8" in English, "7,8" in French |
i18n.percent(share, decimals = 0) | a share (1 is all of it) as a percent: percent(0.34) is "34%" in English, "34 %" in French (a no-break space) |
i18n.decimal_marks(text) | a text a printf made of numbers ("%.1f ms"), each . between two digits made the decimal mark: for a format a program keeps (a slider's) |
i18n.parse_decimal(text) | a number a person typed, with either mark ("7,8" or "7.8"), spaces around it allowed |
i18n.decimal_mark() | the mark itself, '.' or ',' |
Each returns a string in the temporary allocator unless an allocator is given. The mark and the percent's space come from the language's primary subtag (CLDR: French, German, Spanish, Russian... a comma; French and German "34 %"). In English (no language in use) they are "7.8" and "34%".
Write the number into the sentence as a string: the format has a %s where the number goes, the catalogue too.
fmt.tprintf(i18n.tr("median %s ms"), i18n.decimal(p50, 1)) // "médiane 2,2 ms"
fmt.tprintf(i18n.tr("%s of the pixels drawn"), i18n.percent(share))
fmt.tprintf("%s %s", i18n.decimal(rate, 1), i18n.tr("Mbit/s")) // "26,3 Mbit/s": the unit stays with its numberThe toolkit's own numbers use them: ui.number (its text, and what is typed, either mark), ui.slider and
ui.range_slider (their format through decimal_marks), the settings' fields and their "1 to 64" (a decimal one reads
either mark), timefmt.size ("7,8 Go"), the Diagnostics page, the zoom of ui.preview, the percent a progress bar
tells a screen reader.
Changing the language
i18n.use(l) may be called at any time, from any thread: each thread's next tr gives the texts of l (the caches of the
threads are keyed by a generation, which use moves). A language once in use is never freed while the program runs: a
widget may hold a text tr gave, and another thread may be in the middle of a lookup in it. prefs_page.language_start
keeps each language it loaded, so switching back and forth loads each once.
i18n.version() moves at each change of language. The rule for a program:
- Text translated where it is shown (
trat each frame,i18n.laterpassed to the toolkit's widgets: the settings page, the palette, the key sheet, menus, tables) needs nothing: the next frame is in the new language. - What a program made once from translated text (a table of commands with translated labels, a sheet, a list of strings
kept from one frame to the next, a window's title) is made again when
i18n.version()is not the value it was made at. The command palette does it for its sources: it asks each one again wheni18n.version()moves. - A message already shown (a toast, a line of a log) stays as it was said.
What it costs
In English each call returns its argument, the same bytes (raw_data(tr(s)) == raw_data(s)): no lookup, no allocation.
In another language, tr looks a text up by its characters the first time (FNV-1a, a table with open addressing made once when
the language is loaded), then by the address of the literal in a small cache of its thread (512 slots): the interface is immediate, tr
runs for every label at every frame, so it does not hash the text each time. The cache compares the characters too: a text
made at run time at an address a literal had is never taken for it. tr takes no lock and allocates nothing.
The catalogues from the code
i18n <language> [-check] reads every tr call and every i18n.later of the code (not its tests) and writes the
catalogue: the translations already written are kept, a new text is added with its translation empty, a text the code no
longer has is dropped. -check writes nothing and fails when a text is not translated, when the code has a text the
catalogue has not, or the catalogue one the code has not: a project's check runs it.
- The toolkit:
trame i18n frwritessrc/i18n/lang/fr.txtfromsrc/. - A program:
toolbox.i18n_command({dirs = {"src"}, folder = "i18n"})among its tool's commands:apptool i18n fr.