...

Package ut

import "github.com/go-playground/universal-translator"
Overview
Index

Overview ▾

Index ▾

Variables
type ErrBadParamSyntax
    func (e *ErrBadParamSyntax) Error() string
type ErrBadPluralDefinition
    func (e *ErrBadPluralDefinition) Error() string
type ErrCardinalTranslation
    func (e *ErrCardinalTranslation) Error() string
type ErrConflictingTranslation
    func (e *ErrConflictingTranslation) Error() string
type ErrExistingTranslator
    func (e *ErrExistingTranslator) Error() string
type ErrMissingBracket
    func (e *ErrMissingBracket) Error() string
type ErrMissingLocale
    func (e *ErrMissingLocale) Error() string
type ErrMissingPluralTranslation
    func (e *ErrMissingPluralTranslation) Error() string
type ErrOrdinalTranslation
    func (e *ErrOrdinalTranslation) Error() string
type ErrRangeTranslation
    func (e *ErrRangeTranslation) Error() string
type ImportExportFormat
type Translator
type UniversalTranslator
    func New(fallback locales.Translator, supportedLocales ...locales.Translator) *UniversalTranslator
    func (t *UniversalTranslator) AddTranslator(translator locales.Translator, override bool) error
    func (t *UniversalTranslator) Export(format ImportExportFormat, dirname string) error
    func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool)
    func (t *UniversalTranslator) GetFallback() Translator
    func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool)
    func (t *UniversalTranslator) Import(format ImportExportFormat, dirnameOrFilename string) error
    func (t *UniversalTranslator) ImportByReader(format ImportExportFormat, reader io.Reader) error
    func (t *UniversalTranslator) VerifyTranslations() (err error)

Package files

errors.go import_export.go translator.go universal_translator.go

Variables

var (
    // ErrUnknowTranslation indicates the translation could not be found
    ErrUnknowTranslation = errors.New("Unknown Translation")
)

type ErrBadParamSyntax

ErrBadParamSyntax is the error representing a bad parameter definition in a translation eg. This is a {must-be-int}

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

func (*ErrBadParamSyntax) Error

func (e *ErrBadParamSyntax) Error() string

Error returns ErrBadParamSyntax error message

type ErrBadPluralDefinition

ErrBadPluralDefinition is the error representing an incorrect plural definition usually found within translations defined within files during the import process.

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

func (*ErrBadPluralDefinition) Error

func (e *ErrBadPluralDefinition) Error() string

Error returns ErrBadPluralDefinition's internal error text

type ErrCardinalTranslation

ErrCardinalTranslation is the error representing a cardinal translation error

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

func (*ErrCardinalTranslation) Error

func (e *ErrCardinalTranslation) Error() string

Error returns ErrCardinalTranslation's internal error text

type ErrConflictingTranslation

ErrConflictingTranslation is the error representing a conflicting translation

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

func (*ErrConflictingTranslation) Error

func (e *ErrConflictingTranslation) Error() string

Error returns ErrConflictingTranslation's internal error text

type ErrExistingTranslator

ErrExistingTranslator is the error representing a conflicting translator

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

func (*ErrExistingTranslator) Error

func (e *ErrExistingTranslator) Error() string

Error returns ErrExistingTranslator's internal error text

type ErrMissingBracket

ErrMissingBracket is the error representing a missing bracket in a translation eg. This is a {0 <-- missing ending '}'

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

func (*ErrMissingBracket) Error

func (e *ErrMissingBracket) Error() string

Error returns ErrMissingBracket error message

type ErrMissingLocale

ErrMissingLocale is the error representing an expected locale that could not be found aka locale not registered with the UniversalTranslator Instance

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

func (*ErrMissingLocale) Error

func (e *ErrMissingLocale) Error() string

Error returns ErrMissingLocale's internal error text

type ErrMissingPluralTranslation

ErrMissingPluralTranslation is the error signifying a missing translation given the locales plural rules.

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

func (*ErrMissingPluralTranslation) Error

func (e *ErrMissingPluralTranslation) Error() string

Error returns ErrMissingPluralTranslation's internal error text

type ErrOrdinalTranslation

ErrOrdinalTranslation is the error representing an ordinal translation error

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

func (*ErrOrdinalTranslation) Error

func (e *ErrOrdinalTranslation) Error() string

Error returns ErrOrdinalTranslation's internal error text

type ErrRangeTranslation

ErrRangeTranslation is the error representing a range translation error

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

func (*ErrRangeTranslation) Error

func (e *ErrRangeTranslation) Error() string

Error returns ErrRangeTranslation's internal error text

type ImportExportFormat

ImportExportFormat is the format of the file import or export

type ImportExportFormat uint8

supported Export Formats

const (
    FormatJSON ImportExportFormat = iota
)

type Translator

Translator is universal translators translator instance which is a thin wrapper around locales.Translator instance providing some extra functionality

type Translator interface {
    locales.Translator

    // adds a normal translation for a particular language/locale
    // {#} is the only replacement type accepted and are ad infinitum
    // eg. one: '{0} day left' other: '{0} days left'
    Add(key interface{}, text string, override bool) error

    // adds a cardinal plural translation for a particular language/locale
    // {0} is the only replacement type accepted and only one variable is accepted as
    // multiple cannot be used for a plural rule determination, unless it is a range;
    // see AddRange below.
    // eg. in locale 'en' one: '{0} day left' other: '{0} days left'
    AddCardinal(key interface{}, text string, rule locales.PluralRule, override bool) error

    // adds an ordinal plural translation for a particular language/locale
    // {0} is the only replacement type accepted and only one variable is accepted as
    // multiple cannot be used for a plural rule determination, unless it is a range;
    // see AddRange below.
    // eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring'
    // - 1st, 2nd, 3rd...
    AddOrdinal(key interface{}, text string, rule locales.PluralRule, override bool) error

    // adds a range plural translation for a particular language/locale
    // {0} and {1} are the only replacement types accepted and only these are accepted.
    // eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
    AddRange(key interface{}, text string, rule locales.PluralRule, override bool) error

    // creates the translation for the locale given the 'key' and params passed in
    T(key interface{}, params ...string) (string, error)

    // creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments
    //  and param passed in
    C(key interface{}, num float64, digits uint64, param string) (string, error)

    // creates the ordinal translation for the locale given the 'key', 'num' and 'digit' arguments
    // and param passed in
    O(key interface{}, num float64, digits uint64, param string) (string, error)

    //  creates the range translation for the locale given the 'key', 'num1', 'digit1', 'num2' and
    //  'digit2' arguments and 'param1' and 'param2' passed in
    R(key interface{}, num1 float64, digits1 uint64, num2 float64, digits2 uint64, param1, param2 string) (string, error)

    // VerifyTranslations checks to ensures that no plural rules have been
    // missed within the translations.
    VerifyTranslations() error
}

type UniversalTranslator

UniversalTranslator holds all locale & translation data

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

func New

func New(fallback locales.Translator, supportedLocales ...locales.Translator) *UniversalTranslator

New returns a new UniversalTranslator instance set with the fallback locale and locales it should support

func (*UniversalTranslator) AddTranslator

func (t *UniversalTranslator) AddTranslator(translator locales.Translator, override bool) error

AddTranslator adds the supplied translator, if it already exists the override param will be checked and if false an error will be returned, otherwise the translator will be overridden; if the fallback matches the supplied translator it will be overridden as well NOTE: this is normally only used when translator is embedded within a library

func (*UniversalTranslator) Export

func (t *UniversalTranslator) Export(format ImportExportFormat, dirname string) error

Export writes the translations out to a file on disk.

NOTE: this currently only works with string or int translations keys.

func (*UniversalTranslator) FindTranslator

func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool)

FindTranslator trys to find a Translator based on an array of locales and returns the first one it can find, otherwise returns the fallback translator.

func (*UniversalTranslator) GetFallback

func (t *UniversalTranslator) GetFallback() Translator

GetFallback returns the fallback locale

func (*UniversalTranslator) GetTranslator

func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool)

GetTranslator returns the specified translator for the given locale, or fallback if not found

func (*UniversalTranslator) Import

func (t *UniversalTranslator) Import(format ImportExportFormat, dirnameOrFilename string) error

Import reads the translations out of a file or directory on disk.

NOTE: this currently only works with string or int translations keys.

func (*UniversalTranslator) ImportByReader

func (t *UniversalTranslator) ImportByReader(format ImportExportFormat, reader io.Reader) error

ImportByReader imports the the translations found within the contents read from the supplied reader.

NOTE: generally used when assets have been embedded into the binary and are already in memory.

func (*UniversalTranslator) VerifyTranslations

func (t *UniversalTranslator) VerifyTranslations() (err error)

VerifyTranslations runs through all locales and identifies any issues eg. missing plural rules for a locale