localizer

package module
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 12 Imported by: 1

README

Localizer: convenient localization for Go

GitHub Workflow Status Go Report Card Go Version PkgGoDev

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

package main

import (
    "fmt"
    "time"

    "golang.org/x/text/language"

    "github.com/razor-1/localizer"
    "github.com/razor-1/localizer-cldr/resources/currency"
)

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)) // "10.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")

gtstore := gotextstore.GetTranslationStore(gotl)
l, err := localizer.NewLocaleWithStore(language.Spanish, gtstore)
if err != nil {
    panic(err)
}

fmt.Println(l.Get("form.button.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 a helper package, localizer/gotextstore, which integrates with gotext to provide gettext po/mo support for localizer. If other packages implement this interface, support for xliff, xmb, and other common formats can be added. 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

Examples

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{}

FmtParams contains substitutions for python-style format strings

type Locale

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

Locale is the struct providing access to most of the localizer features

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) CountTranslations added in v0.0.8

func (l *Locale) CountTranslations() int

CountTranslations returns the number of translations currently loaded in this locale.

func (*Locale) DisplayName added in v0.0.9

func (l *Locale) DisplayName(tag language.Tag) string

DisplayName returns the formatted display name for the provided language tag in the locale.

Example
loc, _ := NewLocale(language.Spanish)
fmt.Println(loc.DisplayName(language.English))
Output:

inglés

func (*Locale) FmtCurrency

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

FmtCurrency returns the number formatted as a currency for the locale, e.g. "$123.45"

func (*Locale) FmtCurrencyWhole

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

FmtCurrencyWhole returns the number formatted as a currency for the locale, excluding any decimal portion, e.g. "$123"

func (*Locale) FmtDateFull

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

FmtDateFull returns the CLDR Full form of the date, e.g. "Tuesday, September 14, 1999" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateLong

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

FmtDateLong returns the CLDR Long form of the date, e.g. "September 14, 1999" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateMedium

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

FmtDateMedium returns the CLDR Medium form of the date, e.g. "Sep 14, 1999" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateShort

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

FmtDateShort returns the CLDR Short form of the date, e.g. "9/14/99" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateTimeFull

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

FmtDateTimeFull returns the CLDR Full form of the date and time, e.g. "Tuesday, September 14, 1999 at 1:04:20 PM GMT+00:00" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateTimeLong

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

FmtDateTimeLong returns the CLDR Long form of the date and time, e.g. "September 14, 1999 at 1:04:20 PM GMT+00:00" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateTimeMedium

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

FmtDateTimeMedium returns the CLDR Medium form of the date and time, e.g. "Sep 14, 1999, 1:04:20 PM" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtDateTimeShort

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

FmtDateTimeShort returns the CLDR Short form of the date and time, e.g. "9/14/99, 1:04 PM" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtNumber

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

FmtNumber returns the number formatted for the locale, e.g. "123.45" or "123,45"

func (*Locale) FmtNumberWhole

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

FmtNumberWhole returns the number formatted for the locale, excluding any decimal portion, e.g. "123.45" or "123,45"

func (*Locale) FmtPercent

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

FmtPercent returns the number formatted as a percentage for the locale, e.g. 0.09 would return "9 %"

func (*Locale) FmtTimeFull

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

FmtTimeFull returns the CLDR Full form of the time, e.g. "1:04:20 PM GMT+00:00" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtTimeLong

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

FmtTimeLong returns the CLDR Long form of the time, e.g. "1:04:20 PM GMT+00:00" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtTimeMedium

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

FmtTimeMedium returns the CLDR Medium form of the time, e.g. "1:04:20 PM" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

func (*Locale) FmtTimeShort

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

FmtTimeShort returns the CLDR Short form of the time, e.g. "1:04 PM" see http://cldr.unicode.org/translation/date-time-1/date-time-patterns for more information

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

Jump to

Keyboard shortcuts

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