![]() |
Translation of the interface and locales | ![]() |
Declared in
System/Language/dictionary.hpp
. A dictionary translates strings from one language from into another to; it is a resource named from-to, created by load_dictionary (from, to) and kept for the session. Its table maps source strings to translations.
Dictionaries are loaded from the files from-to.scm in the search path $TEXMACS_DIC_PATH, which consists of $TEXMACS_HOME_PATH/langs/natural/dic, $TEXMACS_PATH/langs/natural/dic and the
langs/natural/dic
directories of the plug-ins (
System/Boot/init_texmacs.cpp
). All matching files are loaded, so a plug-in or the user can add entries. Each file is a sequence of pairs
("Bibtex command" "commande Bibtex") |
For some target languages (Chinese, Japanese, Korean, Taiwanese, Russian, Ukrainian, Bulgarian, German, Greek, Slovak) the translations are stored in UTF-8 and converted to Cork when loaded; for the others the files must be in the Cork encoding, which coincides with ISO-8859-1 for most accented letters. Only english-lan.scm files exist; if a dictionary lan-english is requested and no file exists, it is built by inverting english-lan.
dictionary_rep::translate (s, guess) looks for a translation in this order:
the string itself;
the string with its first letter in lower case (the translation then gets an upper case first letter);
the part before a :: marker: a string such as "Ctrl::keyboard" is translated as "Ctrl", but may have its own entry, which allows to disambiguate homonyms;
if guess is set, the string is split: leading and trailing non letters (such as ":", "…" or a number) are translated separately, with a space before :, ! and ? in French, and otherwise the string is split at its last non letter which is not a space;
failing all this, the string is returned unchanged.
The output language is a global variable of
dictionary.cpp
, set with set_output_language and read with get_output_language. At the Scheme level, set-output-language is bound to gui_set_output_language (
Texmacs/Server/tm_server.cpp
), which also refreshes all menus and widgets. It is called when the language preference changes (notify-language in
texmacs/texmacs/tm-server.scm
), whose default is the language of the user's locale (get-locale-language).
The main functions are
Translate a string from English into the output language.
Translate between two given languages.
The same without guessing.
Translate all strings of a tree which are accessible children according to the current DRD, except inside verbatim. Two tags are special: localize translates its argument from English into the output language, whatever the from and to arguments, and (replace pattern arg1 …) translates the pattern without guessing and substitutes the translated arguments for %1, %2, …. The Scheme function replace (
language/natural.scm
) builds and translates such a tree.
Translate the tree and serialize it as a string, as needed for window titles and native menus; keyboard shortcuts (render-key) are rendered in a GUI dependent way.
Menus and widgets call these functions themselves: every label of a menu or widget is a string or tree in English, translated when the widget is built.
Documents use the same dictionaries through the typesetting primitive translate (edit_env_rep::exec_translate,
Typeset/Env/env_exec.cpp
). The macro localize of
std-utils.ts
is defined as <translate|text|english|<value|language>>, so that the automatically generated texts of a document (“Chapter”, “Theorem”, …) follow the document language rather than the language of the interface.
Two Scheme modules help translators:
language/natural.scm
In developer mode, the menu and widget macros of
kernel/gui/gui-markup.scm
record every label in the table all-translations. tr-missing lists the labels seen so far that have no translation, and tr-rebuild rewrites the dictionary file with the missing entries added. The commands are in the translations submenu of the developer menu.
utils/misc/translation-list.scm
A more systematic approach: update-translatable collects all translatable strings from the Scheme sources into
english-new.scm
(minus those in
english-ignore.scm
), update-missing computes, for each language, the list of missing translations in src/TeXmacs/langs/natural/miss/english-lan-miss.scm, and translate-begin / translate-end export these lists for an online translation service and merge the results back.
System/Language/locale.cpp
converts between TeXmacs language names and system locales:
The language of the user, from LC_ALL, LC_MESSAGES, LANG or GDM_LANG, from the system settings on macOS, or from the user interface language on Windows; English by default.
From a locale such as fr_FR.UTF-8 to french, using the first two letters, with special cases for en_GB and zh_TW.
The converse, used to choose spell checking dictionaries, to build the language parameter sent to LanguageTool and to format dates.
The character set of the system: UTF-8 on Windows, macOS, Haiku, Android and with the X11 toolkit, and otherwise the value reported by nl_langinfo.
Dates in a given language and format. With Qt they are computed by Qt (
Plugins/Qt/qt_utilities.cpp
); otherwise date is run with the locale of the language.
The dictionary loader converts from UTF-8 only for a fixed list of target languages (dictionary.cpp:51-55), which does not match the actual encodings of the files: for instance
english-italian.scm
is in UTF-8 but Italian is not in the list, so accented Italian translations are garbled (the translation of “cell properties” comes out with the two UTF-8 bytes of the accented letter instead of one Cork character). A new dictionary must be written in the encoding the loader expects for its language.
Some locale codes in
locale.cpp
are not valid ISO codes: Greek is mapped from gr and to gr_GR (lines 118 and 150; the language code is el), Swedish to sv_SV (line 162; the country is SE) and Esperanto to eo_EO (line 146). As a consequence, Greek users are not recognized from their locale, and spell checking dictionaries and LanguageTool may not be found for these languages.
Without Qt, get_date converts dates for Czech, Hungarian and Polish only if the locale is cz_CZ, hu_HU or pl_PL (
locale.cpp:330
), but Czech is mapped to cs_CZ, so Czech dates are not converted.
The language lists are duplicated: text_language and get_supported_languages (C++), supported-languages (
kernel/texmacs/tm-modes.scm
), the locale tables, the hyphenation files, the dictionaries and the style packages must be kept consistent by hand. For instance american is a text language but not one of the Scheme supported-languages.