i18n

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: Apache-2.0 Imports: 12 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Langs is the list of all loaded languages in the application
	Langs []string
)

Functions

func BootStrap

func BootStrap()

BootStrap initializes available languages

func GetAllCustomTranslations

func GetAllCustomTranslations() map[string]map[string]map[string]string

GetAllCustomTranslations returns all custom translations by lang and by modules

func GetAllLanguageList

func GetAllLanguageList() []string

GetAllLanguageList returns a slice containing all known language codes

func LoadPOFile

func LoadPOFile(fileName string)

LoadPOFile load the file with the given filename into the Registry. This function is meant to be called several times to load all translations across all languages and modules. It panics in case of errors in the PO file.

func OverrideLocale

func OverrideLocale(loc *Locale) error

OverrideLocale overrides the locale with the same ISO code as loc with loc. If such a locale does not exist, an error is returned and the locale is not registered.

func RegisterLocale

func RegisterLocale(loc *Locale) error

RegisterLocale registers a new locale

func TranslateCode

func TranslateCode(lang, context, src string) string

TranslateCode returns the translation for the given src in the given lang, in the given context. If no translation is found or if the translation is the empty string src is returned.

func TranslateCustom

func TranslateCustom(lang, id, moduleName string) string

TranslateCustom returns the custom translation for the given id

func TranslateFieldDescription

func TranslateFieldDescription(lang, model, field, defaultValue string) string

TranslateFieldDescription returns the translation for the given model field name in the given lang, using the default translation Registry. If no translation is found or if the translation is the empty string defaultValue is returned.

func TranslateFieldHelp

func TranslateFieldHelp(lang, model, field, defaultValue string) string

TranslateFieldHelp returns the translation for the given model field help in the given lang, using the default translation Registry. If no translation is found or if the translation is the empty string defaultValue is returned.

func TranslateFieldSelection

func TranslateFieldSelection(lang, model, field string, selection types.Selection) types.Selection

TranslateFieldSelection returns the translated version of the given selection in the given lang, using the default translation Registry. When no translation is found for an item, the original string is used.

func TranslateResourceItem

func TranslateResourceItem(lang, resourceID, src string) string

TranslateResourceItem returns the translation for the given src of the given resource in the given lang using the default translation Registry. If no translation is found or if the translation is the empty string src is returned.

Types

type Currency added in v0.0.38

type Currency interface {
	// Symbol returns the currency symbol when printing amounts
	Symbol() string
	// Position returns 'before' or 'after' depending on where the symbol must be printed
	Position() string
	// DecimalPlaces for this currency
	DecimalPlaces() int
	// Round returns the given value rounded according to this currency
	Round(float64) float64
}

A Currency with symbol, position and decimals

type LangDirection

type LangDirection string

A LangDirection defines the direction of a language either left-to-right or right-to-left

const (
	// LangDirectionLTR defines a language written from left to right
	LangDirectionLTR LangDirection = "ltr"
	// LangDirectionRTL defines a language written from right to left
	LangDirectionRTL LangDirection = "rtl"
)

type Locale

type Locale struct {
	Name         string         `json:"name"`
	Code         string         `json:"code"`
	ISOCode      string         `json:"iso_code"`
	WeekStart    time.Weekday   `json:"week_start"`
	DateFormat   string         `json:"date_format"`
	DateFormatGo string         `json:"date_format_go"`
	Direction    LangDirection  `json:"lang_direction"`
	ThousandsSep string         `json:"thousands_sep"`
	TimeFormat   string         `json:"time_format"`
	TimeFormatGo string         `json:"time_format_go"`
	DecimalPoint string         `json:"decimal_point"`
	Grouping     NumberGrouping `json:"grouping"`
}

Locale defines the parameters of a language locale

func GetLocale

func GetLocale(lang string) *Locale

GetLocale returns a Locale struct describing a language's rules at first call, the data file containing all languages parameters is read if the language is not loaded, it returns a Locale similar to English (en_US)

func (*Locale) Check

func (l *Locale) Check() error

Check returns an error if this locale is not valid

func (*Locale) FormatDate added in v0.0.38

func (l *Locale) FormatDate(date dates.Date) string

FormatDate returns the given date formatted according to this Locale

func (*Locale) FormatDateTime added in v0.0.38

func (l *Locale) FormatDateTime(datetime dates.DateTime) string

FormatDateTime returns the given datetime formatted according to this Locale

func (*Locale) FormatFloat added in v0.0.38

func (l *Locale) FormatFloat(number float64, digits nbutils.Digits) string

FormatFloat formats the given number according to this Locale, with the given digits

func (*Locale) FormatMonetary added in v0.0.38

func (l *Locale) FormatMonetary(value float64, curr Currency) string

FormatMonetary formats the given value according to this Locale and given currency

func (*Locale) FormatTime added in v0.0.38

func (l *Locale) FormatTime(datetime dates.DateTime) string

FormatTime returns the time part of the given datetime formatted according to this Locale

type NumberGrouping added in v0.0.38

type NumberGrouping []int

NumberGrouping represents grouping values of a number as follows:

  • it splits a number into groups of N, N being a value in the slice
  • the values define groups from right to left
  • all values should be positive
  • 0 at the end means repetition of previous int
  • if the last value is not a 0, the grouping will end e.g. : 3 -> 123456,789 3,0 -> 123,456,789 3,2 -> 1234,56,789 3,2,0 -> 12,34,56,789

func (NumberGrouping) MarshalJSON added in v0.0.40

func (nb NumberGrouping) MarshalJSON() ([]byte, error)

MarshalJSON function for the NumberGrouping type that should marshal as string.

type Translation

type Translation struct {
	// contains filtered or unexported fields
}

A Translation holds all the translations for a given language

type TranslationsCollection

type TranslationsCollection struct {
	// contains filtered or unexported fields
}

A TranslationsCollection holds all the translations of the application

var Registry *TranslationsCollection

Registry holds all the translation of the application

func NewTranslationsCollection

func NewTranslationsCollection() *TranslationsCollection

NewTranslationsCollection returns a pointer to a new TranslationsCollection ready for use

func (*TranslationsCollection) LoadPOFile

func (tc *TranslationsCollection) LoadPOFile(fileName string)

LoadPOFile load the file with the given filename into the TranslationsCollection. This function can be called several times to iteratively load translations. It panics in case of errors in the PO file.

func (*TranslationsCollection) TranslateCode

func (tc *TranslationsCollection) TranslateCode(lang, context, src string) string

TranslateCode returns the translation for the given src in the given lang, in the given context. If no translation is found or if the translation is the empty string src is returned.

func (*TranslationsCollection) TranslateCustom

func (tc *TranslationsCollection) TranslateCustom(lang, id, moduleName string) string

TranslateCustom returns the translation for the given src of the given custom po string in the given lang. If no translation is found or if the translation is the empty string src is returned.

func (*TranslationsCollection) TranslateFieldDescription

func (tc *TranslationsCollection) TranslateFieldDescription(lang, model, field, defaultValue string) string

TranslateFieldDescription returns the translation for the given model field name in the given lang. If no translation is found or if the translation is the empty string defaultValue is returned.

func (*TranslationsCollection) TranslateFieldHelp

func (tc *TranslationsCollection) TranslateFieldHelp(lang, model, field, defaultValue string) string

TranslateFieldHelp returns the translation for the given model field help in the given lang. If no translation is found or if the translation is the empty string defaultValue is returned.

func (*TranslationsCollection) TranslateFieldSelection

func (tc *TranslationsCollection) TranslateFieldSelection(lang, model, field string, selection types.Selection) types.Selection

TranslateFieldSelection returns the translated version of the given selection in the given lang. When no translation is found for an item, the original string is used.

func (*TranslationsCollection) TranslateResourceItem

func (tc *TranslationsCollection) TranslateResourceItem(lang, resourceID, src string) string

TranslateResourceItem returns the translation for the given src of the given resource in the given lang. If no translation is found or if the translation is the empty string src is returned.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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