Documentation
¶
Overview ¶
Package i18n handles text and data translation based on various locales.
The code in this package was derived directly from https://github.com/go-playground/universal-translator. The file format has been changed from JSON to TOML for ease of translations and translation keys must all be strings.
Index ¶
- Constants
- type ErrBadParamSyntax
- type ErrCardinalTranslation
- type ErrConflictingTranslation
- type ErrExistingTranslator
- type ErrExportPathFailure
- type ErrExportWriteFailure
- type ErrImportPathFailure
- type ErrImportReadFailure
- type ErrInvalidRuleType
- type ErrKeyIsNotString
- type ErrLocaleNotRegistered
- type ErrMissingBrace
- type ErrMissingPluralTranslation
- type ErrOrdinalTranslation
- type ErrRangeTranslation
- type ErrRegisterValidationTranslationFailure
- type ErrUnknownTranslation
- type UniversalTranslator
- func (t *UniversalTranslator) AddTranslator(translator locales.Translator, override bool, ctx context.Context) error
- func (ut *UniversalTranslator) Export(path string, ctx context.Context) error
- func (t *UniversalTranslator) FindTranslator(locales ...string) (trans ut.Translator, found bool)
- func (t *UniversalTranslator) GetFallback() ut.Translator
- func (t *UniversalTranslator) GetTranslator(locale string) (trans ut.Translator, found bool)
- func (ut *UniversalTranslator) Import(path string, ctx context.Context) error
- func (ut *UniversalTranslator) ImportFromReader(reader io.Reader, ctx context.Context) error
- func (t *UniversalTranslator) VerifyTranslations() (err error)
Constants ¶
const ( ErrKeyIsNotStringCode = 1501 ErrUnknownTranslationCode = 1502 ErrExistingTranslatorCode = 1503 ErrConflictingTranslationCode = 1504 ErrRangeTranslationCode = 1505 ErrOrdinalTranslationCode = 1506 ErrCardinalTranslationCode = 1507 ErrMissingPluralTranslationCode = 1508 ErrMissingBraceCode = 1509 ErrBadParamSyntaxCode = 1510 ErrLocaleNotRegisteredCode = 1511 ErrInvalidRuleTypeCode = 1512 ErrExportPathFailureCode = 1513 ErrExportWriteFailureCode = 1514 ErrImportPathFailureCode = 1515 ErrImportReadFailureCode = 1516 ErrRegisterValidationTranslationFailureCode = 1517 )
Object error codes (1501-1750)
const ( RuleTypePlain = "plain" RuleTypeCardinal = "cardinal" RuleTypeOrdinal = "ordinal" RuleTypeRange = "range" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrBadParamSyntax ¶
ErrBadParamSyntax occurs when there is a bad parameter definition in a translation. eg. This is a {must-be-int}
func (*ErrBadParamSyntax) Code ¶ added in v0.1.5
func (e *ErrBadParamSyntax) Code() int
Code returns the corresponding error code.
func (*ErrBadParamSyntax) Error ¶
func (e *ErrBadParamSyntax) Error() string
Error returns the string version of the error.
func (*ErrBadParamSyntax) InternalError ¶ added in v0.1.9
func (e *ErrBadParamSyntax) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrCardinalTranslation ¶
type ErrCardinalTranslation struct {
Text string
}
ErrCardinalTranslation occurs when there is a cardinal translation error.
func (*ErrCardinalTranslation) Code ¶ added in v0.1.5
func (e *ErrCardinalTranslation) Code() int
Code returns the corresponding error code.
func (*ErrCardinalTranslation) Error ¶
func (e *ErrCardinalTranslation) Error() string
Error returns the string version of the error.
func (*ErrCardinalTranslation) InternalError ¶ added in v0.1.9
func (e *ErrCardinalTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrConflictingTranslation ¶
type ErrConflictingTranslation struct { Locale string Key string Rule locales.PluralRule Text string }
ErrConflictingTranslation occurs when there is a conflicting translation.
func (*ErrConflictingTranslation) Code ¶ added in v0.1.5
func (e *ErrConflictingTranslation) Code() int
Code returns the corresponding error code.
func (*ErrConflictingTranslation) Error ¶
func (e *ErrConflictingTranslation) Error() string
Error returns the string version of the error.
func (*ErrConflictingTranslation) InternalError ¶ added in v0.1.9
func (e *ErrConflictingTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrExistingTranslator ¶
type ErrExistingTranslator struct {
Locale string
}
ErrExistingTranslator occurs when there is a conflicting translator.
func (*ErrExistingTranslator) Code ¶ added in v0.1.5
func (e *ErrExistingTranslator) Code() int
Code returns the corresponding error code.
func (*ErrExistingTranslator) Error ¶
func (e *ErrExistingTranslator) Error() string
Error returns the string version of the error.
func (*ErrExistingTranslator) InternalError ¶ added in v0.1.9
func (e *ErrExistingTranslator) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrExportPathFailure ¶ added in v0.1.5
ErrExportPathFailure occurs when a failure is detected while creating the output path for exported trandlations.
func (*ErrExportPathFailure) Code ¶ added in v0.1.5
func (e *ErrExportPathFailure) Code() int
Code returns the corresponding error code.
func (*ErrExportPathFailure) Error ¶ added in v0.1.5
func (e *ErrExportPathFailure) Error() string
Error returns the string version of the error.
func (*ErrExportPathFailure) InternalError ¶ added in v0.1.9
func (e *ErrExportPathFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrExportWriteFailure ¶ added in v0.1.5
ErrExportWriteFailure occurs when a failure is detected while writing exported translations.
func (*ErrExportWriteFailure) Code ¶ added in v0.1.5
func (e *ErrExportWriteFailure) Code() int
Code returns the corresponding error code.
func (*ErrExportWriteFailure) Error ¶ added in v0.1.5
func (e *ErrExportWriteFailure) Error() string
Error returns the string version of the error.
func (*ErrExportWriteFailure) InternalError ¶ added in v0.1.9
func (e *ErrExportWriteFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrImportPathFailure ¶ added in v0.1.5
ErrImportPathFailure occurs when a failure is detected while opening the file or folder for importing translations.
func (*ErrImportPathFailure) Code ¶ added in v0.1.5
func (e *ErrImportPathFailure) Code() int
Code returns the corresponding error code.
func (*ErrImportPathFailure) Error ¶ added in v0.1.5
func (e *ErrImportPathFailure) Error() string
Error returns the string version of the error.
func (*ErrImportPathFailure) InternalError ¶ added in v0.1.9
func (e *ErrImportPathFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrImportReadFailure ¶ added in v0.1.5
ErrImportReadFailure occurs when a failure is detected while reading translations during import.
func (*ErrImportReadFailure) Code ¶ added in v0.1.5
func (e *ErrImportReadFailure) Code() int
Code returns the corresponding error code.
func (*ErrImportReadFailure) Error ¶ added in v0.1.5
func (e *ErrImportReadFailure) Error() string
Error returns the string version of the error.
func (*ErrImportReadFailure) InternalError ¶ added in v0.1.9
func (e *ErrImportReadFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrInvalidRuleType ¶ added in v0.1.5
type ErrInvalidRuleType struct {
RuleType string
}
ErrInvalidRuleType occurs when an invalid rule type is detected in the translation file.
func (*ErrInvalidRuleType) Code ¶ added in v0.1.5
func (e *ErrInvalidRuleType) Code() int
Code returns the corresponding error code.
func (*ErrInvalidRuleType) Error ¶ added in v0.1.5
func (e *ErrInvalidRuleType) Error() string
Error returns the string version of the error.
func (*ErrInvalidRuleType) InternalError ¶ added in v0.1.9
func (e *ErrInvalidRuleType) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrKeyIsNotString ¶ added in v0.1.5
type ErrKeyIsNotString struct { }
ErrKeyIsNotString occurs when a translation key is not a string.
func (*ErrKeyIsNotString) Code ¶ added in v0.1.5
func (e *ErrKeyIsNotString) Code() int
Code returns the corresponding error code.
func (*ErrKeyIsNotString) Error ¶ added in v0.1.5
func (e *ErrKeyIsNotString) Error() string
Error returns the string version of the error.
func (*ErrKeyIsNotString) InternalError ¶ added in v0.1.9
func (e *ErrKeyIsNotString) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrLocaleNotRegistered ¶ added in v0.1.5
type ErrLocaleNotRegistered struct {
Locale string
}
ErrLocaleNotRegistered occurs when a local is not registered with the translator instance.
func (*ErrLocaleNotRegistered) Code ¶ added in v0.1.5
func (e *ErrLocaleNotRegistered) Code() int
Code returns the corresponding error code.
func (*ErrLocaleNotRegistered) Error ¶ added in v0.1.5
func (e *ErrLocaleNotRegistered) Error() string
Error returns the string version of the error.
func (*ErrLocaleNotRegistered) InternalError ¶ added in v0.1.9
func (e *ErrLocaleNotRegistered) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrMissingBrace ¶ added in v0.1.5
ErrMissingBrace occurs when there is a missing brace in a translation. eg. This is a {0 <-- missing ending '}'
func (*ErrMissingBrace) Code ¶ added in v0.1.5
func (e *ErrMissingBrace) Code() int
Code returns the corresponding error code.
func (*ErrMissingBrace) Error ¶ added in v0.1.5
func (e *ErrMissingBrace) Error() string
Error returns the string version of the error.
func (*ErrMissingBrace) InternalError ¶ added in v0.1.9
func (e *ErrMissingBrace) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrMissingPluralTranslation ¶
type ErrMissingPluralTranslation struct { Locale string Key string Rule locales.PluralRule TranslationType string }
ErrMissingPluralTranslation occurs when there is a missing translation given the locales plural rules.
func (*ErrMissingPluralTranslation) Code ¶ added in v0.1.5
func (e *ErrMissingPluralTranslation) Code() int
Code returns the corresponding error code.
func (*ErrMissingPluralTranslation) Error ¶
func (e *ErrMissingPluralTranslation) Error() string
Error returns the string version of the error.
func (*ErrMissingPluralTranslation) InternalError ¶ added in v0.1.9
func (e *ErrMissingPluralTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrOrdinalTranslation ¶
type ErrOrdinalTranslation struct {
Text string
}
ErrOrdinalTranslation occurs when there is an ordinal translation error.
func (*ErrOrdinalTranslation) Code ¶ added in v0.1.5
func (e *ErrOrdinalTranslation) Code() int
Code returns the corresponding error code.
func (*ErrOrdinalTranslation) Error ¶
func (e *ErrOrdinalTranslation) Error() string
Error returns the string version of the error.
func (*ErrOrdinalTranslation) InternalError ¶ added in v0.1.9
func (e *ErrOrdinalTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrRangeTranslation ¶
type ErrRangeTranslation struct {
Text string
}
ErrRangeTranslation occurs when there is a range translation error.
func (*ErrRangeTranslation) Code ¶ added in v0.1.5
func (e *ErrRangeTranslation) Code() int
Code returns the corresponding error code.
func (*ErrRangeTranslation) Error ¶
func (e *ErrRangeTranslation) Error() string
Error returns the string version of the error.
func (*ErrRangeTranslation) InternalError ¶ added in v0.1.9
func (e *ErrRangeTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrRegisterValidationTranslationFailure ¶ added in v0.1.5
ErrRegisterValidationTranslationFailure occurs when a failure is detected while registering a validation tag's error message translation.
func (*ErrRegisterValidationTranslationFailure) Code ¶ added in v0.1.5
func (e *ErrRegisterValidationTranslationFailure) Code() int
Code returns the corresponding error code.
func (*ErrRegisterValidationTranslationFailure) Error ¶ added in v0.1.5
func (e *ErrRegisterValidationTranslationFailure) Error() string
Error returns the string version of the error.
func (*ErrRegisterValidationTranslationFailure) InternalError ¶ added in v0.1.9
func (e *ErrRegisterValidationTranslationFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrUnknownTranslation ¶ added in v0.1.5
type ErrUnknownTranslation struct {
Key string
}
ErrUnknownTranslation occurs when an unknown translation key is supplied.
func (*ErrUnknownTranslation) Code ¶ added in v0.1.5
func (e *ErrUnknownTranslation) Code() int
Code returns the corresponding error code.
func (*ErrUnknownTranslation) Error ¶ added in v0.1.5
func (e *ErrUnknownTranslation) Error() string
Error returns the string version of the error.
func (*ErrUnknownTranslation) InternalError ¶ added in v0.1.9
func (e *ErrUnknownTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type UniversalTranslator ¶
type UniversalTranslator struct {
// contains filtered or unexported fields
}
UniversalTranslator holds all locale & translation data.
func NewUniversalTranslator ¶
func NewUniversalTranslator(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, ctx context.Context) 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.
The following errors are returned by this function: ErrExistingTranslator
func (*UniversalTranslator) Export ¶
func (ut *UniversalTranslator) Export(path string, ctx context.Context) error
Export writes the translations out to a directory.
Each locale is written to its own file called <locale>.toml in the given directory.
The following errors are returned by this function: ErrExportPathFailure, ErrKeyIsNotString, ExportWriteFailure
func (*UniversalTranslator) FindTranslator ¶
func (t *UniversalTranslator) FindTranslator(locales ...string) (trans ut.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() ut.Translator
GetFallback returns the fallback locale.
func (*UniversalTranslator) GetTranslator ¶
func (t *UniversalTranslator) GetTranslator(locale string) (trans ut.Translator, found bool)
GetTranslator returns the specified translator for the given locale or fallback if not found.
func (*UniversalTranslator) Import ¶
func (ut *UniversalTranslator) Import(path string, ctx context.Context) error
Import reads the translations from a file or directory on disk.
If the path is a directory, any .toml files located in the directory will be imported.
The following errors are returned by this function: ErrImportPathFailure, any error from the ImportFromReader() function
func (*UniversalTranslator) ImportFromReader ¶
ImportFromReader imports the the translations found within the contents read from the supplied reader.
The following errors are returned by this function: ErrImportReadFailure, ErrLocaleNotRegistered, ErrInvalidRuleType, any error from the translator's Add(), AddCardinal(), AddOrdinal() or AddRange() functions
func (*UniversalTranslator) VerifyTranslations ¶
func (t *UniversalTranslator) VerifyTranslations() (err error)
VerifyTranslations runs through all locales and identifies any issues.
The following errors are returned by this function: any error from the translator's VerifyTranslations() function