beetlebug▾
/

Translations

A program shows its texts in the language of its user with the msgcat package of Tcl. Each text goes through msgcat::mc, and a table of translations for each locale gives the text of the locale. This page covers the commands that a program needs, a full example in English, Spanish, German, French, and Chinese, and the differences between ztcl and Tcl.

msgcat in ztcl

msgcat comes with the capability clock. A program with --allow clock, or a project with allow {clock}, has it, and package require msgcat works as in Tcl:

package require msgcat
namespace import msgcat::mc
msgcat::mcset de "Hello" "Hallo"
msgcat::mcset fr "Hello" "Bonjour"
foreach locale {de fr es} {
    msgcat::mclocale $locale
    puts "$locale: [mc Hello]"
}
de: Hallo
fr: Bonjour
es: Hello

A text with no translation is its own translation, so es gives Hello.

The script of msgcat runs at the first call of one of its commands, so an interpreter or a thread that does not use it holds none of its memory. Until then info procs ::msgcat::* is empty, and info commands ::msgcat::* gives only the exported commands. A command of msgcat that a script defines before the first call, such as msgcat::mcunknown, stays.

Locales and the fallback

A locale is a language and an optional region, in lowercase: de, de_at, zh_cn. msgcat::mclocale gives or sets the locale, and msgcat::mcpreferences gives the list that mc looks in: the locale, each shorter prefix, then the root locale {}.

package require msgcat
namespace import msgcat::mc
msgcat::mcset de "Save" "Speichern"
msgcat::mcset {} "Close" "Close the window"
msgcat::mclocale de_at
puts [msgcat::mcpreferences]
puts [mc Save]
puts [mc Close]
puts [mc Quit]
de_at de {}
Speichern
Close the window
Quit

de_at has no table of its own, so mc Save gives the text of de. Close has a text in the root locale only, and Quit has none.

Arguments

mc passes the words after the text to format, after the translation. A position such as %1$s keeps the arguments in their place when a language puts the words in another order:

package require msgcat
namespace import msgcat::mc
msgcat::mcset de "%1\$s has %2\$d files" "%1\$s hat %2\$d Dateien"
msgcat::mcset zh "%1\$s has %2\$d files" "%2\$d 个文件属于 %1\$s"
foreach locale {en de zh} {
    msgcat::mclocale $locale
    puts [mc "%1\$s has %2\$d files" Ana 3]
}
Ana has 3 files
Ana hat 3 Dateien
3 个文件属于 Ana

Plurals

msgcat has no rules for plurals. A program keeps a key for each form and picks the form by the rule of the language. English and German have one form for 1 and another for each other count, French uses the singular for 0 too, and Chinese has one form:

package require msgcat
namespace import msgcat::mc
msgcat::mcset {} files.one "%d file"
msgcat::mcset {} files.other "%d files"
msgcat::mcset fr files.one "%d fichier"
msgcat::mcset fr files.other "%d fichiers"
msgcat::mcset zh files.other "%d 个文件"

proc form {n} {
    switch -- [lindex [split [msgcat::mclocale] _] 0] {
        fr { expr {$n < 2 ? "one" : "other"} }
        zh { return other }
        default { expr {$n == 1 ? "one" : "other"} }
    }
}
foreach locale {en fr zh} {
    msgcat::mclocale $locale
    set out {}
    foreach n {0 1 2} { lappend out [mc files.[form $n] $n] }
    puts "$locale: [join $out {, }]"
}
en: 0 files, 1 file, 2 files
fr: 0 fichier, 1 fichier, 2 fichiers
zh: 0 个文件, 1 个文件, 2 个文件

Message files

A program keeps its translations in a folder of message files, one for each locale: de.msg, fr.msg, and ROOT.msg for the root locale. Each file is a Tcl script of msgcat::mcset commands, in UTF-8. msgcat::mcload FOLDER reads the file of each locale of the preferences, and a later mclocale reads the files of its new locales.

# msgs/de.msg
msgcat::mcset de "Hello, %1\$s!" "Hallo, %1\$s!"
msgcat::mcset de messages.one "Sie haben %d neue Nachricht."
msgcat::mcset de messages.other "Sie haben %d neue Nachrichten."
# The folder msgs/ next to the folder of this script.
msgcat::mcload [file join [file dirname [file dirname [info script]]] msgs]

The messages of mcset belong to the namespace of the caller, and mc looks in the namespace of its caller first, then in each parent. So a package keeps its texts apart from the texts of the program.

The locale of the user

At its start, msgcat sets the locale from the environment: the first of LC_ALL, LC_MESSAGES, and LANG, with the encoding and the modifier removed, so de_DE.UTF-8 gives de_de. With none of them, the locale is c. A program can also read the locale from an option, and set it with msgcat::mclocale.

Each runner gives msgcat the environment that it has:

Dates

clock format and clock scan have the option -locale, and read the names of the days and the months of the locale from the messages of the namespace ::tcl::clock. ztcl has the names of English only (see the differences), so a program gives the names of its other locales in its message files:

package require msgcat
namespace eval ::tcl::clock {
    ::msgcat::mcset de DAYS_OF_WEEK_FULL {Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag}
    ::msgcat::mcset de MONTHS_FULL {Januar Februar März April Mai Juni Juli August September Oktober November Dezember}
}
set t 1795996800
puts [clock format $t -format "%A, %e. %B %Y" -locale de -gmt 1]
puts [clock format $t -format "%A, %B %e, %Y" -locale en -gmt 1]
Montag, 30. November 2026
Monday, November 30, 2026

The keys are those of Tcl: DAYS_OF_WEEK_ABBREV, DAYS_OF_WEEK_FULL, MONTHS_ABBREV, MONTHS_FULL, AM, PM, and the formats such as DATE_FORMAT and TIME_FORMAT. A locale with a region (de_at) uses the names of its language (de).

A full example

examples/i18n of the ztcl repository is a project of ztcl init: a report in five languages with a greeting, a count of new messages with its plural, and a date.

examples/i18n/
  ztcl.project   kind tool, files {lib msgs}, allow {clock}
  main.tcl       the option --lang, then the report
  lib/report.tcl the report: mcload, the plural rule, the date
  msgs/          ROOT.msg, es.msg, de.msg, fr.msg, and zh.msg
cd examples/i18n
ztcl run --lang es Ana 3 1795996800
ztcl run --lang zh_cn Ana 3 1795996800
¡Hola, Ana!
Tienes 3 mensajes nuevos.
Hoy es lunes, 30 de noviembre de 2026.
Ana,你好!
你有 3 条新消息。
今天是2026年11月30日 星期一。

Under a tclsh, LANG=fr_FR.UTF-8 tclsh main.tcl Ana 1 reads French from the environment, and prints Vous avez 1 nouveau message.

Differences from Tcl

Tcl ztcl
msgcat is a package of the library. msgcat comes with the capability clock. A program with no capability, such as a test of ztcl test, has no msgcat.
clock -locale has the names of many locales, from the message files of the library. clock has the names of English only. A program gives the names of its other locales with mcset in ::tcl::clock.
Tcl 9.1 has msgcat 1.7, with mcn, mcutil, mcpackagenamespaceget, and more options of mcexists and mcpackagelocale. ztcl has msgcat 1.6.1, the version of Tcl 8.6, also with --tcl 9.1.
On Windows, with no variable of the locale, msgcat reads the locale of the user from the registry. The locale is c.