Documentation ¶
Overview ¶
Package i18n implements support for translation and localization of Go applications.
Index ¶
- func Sprintf(lang Languager, format string, args ...interface{}) string
- func Sprintfc(lang Languager, ctx string, format string, args ...interface{}) string
- func Sprintfn(lang Languager, singular string, plural string, n int, args ...interface{}) string
- func Sprintfnc(lang Languager, ctx string, singular string, plural string, n int, ...) string
- func T(lang Languager, str string) string
- func Tc(lang Languager, context string, str string) string
- func Tn(lang Languager, singular string, plural string, n int) string
- func Tnc(lang Languager, context string, singular string, plural string, n int) string
- func TranslatedError(err error, languager Languager) error
- type Error
- func Errorf(format string, args ...interface{}) Error
- func Errorfc(ctx string, format string, args ...interface{}) Error
- func Errorfn(singular string, plural string, n int, args ...interface{}) Error
- func Errorfnc(ctx string, singular string, plural string, n int, args ...interface{}) Error
- func FromError(e error) Error
- func NewError(message string) Error
- type Languager
- type String
- type Tabler
- type TranslatableString
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Tc ¶
Tc works like T, but accepts an additional context argument, to allow differentiating strings with the same singular form but different translation depending on the context.
func Tn ¶
Tn translates the given string into the language returned by lang. The string will have different forms for singular and plural forms. The chosen form will depend on the n parameter and the target language. If there's no translation, the singular form will be returned iff n = 1.
func Tnc ¶
Tnc works like Tn, but accepts an additional context argument, to allow differentiating strings with the same singular form but different translation depending on the context. See the documentation for Tn for information about which form (singular or plural) is chosen.
func TranslatedError ¶
Types ¶
type Error ¶
type Error interface { // Error returns the untranslated error message. Error() string // Err translates the Error and returns it as an error. Err(languager Languager) error // TranslatedError returns the translated error message. TranslatedError(languager Languager) string }
Error represents an error which can be translated to another language. Keep in mind that Error also implements error, so you can return an Error from any function that returns error. You can later use FromError() to get back an Error again or TranslatedError, to get an error with the translated message.
func Errorf ¶
Errorf returns a error with the given format and arguments. The returned Error uses Sprintf internally, which means it will translate any argument which supports translation.
func Errorfc ¶
Errorfc returns a error with the given context, format and arguments. The returned Error uses Sprintf internally, which means it will translate any argument which supports translation.
func Errorfn ¶
Errorfn returns a error with the given singular and plural forms as well as the given and arguments. The returned Error uses Sprintf internally, which means it will translate any argument which supports translation.
func Errorfnc ¶
Errorfnc returns a error with the given conext, singular and plural forms as well as the given and arguments. The returned Error uses Sprintf internally, which means it will translate any argument which supports translation.
type Languager ¶
type Languager interface { // Language returns the current language. Language() string }
Languager is the interface implemented by any object which can return a language identifier. Valid language identifiers have either 2 characters e.g. "es", "en" or either 5 e.g. "es_ES", "en_US".
type String ¶
type String string
String is an alias for string, but variables or constants declared with the type String will be extracted for translation.
String declarations might include a context by using the | character, which can be escaped by \. e.g.
var foo = i18n.String("ctx|str")
Declares a translatable string with context "ctx" and a value of "str".
var bar = i18n.String("ctx\\|str")
Declares a translatable string without context and with the value "ctx|str".
String declarations can also include a plural form by adding another | separated field.
var hasPlural = i18n.String("ctx|singular|plural")
func (String) Context ¶
Context returns the translation context for the String, which might be empty.
func (String) TranslatedString ¶
TranslatedString returns the string translated into the language returned by lang.
type Tabler ¶
Tabler is the interface implemented by types which instead of returning a language code, can store and return a translation table, resulting in better performance. All functions in the i18n package which accept a Languager will check if the received object implements Tabler.
type TranslatableString ¶
TranslatableString is the interface implemented by strings that can be translated.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package messages implements functions for extracting messages from source code files and compiling them to Go code.
|
Package messages implements functions for extracting messages from source code files and compiling them to Go code. |