github.com/jjonline/i18n-stringer (go): Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.  The above is the copyright information reserved by reference stringer REF: https://github.com/golang/tools/tree/master/cmd/stringer  Copyright 2021 The team jjonline Authors. All rights reserved. Use of this source code is governed by a MIT License license that can be found in the LICENSE file.  i18n-stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer, error interface. Given the name of a (signed or unsigned) integer type T that has constants defined, i18n-stringer will create a new self-contained Go source file implementing  wrapped type I18nTErrorWrap implement method list  As you can see type I18nTErrorWrap also is an typed Error, and can wrap/unwrap your business logic error The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.  fmt.Stringer works best with constants that are consecutive values such as created using iota, but creates good code regardless.  For example, given this snippet,  Create an i18n directory in the same level directory of the source code, create a TOML file use locale name as the file name in the directory, and define the text corresponding to these constants  For example,  Define TOML key-value pairs in the file srcdir/i18n/en.toml  Placebo="en locale Placebo" Aspirin="en locale Aspirin" Ibuprofen="en locale Ibuprofen" Acetaminophen="en locale Acetaminophen"  The above directory tree defines three locale: en, zh_cn AND zh_hk As you can see, the TOML file name in the i18n directory is used as the locale identifier, the subdirectory name under the directory i18n will be used as the locale identifier, and the TOML file name in the subdirectory is no longer restricted The directory name i18n can be overridden with the -tomlpath flag.  running this command  in the same directory will create the file pill_i18n_string.go, in package painkiller, containing a definition of, and a struct I18nPillErrorWrap will also be created  That methods will translate the value of a Pill constant to the string representation of the respective value define in TOML file  Typically this process would be run using go generate, like this:  If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print defined in TOML value of key Paracetamol). NOTE: It is not recommended to use constants of the same value  With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.  The -check flag is used to check missing or useless key-value pairs in TOML files without generating files. Output can be used to help check TOMl files  The -tomlpath flag is used to specify the TOML file storage path. If is omitted, the default value is i18n  The -ctxkey flag is used to specify the key to obtain the current locale from context.Context If is omitted, the default value is i18nLocale  The -defaultlocale flag is used to specify the default language locale. The default language locale will be used when obtaining the translation value for String, Error methods and without or invalid the language locale for Trans, Lang methods If is omitted, the default is the one first of naturally sorted list  The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_i18n_stringer.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.
Keywords: go|golang|i18n|i18n-stringer|toml