lang

package
v5.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = enUS

Default language used if no language is provided in a feature's options. By default, equals to the built-in "en-US" language. Although it can be replaced, writing to this variable is not concurrently safe.

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

func (l *Language) Get(line string, placeholders ...string) string

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)

func (*Language) Name

func (l *Language) Name() string

Name returns the name of the language. For example "en-US".

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

func (l *Languages) DetectLanguage(lang string) *Language

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

func (l *Languages) Get(lang string, line string, placeholders ...string) string

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

func (l *Languages) 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 (*Languages) GetDefault

func (l *Languages) GetDefault() *Language

GetDefault is an alias for `l.GetLanguage(l.Default)`

func (*Languages) GetLanguage

func (l *Languages) GetLanguage(lang string) *Language

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

func (l *Languages) IsAvailable(lang string) bool

IsAvailable returns true if the language is available.

func (*Languages) Load

func (l *Languages) Load(fs fsutil.FS, language, path string) error

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

func (l *Languages) LoadAllAvailableLanguages(fs fsutil.FS) error

LoadAllAvailableLanguages loads every available language directory. If the given FS implements `fsutil.WorkingDirFS`, the directory used will be "<working directory>/resources/lang".

func (*Languages) LoadDirectory

func (l *Languages) LoadDirectory(fs fsutil.FS, directory string) error

LoadDirectory loads every language directory in the given directory if it exists.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL