Documentation
¶
Index ¶
- func DetectLanguage(lang string) string
- func Get(lang string, line string, placeholders ...string) string
- func GetAvailableLanguages() []string
- func IsAvailable(lang string) bool
- func Load(language, path string)
- func LoadAllAvailableLanguages()
- func LoadDefault()
- func SetDefaultFieldName(field, name string)
- func SetDefaultLine(key, line string)
- func SetDefaultValidationRule(key, line string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func 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. For example, if "en-US" and "en-UK" are available and the request accepts "en", "en-US" will be used.
func Get ¶
Get a language line.
For validation rules and attributes messages, use a dot-separated path: - "validation.rules.<rule_name>" - "validation.fields.<field_name>" - "validation.fields.<field_name>.<rule_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" attribute.
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 GetAvailableLanguages ¶
func GetAvailableLanguages() []string
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 IsAvailable ¶
IsAvailable returns true if the language is available.
func Load ¶
func Load(language, path string)
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 attribute-specific validation messages)
Each file is optional.
func LoadAllAvailableLanguages ¶
func LoadAllAvailableLanguages()
LoadAllAvailableLanguages loads every language directory in the "resources/lang" directory if it exists.
func LoadDefault ¶
func LoadDefault()
LoadDefault load the fallback language ("en-US"). This function is intended for internal use only.
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 ¶
This section is empty.