package module
Version: v0.0.0-...-9129f25 Latest Latest

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

Go to latest
Published: Apr 26, 2021 License: MIT Imports: 4 Imported by: 0



Build Status Go Report Card GoDoc





View Source
const (
	DefaultDomain = "messages"

Default values.


This section is empty.


func FromContext

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

FromContext get language from context or use default.

func WithLanguage

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

WithLanguage sets language to context.


type Arg

type Arg interface {
	Val() interface{}

Arg arg translate.

type Option

type Option func(*Translate)

Option configures translate.

func WithArgs

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

WithArgs sets arguments value.

func WithCurrency

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

WithCurrency sets date argument.

func WithDomain

func WithDomain(domain string) Option

WithDomain sets domain translate.

func WithLocale

func WithLocale(locale string) Option

WithLocale sets locale translate.

func WithNumber

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

WithNumber sets number with options.

type Provider

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

Provider for translate key.

type Translate

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

Translate configure translate.

func (*Translate) ArgValues

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

ArgValues gets value arguments.

type Translator

type Translator struct {
	// contains filtered or unexported fields

Translator struct.

func New

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

New creates new translator.

func (*Translator) Trans

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

Trans translates key by options.

package main

import (


func main() {
	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

type TranslatorOption func(*Translator)

TranslatorOption options translator.


Path Synopsis

Jump to

Keyboard shortcuts

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