translation: Index | Examples | Files | Directories

package translation

import ""



Package Files

context.go translate.go translator.go


const (
    DefaultDomain = "messages"

Default values.

func FromContext Uses

func FromContext(ctx context.Context, def language.Tag) language.Tag

FromContext get language from context or use default.

func WithLanguage Uses

func WithLanguage(ctx context.Context, lang language.Tag) context.Context

WithLanguage sets language to context.

type Arg Uses

type Arg interface {
    Val() interface{}

Arg arg translate.

type Option Uses

type Option func(*Translate)

Option configures translate.

func WithArgs Uses

func WithArgs(vals ...interface{}) Option

WithArgs sets arguments value.

func WithCurrency Uses

func WithCurrency(key string, val interface{}, opts ...arg.CurrencyOption) Option

WithCurrency sets date argument.

func WithDomain Uses

func WithDomain(domain string) Option

WithDomain sets domain translate.

func WithLocale Uses

func WithLocale(locale string) Option

WithLocale sets locale translate.

func WithNumber Uses

func WithNumber(key string, val interface{}, opts ...arg.NumberOption) Option

WithNumber sets number with options.

type Provider Uses

type Provider interface {
    Translate(ctx context.Context, key string, opt Translate) string

Provider for translate key.

type Translate Uses

type Translate struct {
    Domain string
    Locale language.Tag
    Args   []Arg

Translate configure translate.

func (*Translate) ArgValues Uses

func (t *Translate) ArgValues() []interface{}

ArgValues gets value arguments.

type Translator Uses

type Translator struct {
    // contains filtered or unexported fields

Translator struct.

func New Uses

func New(locale language.Tag, provider Provider, opts ...TranslatorOption) *Translator

New creates new translator.

func (*Translator) Trans Uses

func (t *Translator) Trans(ctx context.Context, key string, opts ...Option) string

Trans translates key by options.


cat := catalog.NewBuilder()

_ = cat.Set(language.Russian, "Hello World!", catalog.String("Привет Мир!"))
_ = cat.Set(language.Slovak, "Hello World!", catalog.String("Ahoj Svet!"))
_ = cat.Set(language.Hebrew, "Hello World!", catalog.String("שלום עולם"))
_ = cat.Set(language.Russian, "Hello {name}!", catalog.String("Привет %[1]s!"))
_ = cat.Set(language.English, "Hello {name}!", catalog.String("Hello %[1]s!"))
_ = cat.Set(language.English, "There are {bikes} bikes per household.", catalog.String("There are %v bikes per household."))
_ = cat.Set(language.Russian, "There are {bikes} bikes per household.", catalog.String("В каждом доме есть %v велосипеда."))
_ = cat.Set(language.English, "You are {minute} minute(s) late.",
    plural.Selectf(1, "",
        "=0", "You're on time.",
        plural.One, "You are %v minute late.",
        plural.Other, "You are %v minutes late."))
_ = cat.Set(language.Russian, "You are {minute} minute(s) late.",
    plural.Selectf(1, "",
        "=1", "Вы опоздали на одну минуту.",
        "=0", "Вы вовремя.",
        plural.One, "Вы опоздали на %v минуту.",
        plural.Few, "Вы опоздали на %v минуты.",
        plural.Other, "Вы опоздали на %v минут.",
_ = cat.Set(language.Russian, "It costs {cost}.",
    catalog.String("Это стоит %.2f."))
_ = cat.Set(language.English, "It costs {cost}.",
    catalog.String("It costs %.2f."))

provider := gotext.NewProvider(gotext.WithCatalog(translation.DefaultDomain, cat))

trans := translation.New(language.Russian, provider)
ctx := context.Background()
// context with language
heCtx := translation.WithLanguage(ctx, language.Make("he"))

fmt.Println(trans.Trans(ctx, "Hello World!"))
fmt.Println(trans.Trans(ctx, "Hello World!", translation.WithLocale("en")))
fmt.Println(trans.Trans(ctx, "Hello World!", translation.WithLocale("sk")))
fmt.Println(trans.Trans(heCtx, "Hello World!"))

fmt.Println(trans.Trans(ctx, "Hello {name}!", translation.WithArgs("Andrey")))

fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithNumber("minute", 1)))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithNumber("minute", 0)))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithNumber("minute", 101)))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithArgs(123456.78)))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithArgs(50)))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithArgs(1), translation.WithLocale("en")))
fmt.Println(trans.Trans(ctx, "You are {minute} minute(s) late.", translation.WithArgs(33), translation.WithLocale("en")))

fmt.Println(trans.Trans(ctx, "There are {bikes} bikes per household.", translation.WithNumber("bikes", 1.2)))
fmt.Println(trans.Trans(ctx, "There are {bikes} bikes per household.", translation.WithNumber("bikes", 1.2), translation.WithLocale("en")))

fmt.Println(trans.Trans(ctx, "It costs {cost}.",
    translation.WithCurrency("cost", 12.0101, arg.WithCurrencyISO("rub"), arg.WithCurrencyFormat(arg.CurrencyFormatSymbol))))
fmt.Println(trans.Trans(ctx, "It costs {cost}.",
    translation.WithCurrency("cost", 15.0, arg.WithCurrencyISO("HKD"), arg.WithCurrencyFormat(arg.CurrencyFormatSymbol)),


Привет Мир!
Hello World!
Ahoj Svet!
שלום עולם
Привет Andrey!
Вы опоздали на одну минуту.
Вы вовремя.
Вы опоздали на 101 минуту.
Вы опоздали на 123 456,78 минут.
Вы опоздали на 50 минут.
You are 1 minute late.
You are 33 minutes late.
В каждом доме есть 1,2 велосипеда.
There are 1.2 bikes per household.
Это стоит ₽ 12.0101.
It costs HK$ 15.

type TranslatorOption Uses

type TranslatorOption func(*Translator)

TranslatorOption options translator.



Package translation imports 4 packages (graph) and is imported by 2 packages. Updated 2020-05-14. Refresh now. Tools for package owners.