i18n

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package i18n implements support for translation and localization of Go applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sprintf

func Sprintf(lang Languager, format string, args ...interface{}) string

func Sprintfc

func Sprintfc(lang Languager, ctx string, format string, args ...interface{}) string

func Sprintfn

func Sprintfn(lang Languager, singular string, plural string, n int, args ...interface{}) string

func Sprintfnc

func Sprintfnc(lang Languager, ctx string, singular string, plural string, n int, args ...interface{}) string

func T

func T(lang Languager, str string) string

T returns the given string translated into the language returned by lang.

func Tc

func Tc(lang Languager, context string, str string) string

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

func Tn(lang Languager, singular string, plural string, n int) string

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

func Tnc(lang Languager, context string, singular string, plural string, n int) string

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

func TranslatedError(err error, languager Languager) error

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

func Errorf(format string, args ...interface{}) Error

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

func Errorfc(ctx string, format string, args ...interface{}) Error

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

func Errorfn(singular string, plural string, n int, args ...interface{}) Error

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

func Errorfnc(ctx string, singular string, plural string, n int, args ...interface{}) Error

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.

func FromError

func FromError(e error) Error

FromError returns an Error from an error, translating it when possible. If e already implements Error, the received value is returned.

func NewError

func NewError(message string) Error

NewError returns an Error with the given message.

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

func (s String) Context() string

Context returns the translation context for the String, which might be empty.

func (String) String

func (s String) String() string

String returns the String as a plain string.

func (String) TranslatedString

func (s String) TranslatedString(lang Languager) string

TranslatedString returns the string translated into the language returned by lang.

type Tabler

type Tabler interface {
	TranslationTable() *table.Table
}

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

type TranslatableString interface {
	TranslatedString(lang Languager) string
}

TranslatableString is the interface implemented by strings that can be translated.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL