Documentation ¶
Index ¶
- func SetDefaultFieldName(field, name string)
- func SetDefaultLine(key, line string)
- func SetDefaultValidationRule(key, line string)
- type Language
- type Languages
- func (l *Languages) DetectLanguage(lang string) *Language
- func (l *Languages) Get(lang string, line string, placeholders ...string) string
- func (l *Languages) GetAvailableLanguages() []string
- func (l *Languages) GetDefault() *Language
- func (l *Languages) GetLanguage(lang string) *Language
- func (l *Languages) IsAvailable(lang string) bool
- func (l *Languages) Load(language, path string) error
- func (l *Languages) LoadAllAvailableLanguages() error
- func (l *Languages) LoadDirectory(directory string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultFieldName ¶
func SetDefaultFieldName(field, name string)
SetDefaultFieldName set the field name used in validation error message placeholders for the given field in the default "en-US" language. Values set this way can be overridden by language files.
func SetDefaultLine ¶
func SetDefaultLine(key, line string)
SetDefaultLine set the language line identified by the given key in the default "en-US" language. Values set this way can be overridden by language files.
func SetDefaultValidationRule ¶
func SetDefaultValidationRule(key, line string)
SetDefaultValidationRule set the validation error message for the rule identified by the given key in the default "en-US" language. Values set this way can be overridden by language files.
Types ¶
type Language ¶
type Language struct {
// contains filtered or unexported fields
}
Language represents a full Language.
func (*Language) Get ¶
Get a language line.
For validation rules messages and field names, use a dot-separated path:
- "validation.rules.<rule_name>"
- "validation.fields.<field_name>"
For normal lines, just use the name of the line. Note that if you have a line called "validation", it won't conflict with the dot-separated paths.
If not found, returns the exact "line" argument.
The placeholders parameter is a variadic associative slice of placeholders and their replacement. In the following example, the placeholder ":username" will be replaced with the Name field in the user struct.
lang.Get("greetings", ":username", user.Name)
type Languages ¶
type Languages struct { Default string // contains filtered or unexported fields }
Languages container for all loaded languages.
This structure is not protected for concurrent usage. Therefore, don't load more languages when this instance is expected to receive reads.
func New ¶
func New() *Languages
New create a `Languages` with preloaded default language "en-US".
The default language can be replaced by modifying the `Default` field in the returned struct.
func (*Languages) DetectLanguage ¶
DetectLanguage detects the language to use based on the given lang string. The given lang string can use the HTTP "Accept-Language" header format.
If "*" is provided, the default language will be used. If multiple languages are given, the first available language will be used, and if none are available, the default language will be used. If no variant is given (for example "en"), the first available variant will be used.
func (*Languages) Get ¶
Get a language line.
For validation rules messages and field names, use a dot-separated path:
- "validation.rules.<rule_name>"
- "validation.fields.<field_name>"
For normal lines, just use the name of the line. Note that if you have a line called "validation", it won't conflict with the dot-separated paths.
If not found, returns the exact "line" argument.
The placeholders parameter is a variadic associative slice of placeholders and their replacement. In the following example, the placeholder ":username" will be replaced with the Name field in the user struct.
lang.Get("en-US", "greetings", ":username", user.Name)
func (*Languages) GetAvailableLanguages ¶
GetAvailableLanguages returns a slice of all loaded languages. This can be used to generate different routes for all languages supported by your applications.
/en/products /fr/produits ...
func (*Languages) GetDefault ¶
GetDefault is an alias for `l.GetLanguage(l.Default)`
func (*Languages) GetLanguage ¶
GetLanguage returns a language by its name. If the language is not available, returns a dummy language that will always return the entry name.
func (*Languages) IsAvailable ¶
IsAvailable returns true if the language is available.
func (*Languages) Load ¶
Load a language directory.
Directory structure of a language directory:
en-UK ├─ locale.json (contains the normal language lines) ├─ rules.json (contains the validation messages) └─ fields.json (contains the field names)
Each file is optional.
func (*Languages) LoadAllAvailableLanguages ¶
LoadAllAvailableLanguages loads every language directory in the "resources/lang" directory if it exists.
func (*Languages) LoadDirectory ¶
LoadDirectory loads every language directory in the given directory if it exists.