localizer

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 13 Imported by: 1

README

Localizer: convenient localization for Go

Localizer intends to make it easy for you to work with locales in Go. It was inspired by many good tools that came before it, such as:

I couldn't find one Go package that did everything, so I took the best of what was out there, made some improvements & tweaks, and put them together into localizer. I also wanted to leverage golang.org/x/text/language as much as possible, since it takes care of some difficult tasks.

Getting Started: Example

import (
    "fmt"
    "time"

    "github.com/razor-1/cldr/resources/currency"
    "golang.org/x/text/language"

    "github.com/razor-1/localizer"
)

func main() {
    l, err := localizer.NewLocale(language.Spanish)
    if err != nil {
        panic(err)
    }

    jan2020 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
    month, err := l.Calendar.Format(jan2020, "MMMM")
    if err != nil {
        panic(err)
    }

    fmt.Println(month) // "enero"
    fmt.Println(l.Calendar.FormatNames.Months.Wide.Jan) // "enero"
    fmt.Println(l.FmtNumber(10000.12)) // "100.000,12"
    cur, err := l.FmtCurrency(currency.USD, 10000.12)
    if err != nil {
        panic(err)
    }

    fmt.Println(cur) // "10.000,12 US$"
}

As you can see, the locale object makes it easy to interact with CLDR data. What about translated strings?

gotl := gotext.NewLocale("translations", "es")
gotl.AddDomain("messages")

l, err := localizer.NewLocaleWithStore(language.Spanish, gotl)
if err != nil {
    panic(err)
}
    
fmt.Println(l.Get("Login")) //"Iniciar sesión"
fmt.Println(l.GetPlural("%d hours", 1, 1)) //"1 hora" 
fmt.Println(l.GetPlural("%d hours", 2, 2)) //"2 horas"

You can use any package which implements the localizer/store.TranslationStore interface to load translations. The above example uses the gotext package, providing gettext po/mo support. Hopefully other common packages will implement this interface to provide support for xliff, xmb, and other common formats. You can also easily implement it for your own custom store.

localizer also exposes a message printer if you'd like to use it. Just call l.NewPrinter() and you can then call Printf() and other methods on it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocaleData

func GetLocaleData(tag language.Tag) (*cldr.Locale, error)

GetLocaleData finds the best match for tag and returns a *cldr.Locale, which contains data populated from the Unicode CLDR.

func NamedParameters

func NamedParameters(format string, params FmtParams) string

NamedParameters does string formatting on python-style format strings like "Hello %(name)s".

Types

type FmtParams

type FmtParams map[string]interface{}

type Locale

type Locale struct {
	Tag         language.Tag
	Number      cldr.Number
	Calendar    cldr.Calendar
	Plural      cldr.Plural
	Languages   cldr.Languages
	Territories cldr.Territories
	// contains filtered or unexported fields
}

func GetLocale

func GetLocale(tag language.Tag) *Locale

GetLocale returns a pointer to an existing, already-loaded locale (or nil if if doesn't exist)

func NewLocale

func NewLocale(tag language.Tag) (loc *Locale, err error)

NewLocale instantiates a new Locale for the supplied language tag. It does not load any translations, so it's useful when you only need to use the CLDR data (number/calendar etc). Use NewLocaleWithStore to load translations at initialization time, or call Load on the Locale returned by this function.

func NewLocaleWithStore added in v0.0.3

func NewLocaleWithStore(tag language.Tag, source store.TranslationStore) (loc *Locale, err error)

NewLocaleWithStore instantiates a new Locale for the supplied language tag. It loads the translations from the source store.TranslationStore. Use this if you know you want your Locale to be populated with translations.

func (*Locale) FmtCurrency

func (l *Locale) FmtCurrency(currency string, number interface{}) (formatted string, err error)

func (*Locale) FmtCurrencyWhole

func (l *Locale) FmtCurrencyWhole(currency string, number interface{}) (formatted string, err error)

func (*Locale) FmtDateFull

func (l *Locale) FmtDateFull(tim time.Time) (string, error)

func (*Locale) FmtDateLong

func (l *Locale) FmtDateLong(tim time.Time) (string, error)

func (*Locale) FmtDateMedium

func (l *Locale) FmtDateMedium(tim time.Time) (string, error)

func (*Locale) FmtDateShort

func (l *Locale) FmtDateShort(tim time.Time) (string, error)

func (*Locale) FmtDateTimeFull

func (l *Locale) FmtDateTimeFull(tim time.Time) (string, error)

func (*Locale) FmtDateTimeLong

func (l *Locale) FmtDateTimeLong(tim time.Time) (string, error)

func (*Locale) FmtDateTimeMedium

func (l *Locale) FmtDateTimeMedium(tim time.Time) (string, error)

func (*Locale) FmtDateTimeShort

func (l *Locale) FmtDateTimeShort(tim time.Time) (string, error)

func (*Locale) FmtNumber

func (l *Locale) FmtNumber(number interface{}) string

func (*Locale) FmtNumberWhole

func (l *Locale) FmtNumberWhole(number interface{}) string

func (*Locale) FmtPercent

func (l *Locale) FmtPercent(number interface{}) string

func (*Locale) FmtTimeFull

func (l *Locale) FmtTimeFull(tim time.Time) (string, error)

func (*Locale) FmtTimeLong

func (l *Locale) FmtTimeLong(tim time.Time) (string, error)

func (*Locale) FmtTimeMedium

func (l *Locale) FmtTimeMedium(tim time.Time) (string, error)

func (*Locale) FmtTimeShort

func (l *Locale) FmtTimeShort(tim time.Time) (string, error)

func (*Locale) Get

func (l *Locale) Get(key string) string

Get returns the raw translated string from the catalog, with no formatting.

func (*Locale) GetPlural

func (l *Locale) GetPlural(pluralID string, number interface{}, vars ...interface{}) string

GetPlural determines which plural form should be used for the supplied number. number can be an int type (including int64) or a float formatted as a string. It then determines which plural form should be used by calling the cldr plural function, and returns the corresponding plural translation, formatted with the optional vars.

func (*Locale) GetTranslations added in v0.0.3

func (l *Locale) GetTranslations() map[string]store.Translation

GetTranslations returns the entire catalog of translations currently loaded for this locale. This allows for enumeration of the catalog. Note that it returns a copy so that the internal store can continue to be protected by mutexes.

func (*Locale) Load

func (l *Locale) Load(source store.TranslationStore) error

Load retrieves all translations from the supplied store.TranslationStore and prepares them for use in this Locale.

func (*Locale) NewPrinter

func (l *Locale) NewPrinter() *message.Printer

NewPrinter creates a message.Printer for the Locale

Directories

Path Synopsis
store module
gotextstore Module

Jump to

Keyboard shortcuts

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