goyave: github.com/System-Glitch/goyave/lang Index | Files

package lang

import "github.com/System-Glitch/goyave/lang"

Index

Package Files

defaults.go lang.go

func DetectLanguage Uses

func DetectLanguage(lang string) string

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 Uses

func Get(lang string, line string, placeholders ...string) string

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 Uses

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 Uses

func IsAvailable(lang string) bool

IsAvailable returns true if the language is available.

func Load Uses

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)
  └─ attributes.json (contains the attribute-specific validation messages)

Each file is optional.

func LoadAllAvailableLanguages Uses

func LoadAllAvailableLanguages()

LoadAllAvailableLanguages loads every language directory in the "resources/lang" directory if it exists.

func LoadDefault Uses

func LoadDefault()

LoadDefault load the fallback language ("en-US"). This function is intended for internal use only.

Package lang imports 9 packages (graph). Updated 2020-10-24. Refresh now. Tools for package owners.