Documentation
¶
Overview ¶
Package localizer renders a CLI's own strings (help, flag descriptions, messages, errors) in the user's language, using translation catalogs that are committed to the repository and embedded in the binary.
Typical integration is one line before executing a Cobra root command:
localizer.Localize(rootCmd, locales.FS)
where locales.FS is an embed.FS holding "<language>.json" catalogs (see the catalog format in the docs). Localization never makes network calls; strings without a translation, and all dynamic data such as server responses, are printed unchanged.
Environment controls:
LOCALIZER_LANG=ja force a language (a list like "ja:en" is allowed) LOCALIZER_LANG=en|off disable localization LOCALIZER_LANG=qps pseudo-localize every known string (QA: shows what flows through Localizer) LOCALIZER_DEBUG=1 report untranslated help strings on stderr LOCALIZER_DUMP=<file> write every help string in the command tree, with hit/miss, as JSON
Index ¶
- Constants
- func Error(err error) string
- func Errorf(format string, args ...any) error
- func Init(catalogs fs.FS, opts ...Option) string
- func Lang() string
- func Localize(root *cobra.Command, catalogs fs.FS, opts ...Option)
- func Sprintf(format string, args ...any) string
- func T(s string) string
- func Writer(w io.Writer) io.Writer
- type Dump
- type DumpEntry
- type Option
Constants ¶
const EnvLang = "LOCALIZER_LANG"
EnvLang is the environment variable that overrides language detection for every Localizer-enabled CLI.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error returns err's message translated for display: CLI-authored parts of a wrapped error chain are translated, server-supplied text is left as is. It returns "" for a nil error.
func Errorf ¶
Errorf is fmt.Errorf with a translated format. %w wrapping is preserved. Prefer translating errors when they are displayed (Error) if code elsewhere inspects error strings.
func Init ¶
Init detects the user's language and loads the matching catalog from catalogs, enabling T, Sprintf, Errorf, Error and Writer. It returns the selected language, or "" when output stays in English. Most Cobra applications call Localize instead, which calls Init. Init can be called again to switch catalogs.
func Lang ¶
func Lang() string
Lang returns the active language tag ("qps" for pseudo-localization), or "" when output is English.
func Localize ¶
Localize translates a Cobra command tree in place: every command's Short, Long, Example and deprecation text, every flag description, command group titles, the usage/help/version templates, Cobra's built-in help and completion commands, and the errors Cobra prints. Call it right before root.Execute(), after the application has added all commands and flags.
The first call detects the user's language (see Init); later calls — for example on a tree rebuilt by an interactive shell — reuse that decision. When output stays in English, Localize changes nothing.
func T ¶
T returns the translation of s, or s unchanged. For a format string, call T before formatting: fmt.Sprintf(localizer.T(format), args...) — or use Sprintf.
func Writer ¶
Writer returns a writer that translates CLI strings written to w, one Write call at a time (so prompts without a trailing newline are passed through immediately). It returns w itself when output is not being localized. The returned writer exposes w's Fd method, if any, for terminal detection.
Types ¶
type Dump ¶
type Dump struct {
Language string `json:"language,omitempty"`
Entries []DumpEntry `json:"entries"`
}
Dump is the LOCALIZER_DUMP file format.
type DumpEntry ¶
type DumpEntry struct {
Kind string `json:"kind"` // short, long, example, deprecated, group, flag, flag_deprecated
Command string `json:"command"`
Flag string `json:"flag,omitempty"`
Text string `json:"text"`
Translated *bool `json:"translated,omitempty"` // nil when no language is active
}
DumpEntry is one help string found in a command tree (LOCALIZER_DUMP output).
type Option ¶
type Option func(*config)
Option configures Localize and Init.
func WithEnvVar ¶
WithEnvVar adds an application-specific override variable (for example "CONFLUENT_LANG") that is consulted before LOCALIZER_LANG and the system locale.
func WithLanguage ¶
WithLanguage forces a language (for example from a --lang flag). "en" or "off" disables localization.
func WithoutErrWriter ¶
func WithoutErrWriter() Option
WithoutErrWriter keeps Localize from wrapping the root command's error writer. Cobra's own error messages are then printed in English.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package catalog reads and writes Localizer translation catalogs: one JSON file per language, named "<BCP 47 tag>.json", mapping each English source string to its translation.
|
Package catalog reads and writes Localizer translation catalogs: one JSON file per language, named "<BCP 47 tag>.json", mapping each English source string to its translation. |
|
Package engine looks up translations for strings a CLI prints: exact catalog hits, reverse-matched format strings, and composite text split into paragraphs, lines and "label: message" parts.
|
Package engine looks up translations for strings a CLI prints: exact catalog hits, reverse-matched format strings, and composite text split into paragraphs, lines and "label: message" parts. |
|
examples
|
|
|
demo
command
Command taskctl is a tiny Cobra CLI that demonstrates Localizer.
|
Command taskctl is a tiny Cobra CLI that demonstrates Localizer. |
|
demo/locales
Package locales holds taskctl's translation catalogs, maintained by Localizer.
|
Package locales holds taskctl's translation catalogs, maintained by Localizer. |
|
internal
|
|
|
builtin
Package builtin embeds Localizer's own translations of the strings Cobra and pflag print (help headings, the help/completion commands, argument and flag errors).
|
Package builtin embeds Localizer's own translations of the strings Cobra and pflag print (help headings, the help/completion commands, argument and flag errors). |
|
locale
Package locale works out which language the user wants: explicit overrides, the POSIX locale environment, then the operating system's preferred languages.
|
Package locale works out which language the user wants: explicit overrides, the POSIX locale environment, then the operating system's preferred languages. |
|
Package msgfmt understands the two kinds of placeholders that appear in CLI strings: Go fmt verbs ("Created %s \"%s\".") and text/template actions ("{{.CommandPath}}").
|
Package msgfmt understands the two kinds of placeholders that appear in CLI strings: Go fmt verbs ("Created %s \"%s\".") and text/template actions ("{{.CommandPath}}"). |