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(ctx context.Context, translator locales.Translator, override bool) error
- func (ut *UniversalTranslator) Export(ctx context.Context, path string) 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(ctx context.Context, path string) error
- func (ut *UniversalTranslator) ImportFromReader(ctx context.Context, reader io.Reader) 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" )
Types of translation rules.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (e *ErrExistingTranslator) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrExportPathFailure ¶
ErrExportPathFailure occurs when a failure is detected while creating the output path for exported trandlations.
func (*ErrExportPathFailure) Code ¶
func (e *ErrExportPathFailure) Code() int
Code returns the corresponding error code.
func (*ErrExportPathFailure) Error ¶
func (e *ErrExportPathFailure) Error() string
Error returns the string version of the error.
func (*ErrExportPathFailure) InternalError ¶
func (e *ErrExportPathFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrExportWriteFailure ¶
ErrExportWriteFailure occurs when a failure is detected while writing exported translations.
func (*ErrExportWriteFailure) Code ¶
func (e *ErrExportWriteFailure) Code() int
Code returns the corresponding error code.
func (*ErrExportWriteFailure) Error ¶
func (e *ErrExportWriteFailure) Error() string
Error returns the string version of the error.
func (*ErrExportWriteFailure) InternalError ¶
func (e *ErrExportWriteFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrImportPathFailure ¶
ErrImportPathFailure occurs when a failure is detected while opening the file or folder for importing translations.
func (*ErrImportPathFailure) Code ¶
func (e *ErrImportPathFailure) Code() int
Code returns the corresponding error code.
func (*ErrImportPathFailure) Error ¶
func (e *ErrImportPathFailure) Error() string
Error returns the string version of the error.
func (*ErrImportPathFailure) InternalError ¶
func (e *ErrImportPathFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrImportReadFailure ¶
ErrImportReadFailure occurs when a failure is detected while reading translations during import.
func (*ErrImportReadFailure) Code ¶
func (e *ErrImportReadFailure) Code() int
Code returns the corresponding error code.
func (*ErrImportReadFailure) Error ¶
func (e *ErrImportReadFailure) Error() string
Error returns the string version of the error.
func (*ErrImportReadFailure) InternalError ¶
func (e *ErrImportReadFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrInvalidRuleType ¶
type ErrInvalidRuleType struct {
RuleType string
}
ErrInvalidRuleType occurs when an invalid rule type is detected in the translation file.
func (*ErrInvalidRuleType) Code ¶
func (e *ErrInvalidRuleType) Code() int
Code returns the corresponding error code.
func (*ErrInvalidRuleType) Error ¶
func (e *ErrInvalidRuleType) Error() string
Error returns the string version of the error.
func (*ErrInvalidRuleType) InternalError ¶
func (e *ErrInvalidRuleType) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrKeyIsNotString ¶
type ErrKeyIsNotString struct { }
ErrKeyIsNotString occurs when a translation key is not a string.
func (*ErrKeyIsNotString) Code ¶
func (e *ErrKeyIsNotString) Code() int
Code returns the corresponding error code.
func (*ErrKeyIsNotString) Error ¶
func (e *ErrKeyIsNotString) Error() string
Error returns the string version of the error.
func (*ErrKeyIsNotString) InternalError ¶
func (e *ErrKeyIsNotString) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrLocaleNotRegistered ¶
type ErrLocaleNotRegistered struct {
Locale string
}
ErrLocaleNotRegistered occurs when a local is not registered with the translator instance.
func (*ErrLocaleNotRegistered) Code ¶
func (e *ErrLocaleNotRegistered) Code() int
Code returns the corresponding error code.
func (*ErrLocaleNotRegistered) Error ¶
func (e *ErrLocaleNotRegistered) Error() string
Error returns the string version of the error.
func (*ErrLocaleNotRegistered) InternalError ¶
func (e *ErrLocaleNotRegistered) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrMissingBrace ¶
ErrMissingBrace occurs when there is a missing brace in a translation. eg. This is a {0 <-- missing ending '}'
func (*ErrMissingBrace) Code ¶
func (e *ErrMissingBrace) Code() int
Code returns the corresponding error code.
func (*ErrMissingBrace) Error ¶
func (e *ErrMissingBrace) Error() string
Error returns the string version of the error.
func (*ErrMissingBrace) InternalError ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (e *ErrRangeTranslation) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrRegisterValidationTranslationFailure ¶
ErrRegisterValidationTranslationFailure occurs when a failure is detected while registering a validation tag's error message translation.
func (*ErrRegisterValidationTranslationFailure) Code ¶
func (e *ErrRegisterValidationTranslationFailure) Code() int
Code returns the corresponding error code.
func (*ErrRegisterValidationTranslationFailure) Error ¶
func (e *ErrRegisterValidationTranslationFailure) Error() string
Error returns the string version of the error.
func (*ErrRegisterValidationTranslationFailure) InternalError ¶
func (e *ErrRegisterValidationTranslationFailure) InternalError() error
InternalError returns the internal standard error object if there is one or nil if none is set.
type ErrUnknownTranslation ¶
type ErrUnknownTranslation struct {
Key string
}
ErrUnknownTranslation occurs when an unknown translation key is supplied.
func (*ErrUnknownTranslation) Code ¶
func (e *ErrUnknownTranslation) Code() int
Code returns the corresponding error code.
func (*ErrUnknownTranslation) Error ¶
func (e *ErrUnknownTranslation) Error() string
Error returns the string version of the error.
func (*ErrUnknownTranslation) InternalError ¶
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
NewUniversalTranslator returns a new UniversalTranslator instance set with the fallback locale and locales it should support.
func (*UniversalTranslator) AddTranslator ¶
func (t *UniversalTranslator) AddTranslator(ctx context.Context, 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.
The following errors are returned by this function: ErrExistingTranslator
func (*UniversalTranslator) Export ¶
func (ut *UniversalTranslator) Export(ctx context.Context, path string) 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(ctx context.Context, path string) 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