Skip to content

Testing and QA

Localization follows the user’s environment, so a developer with a Japanese desktop would otherwise get Japanese output in your tests. Set LOCALIZER_LANG=en in the environment of the process under test:

cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(), "LOCALIZER_LANG=en")

Tests that build the command tree without going through your main aren’t affected, because Localize is never called there.

Terminal window
LANG=ja_JP.UTF-8 yourcli --help # the way your users select a language
LOCALIZER_LANG=de yourcli --help # force one, whatever the system settings are

Pseudo-localization rewrites every string Localizer knows about, so anything left in plain English doesn’t go through Localizer yet:

Terminal window
$ LOCALIZER_LANG=qps taskctl --help
⟦ţášķçţļ ķééþš á šɱáļļ ļîšţ öƒ ţášķš.
Ûšé îţ ţö áďď, ļîšţ áñď çöɱþļéţé ţášķš.⟧
⟦Ûšáĝé:⟧
taskctl [command]

Two more switches help with coverage:

  • LOCALIZER_DEBUG=1 reports on stderr each help string that has no translation, as it renders (localizer: untranslated (ja): "Add a task."), and internal failures the runtime swallowed: a catalog that fails to load, a hook that failed, in Go a translation that would break a template. Misses in output and error messages aren’t reported; pseudo-localization shows those. Together with LOCALIZER_DUMP, which looks up every help string in the tree, it reports every help miss at once.

  • LOCALIZER_DUMP=<file> writes every help string in the command tree as JSON, including whether each one was translated. Go writes it when Localize runs, and so does Python for an argparse parser; for Typer and Click, Python writes it the first time help renders, so run --help. Both runtimes create the file new (an existing file is never overwritten) and ignore the variable when the process runs as root or setuid:

    {
    "language": "ja",
    "entries": [
    { "kind": "short", "command": "taskctl add", "text": "Add a task.", "translated": true },
    { "kind": "flag", "command": "taskctl add", "flag": "due", "text": "Due date in YYYY-MM-DD format.", "translated": true }
    ]
    }

    kind is one of short, long, example, deprecated, group, flag or flag_deprecated; Python writes epilog in place of example and has no flag_deprecated.

Translations can be longer than English, and CJK characters take two columns.

Localizer keeps flag columns aligned the same way Cobra does. For your own tables, prefer a library that measures display width (for example one based on go-runewidth) over text/tabwriter, which counts runes.