Documentation
¶
Index ¶
- Variables
- type DefaultLocaleContextHolder
- type DefaultLocaleMessageBundle
- func (this *DefaultLocaleMessageBundle) AddCardinal(key, text string, rule PluralRule)
- func (this *DefaultLocaleMessageBundle) AddOrdinal(key, text string, rule PluralRule)
- func (this *DefaultLocaleMessageBundle) AddRange(key, text string, rule PluralRule)
- func (this *DefaultLocaleMessageBundle) AddTranslation(key, text string)
- func (this *DefaultLocaleMessageBundle) Cardinal(key string, num float64) (string, error)
- func (this *DefaultLocaleMessageBundle) CardinalWithPrecision(key string, num float64, precision uint64) (string, error)
- func (this *DefaultLocaleMessageBundle) LoadLocalizedMessages(messages LocalizedMessages)
- func (this *DefaultLocaleMessageBundle) LoadValidationTranslation(translation ValidationTranslationFunction) error
- func (this DefaultLocaleMessageBundle) New(locale Locale, messageSource MessageSource) *DefaultLocaleMessageBundle
- func (this *DefaultLocaleMessageBundle) Ordinal(key string, num float64) (string, error)
- func (this *DefaultLocaleMessageBundle) OrdinalWithPrecision(key string, num float64, precision uint64) (string, error)
- func (this *DefaultLocaleMessageBundle) Range(key string, num1, num2 float64) (string, error)
- func (this *DefaultLocaleMessageBundle) RangeWithPrecision(key string, num1, num2 float64, precision uint64) (string, error)
- func (this *DefaultLocaleMessageBundle) Translate(key string, params ...string) (string, error)
- type DefaultMessageSource
- func (this *DefaultMessageSource) DefaultMessageBundle() LocaleMessageBundle
- func (this *DefaultMessageSource) FindMessageBundle(locale Locale, locales ...Locale) LocaleMessageBundle
- func (this *DefaultMessageSource) FindTranslator(locale Locale, locales ...Locale) Translator
- func (this DefaultMessageSource) New(defaultLocale Locale) *DefaultMessageSource
- func (this *DefaultMessageSource) NewMessageBundle(locale Locale) LocaleMessageBundle
- func (this *DefaultMessageSource) UniversalTranslator() *ut.UniversalTranslator
- func (this *DefaultMessageSource) Validator() *validator.Validate
- type DefaultMessageSourceValidator
- func (this DefaultMessageSourceValidator) New(contextHolder LocaleContextHolder, messageSource MessageSource) *DefaultMessageSourceValidator
- func (this *DefaultMessageSourceValidator) TranslateValidationErrors(validationErrors validator.ValidationErrors, namespace ...string) validator.ValidationErrorsTranslations
- func (this *DefaultMessageSourceValidator) Validate(data any) error
- type Locale
- type LocaleContextHolder
- type LocaleMessageBundle
- type LocalizedMessages
- type MessageSource
- type MessageSourceValidator
- type PluralRule
- type PluralRuleEnum
- type ThreadLocaleContextHolder
- type Translator
- type ValidationTranslationFunction
Constants ¶
This section is empty.
Variables ¶
var PluralRules = PluralRule{}.New()
PluralRules is an initialized PluralRuleEnum variable.
Functions ¶
This section is empty.
Types ¶
type DefaultLocaleContextHolder ¶
type DefaultLocaleContextHolder struct {
// contains filtered or unexported fields
}
func (*DefaultLocaleContextHolder) Clear ¶
func (this *DefaultLocaleContextHolder) Clear()
Clear() implements LocaleContextHolder.Clear.
func (*DefaultLocaleContextHolder) Get ¶
func (this *DefaultLocaleContextHolder) Get() Locale
Get() implements LocaleContextHolder.Get.
func (DefaultLocaleContextHolder) New ¶
func (this DefaultLocaleContextHolder) New(defaultLococale Locale) *DefaultLocaleContextHolder
func (*DefaultLocaleContextHolder) Set ¶
func (this *DefaultLocaleContextHolder) Set(locale Locale)
Set() implements LocaleContextHolder.Set.
type DefaultLocaleMessageBundle ¶
type DefaultLocaleMessageBundle struct {
// contains filtered or unexported fields
}
DefaultLocaleMessageBundle is the default implementation of LocaleMessageBundle
func (*DefaultLocaleMessageBundle) AddCardinal ¶
func (this *DefaultLocaleMessageBundle) AddCardinal(key, text string, rule PluralRule)
AddCardinal() implements LocaleMessageBundle.AddCardinal
func (*DefaultLocaleMessageBundle) AddOrdinal ¶
func (this *DefaultLocaleMessageBundle) AddOrdinal(key, text string, rule PluralRule)
AddOrdinal() implements LocaleMessageBundle.AddOrdinal
func (*DefaultLocaleMessageBundle) AddRange ¶
func (this *DefaultLocaleMessageBundle) AddRange(key, text string, rule PluralRule)
AddRange() implements LocaleMessageBundle.AddRange
func (*DefaultLocaleMessageBundle) AddTranslation ¶
func (this *DefaultLocaleMessageBundle) AddTranslation(key, text string)
AddTranslation() implements LocaleMessageBundle.AddTranslation
func (*DefaultLocaleMessageBundle) Cardinal ¶
func (this *DefaultLocaleMessageBundle) Cardinal(key string, num float64) (string, error)
Cardinal() implements LocaleMessageBundle.Cardinal
func (*DefaultLocaleMessageBundle) CardinalWithPrecision ¶
func (this *DefaultLocaleMessageBundle) CardinalWithPrecision(key string, num float64, precision uint64) (string, error)
CardinalWithPrecision() implements LocaleMessageBundle.CardinalWithPrecision
func (*DefaultLocaleMessageBundle) LoadLocalizedMessages ¶
func (this *DefaultLocaleMessageBundle) LoadLocalizedMessages(messages LocalizedMessages)
LoadLocalizedMessages() implements LocaleMessageBundle.LoadLocalizedMessages
func (*DefaultLocaleMessageBundle) LoadValidationTranslation ¶
func (this *DefaultLocaleMessageBundle) LoadValidationTranslation(translation ValidationTranslationFunction) error
LoadValidationTranslation() implements LocaleMessageBundle.LoadValidationTranslation
func (DefaultLocaleMessageBundle) New ¶
func (this DefaultLocaleMessageBundle) New(locale Locale, messageSource MessageSource) *DefaultLocaleMessageBundle
New() returns a new DefaultLocaleMessageBundle
func (*DefaultLocaleMessageBundle) Ordinal ¶
func (this *DefaultLocaleMessageBundle) Ordinal(key string, num float64) (string, error)
Ordinal() implements LocaleMessageBundle.Ordinal
func (*DefaultLocaleMessageBundle) OrdinalWithPrecision ¶
func (this *DefaultLocaleMessageBundle) OrdinalWithPrecision(key string, num float64, precision uint64) (string, error)
OrdinalWithPrecision() implements LocaleMessageBundle.OrdinalWithPrecision
func (*DefaultLocaleMessageBundle) Range ¶
func (this *DefaultLocaleMessageBundle) Range(key string, num1, num2 float64) (string, error)
Range() implements LocaleMessageBundle.Range
func (*DefaultLocaleMessageBundle) RangeWithPrecision ¶
func (this *DefaultLocaleMessageBundle) RangeWithPrecision(key string, num1, num2 float64, precision uint64) (string, error)
RangeWithPrecision() implements LocaleMessageBundle.RangeWithPrecision
func (*DefaultLocaleMessageBundle) Translate ¶
func (this *DefaultLocaleMessageBundle) Translate(key string, params ...string) (string, error)
Translate() implements LocaleMessageBundle.Translate
type DefaultMessageSource ¶
type DefaultMessageSource struct {
// contains filtered or unexported fields
}
DefaultMessageSource is the main object used to create / retrieve individual [LocalMessageBundle].
func (*DefaultMessageSource) DefaultMessageBundle ¶
func (this *DefaultMessageSource) DefaultMessageBundle() LocaleMessageBundle
DefaultMessageBundle() implements MessageSource.DefaultMessageBundle.
func (*DefaultMessageSource) FindMessageBundle ¶
func (this *DefaultMessageSource) FindMessageBundle(locale Locale, locales ...Locale) LocaleMessageBundle
FindMessageBundle() implements MessageSource.FindMessageBundle.
func (*DefaultMessageSource) FindTranslator ¶
func (this *DefaultMessageSource) FindTranslator(locale Locale, locales ...Locale) Translator
FindTranslator() implements MessageSource.FindTranslator.
func (DefaultMessageSource) New ¶
func (this DefaultMessageSource) New(defaultLocale Locale) *DefaultMessageSource
New() lets you create a new DefaultMessageSource with a "defaultLocale".
func (*DefaultMessageSource) NewMessageBundle ¶
func (this *DefaultMessageSource) NewMessageBundle(locale Locale) LocaleMessageBundle
NewMessageBundle() implements MessageSource.NewMessageBundle.
func (*DefaultMessageSource) UniversalTranslator ¶
func (this *DefaultMessageSource) UniversalTranslator() *ut.UniversalTranslator
UniversalTranslator() implements MessageSource.UniversalTranslator.
func (*DefaultMessageSource) Validator ¶
func (this *DefaultMessageSource) Validator() *validator.Validate
Validator() implements MessageSource.Validator.
type DefaultMessageSourceValidator ¶
type DefaultMessageSourceValidator struct {
// contains filtered or unexported fields
}
DefaultMessageSourceValidator is the default implementation of MessageSourceValidator, an object that can be used to validate a struct using the provided LocaleContextHolder and MessageSource.
It also implements [FuncMapper] to offer access to localization function in HTML templates.
func (DefaultMessageSourceValidator) New ¶
func (this DefaultMessageSourceValidator) New(contextHolder LocaleContextHolder, messageSource MessageSource) *DefaultMessageSourceValidator
New() construct a new MessageSourceValidator
func (*DefaultMessageSourceValidator) TranslateValidationErrors ¶
func (this *DefaultMessageSourceValidator) TranslateValidationErrors(validationErrors validator.ValidationErrors, namespace ...string) validator.ValidationErrorsTranslations
TranslateValidationErrors() implements MessageSourceValidator.TranslateValidationErrors.
func (*DefaultMessageSourceValidator) Validate ¶
func (this *DefaultMessageSourceValidator) Validate(data any) error
Validate() implements MessageSourceValidator.Validate.
type Locale ¶
type Locale = loc.Translator
Locale is a local alias for github.com/go-playground/locales.Translator
type LocaleContextHolder ¶
type LocaleMessageBundle ¶
type LocaleMessageBundle interface {
LoadLocalizedMessages(messages LocalizedMessages)
// AddValidationTranslation() let's you register a validation function.
//
// To be used with [github.com/go-playground/validator/v10/translations] modules.
//
// Example :
//
// import "github.com/go-playground/validator/v10/translations/en
//
// ...
//
// bundle.AddValidationTranslation(en.RegisterDefaultTranslations)
LoadValidationTranslation(translation ValidationTranslationFunction) error
// AddTranslation() adds a normal translation for this LocaleMessageBundle language/locale
// Multiple string parameters may be references (like "{0}", "{1}", ...)
//
// Example :
//
// bundle.AddTranslation("welcome": "Welcome back, {0}")
AddTranslation(key, text string)
// AddCardinal() adds a cardinal translation for this LocaleMessageBundle 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.
//
// Example :
//
// bundle.AddCardinal("days.left", "{0} day left", PluralRules.ONE)
// bundle.AddCardinal("days.left", "{0} days left", PluralRules.OTHER)
AddCardinal(key, text string, rule PluralRule)
// AddOrdinal() adds an ordinal translation for this LocaleMessageBundle 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.
//
// Example :
//
// bundle.AddOrdinal("spring_days", "{0}st day of spring", PluralRules.ONE)
// bundle.AddOrdinal("spring_days", "{0}nd day of spring", PluralRules.TWO)
// bundle.AddOrdinal("spring_days", "{0}rd day of spring", PluralRules.FEW)
// bundle.AddOrdinal("spring_days", "{0}th day of spring", PluralRules.OTHER)
AddOrdinal(key, text string, rule PluralRule)
// AddRange() adds a range translation for this LocaleMessageBundle language/locale.
// {0} and {1} are the only replacement types accepted and only these are accepted.
//
// Example :
//
// bundle.AddRange('days_left', "{0}-{1} day left", PluralRules.ONE)
// bundle.AddRange('days_left', "{0}-{1} days left", PluralRules.OTHER)
AddRange(key, text string, rule PluralRule)
// Translate() creates the translation for the locale given the 'key' and params passed in
Translate(key string, params ...string) (string, error)
// Cardinal() creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments
// and param passed in
Cardinal(key string, num float64) (string, error)
// CardinalWithPrecision() creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments
// and param passed in.
CardinalWithPrecision(key string, num float64, precision uint64) (string, error)
// Ordinal() creates the ordinal translation for the locale given the 'key', 'num' and 'digit' arguments
// and param passed in.
Ordinal(key string, num float64) (string, error)
// OrdinalWithPrecision() creates the ordinal translation for the locale given the 'key', 'num' and 'digit' arguments
// and param passed in.
OrdinalWithPrecision(key string, num float64, precision uint64) (string, error)
// Range() creates the range translation for the locale given the 'key', 'num1', 'digit1', 'num2' and
// 'digit2' arguments and 'param1' and 'param2' passed in.
Range(key string, num1, num2 float64) (string, error)
// RangeWithPrecision() creates the range translation for the locale given the 'key', 'num1', 'digit1', 'num2' and
// 'digit2' arguments and 'param1' and 'param2' passed in.
RangeWithPrecision(key string, num1, num2 float64, precision uint64) (string, error)
}
LocaleMessageBundle is an interface to an object that let you define Locale messages and apply them, depending on the context.
LocaleMessageBundle should be constructed through a single DefaultMessageSource object.
Each locale messages can be loaded using its methods or through the [LocalizedMessages.Load] method.
type LocalizedMessages ¶
type LocalizedMessages struct {
// contains filtered or unexported fields
}
LocalizedMessages is a type that can be used in a github.com/BeamFoundry/rays/pkg/core.ConfigurationProperties to load locale messages
For example :
type LocalizationProperties struct {
ConfigurationProperties `Name:"localization"`
messages map[string]LocalizedMessages
}
This can load the following YAML example :
localization:
messages:
en:
translation: # map[string]string
welcome: Welcome
cardinal: # map[string]map[PluralRule]string
days.left: # map[PluralRule]string
ONE: {0} day remaining
OTHER: {0} days remaining
ordinal: # map[string]map[PluralRule]string
price: # map[PluralRule]string
ONE: {0}st price
TWO: {0}nd price
FEW: {0}rd price
OTHER: {0}th price
ranges: # map[string]map[PluralRule]string
days.range: # map[PluralRule]string
OTHER: {0}-{1} days
It is to simplify loading messages into a LocaleMessageBundle.
type MessageSource ¶
type MessageSource interface {
// DefaultMessageBundle() lets you retrieve the default configured [LocaleMessageBundle]
DefaultMessageBundle() LocaleMessageBundle
// NewMessageBundle() add a new [LocalMessageBundle] for the desired [Locale]
NewMessageBundle(locale Locale) LocaleMessageBundle
// FindMessageBundle() let's you retrieve the first available [LocaleMessageBundle] from the locale/locales parameters,
// or returns the "defaultLocale" [LocaleMessageBundle] if none match.
FindMessageBundle(locale Locale, locales ...Locale) LocaleMessageBundle
// FindTranslator() let's you retrieve the first available [Translator] from the locale/locales parameters,
// or returns the "defaultLocale" [Translator] if none match.
FindTranslator(locale Locale, locales ...Locale) Translator
// UniversalTranslator() returns the [github.com/go-playground/universal-translator.UniversalTranslator].
UniversalTranslator() *ut.UniversalTranslator
// Validator() returns the [github.com/go-playground/validator/v10.Validator].
Validator() *validator.Validate
}
MessageSource is an interface used to define and retrieve LocaleMessageBundle.
It is the central component to this localization and validation engine, as it contains the central validator.Validate and ut.UniversalTranslator pointers.
Implementations should register a default Locale to create a default LocaleMessageBundle. Further call to MessageSource.NewMessageBundle should be used the configure other LocaleMessageBundle
type MessageSourceValidator ¶
type MessageSourceValidator interface {
// Validate() implements the validation function.
Validate(data any) error
// TranslateValidationErrors() will help ou translate any validation errors,
// using the current locale obtain from the LocaleContextHolder.
TranslateValidationErrors(validationErrors validator.ValidationErrors, namespace ...string) validator.ValidationErrorsTranslations
}
MessageSourceValidator is an interface representing an object that can be used to validate a struct and translate validation errors using the current Locale as defined in the ThreadLocaleContextHolder.
MessageSourceValidator.Validate if used with a predefined struct (ie. not an anonymous one), it will recreate one using reflection to insure the error messages are standardized.
NOTE: It will be changed in a future release.
MessageSourceValidator.TranslateValidationErrors may optionally use a "namespace" prefix to search field name translation for the current Locale. It will be used to combine it, with the fields name (which can be overridden using the struct tag "field"), to return the validation error message with the localized field's name.
This is specially designed to return to web users form validation errors using the apparent field's displayed label, not the HTML form input name.
For examole for an english Locale, with the following locales defined is configuration properties :
localization:
messages:
en:
translation:
registration.firstname: First Name
registration.lastname: Last Name
For the following struct where you have binded the current HTTP Post request to the following struct:
struct {
Firstname string `form:"firstname" field:"firstname" validate:"required"`
Lastname string `form:"lastname" field:"lastname" validate:"required"`
}
If any of the fields does not validate, a call to this function using a namespace of "registration" will return the localized english message validation using either 'First Name' or 'Last Name', instead of the form fields name ("Firstname"/"firstname" or "Lastname"/"lastname").
type PluralRule ¶
type PluralRule = Enum[PluralRuleEnum]
PluralRule is a Enum alias for PluralRuleEnum.
See github.com/BeamFoundry/rays/pkg/lang.Enum on how to use this type.
type PluralRuleEnum ¶
type PluralRuleEnum struct {
ZERO,
ONE,
TWO,
FEW,
MANY,
OTHER PluralRule
}
PluralRuleEnum is an Enum struct based on locales.PluralRule possible values.
See github.com/BeamFoundry/rays/pkg/lang.Enum on how to use this type.
type ThreadLocaleContextHolder ¶
type ThreadLocaleContextHolder struct {
// contains filtered or unexported fields
}
ThreadLocaleContextHolder is an implementation of LocaleContextHolder that can be used in a http middleware and in an http request handler function to set the current Locale to use (based on cookie, query parameter) ...) and retrieve it.
It is to be registred in a [Configuration] object.
It's based on routine.ThreadLocal.
func (*ThreadLocaleContextHolder) Clear ¶
func (this *ThreadLocaleContextHolder) Clear()
Clear() implements LocaleContextHolder.Clear.
It should be called in a http middleware first before calling Set() or Get(), as it might returns a previously used thread value.
func (*ThreadLocaleContextHolder) Get ¶
func (this *ThreadLocaleContextHolder) Get() Locale
Get() implements LocaleContextHolder.Get.
func (ThreadLocaleContextHolder) New ¶
func (this ThreadLocaleContextHolder) New(defautLocale Locale) *ThreadLocaleContextHolder
New() creates a new LocaleContextHolder
func (*ThreadLocaleContextHolder) Set ¶
func (this *ThreadLocaleContextHolder) Set(locale Locale)
Set() implements LocaleContextHolder.Set.
type Translator ¶
type Translator = ut.Translator
Translator is a local alias for github.com/go-playground/universal-translator.Translator
type ValidationTranslationFunction ¶
type ValidationTranslationFunction func(*validator.Validate, Translator) (err error)
ValidationTranslationFunction is a type of function used to register a Translator in the validator.Validate object in a LocaleMessageBundle.