Documentation ¶
Overview ¶
Package i18n is responsible for keeping the key internationalization in one place.
Index ¶
- Constants
- func ExpandKey(ctx context.Context, key string) string
- func N(ctx context.Context, key string, n int, args ...any) string
- func T(ctx context.Context, key string, args ...any) string
- func WithScope(ctx context.Context, key string) context.Context
- type Code
- type DefaultText
- type Dict
- type Locale
- type Locales
- type M
- type PluralRule
Constants ¶
const (
DefaultRuleKey = "default"
)
Standard pluralization rule keys.
Variables ¶
This section is empty.
Functions ¶
func ExpandKey ¶ added in v0.3.0
ExpandKey extracts the current scope from the context and appends it to the start of the provided key.
Types ¶
type Code ¶
type Code string
Code is used to represent a language code which follows the ISO 639-1 standard, with sub-tags aggregated with hyphens, as defined in [RFC5646](https://datatracker.ietf.org/doc/html/rfc5646). Examples include:
fr, en-US, es-419, az-Arab, x-pig-latin, man-Nkoo-GN
func ParseAcceptLanguage ¶
ParseAcceptLanguage provides an ordered set of codes extracted from an HTTP "Accept-Language" header as defined in RFC9110. Current implementation will ignore quality values and instead just assume the order of the provided codes is valid.
type DefaultText ¶ added in v0.4.0
type DefaultText string
DefaultText when detected as an argument to a translation function will be used if no language match is found.
func Default ¶ added in v0.4.0
func Default(txt string) DefaultText
Default when used as an argument to a translation function ensure the provided txt is used as a default value if no language match is found.
type Dict ¶
type Dict struct {
// contains filtered or unexported fields
}
Dict holds the internationalization entries for a specific locale.
func (*Dict) Merge ¶
Merge combines the entries of the second dictionary into this one. If a key is duplicated in the second diction, the original value takes priority.
func (*Dict) UnmarshalJSON ¶
UnmarshalJSON attempts to load the dictionary data from a JSON byte slice.
type Locale ¶
type Locale struct {
// contains filtered or unexported fields
}
Locale holds the internationalization entries for a specific locale.
func (*Locale) N ¶
N uses the locale pluralization rules to determine which string value to provide based on the provided number.
func (*Locale) PluralRule ¶
func (l *Locale) PluralRule() PluralRule
PluralRule provides the pluralization rule for the locale.
func (*Locale) UnmarshalJSON ¶
UnmarshalJSON attempts to load the locale from a JSON byte slice.
type Locales ¶
type Locales struct {
// contains filtered or unexported fields
}
Locales is a map of language keys to their respective locale.
func (*Locales) Load ¶
Load walks through all the files in the provided File System and merges every one with the current list of locales.
func (*Locales) LoadWithDefault ¶ added in v0.5.0
LoadWithDefault performs the regular load operation, but follows up with a second operation that will ensure that default dictionary is merged with every other locale, thus ensuring that every text will have a fallback.
func (*Locales) Match ¶
Match attempts to find the best possible matching locale based on the locale string provided. The locale string is parsed according to the "Accept-Language" header format defined in RFC9110.
func (*Locales) UnmarshalJSON ¶
UnmarshalJSON attempts to load the locales from a JSON byte slice and merge them into any existing locales.
type M ¶
M stands for map and is a simple helper to make it easier to work with internationalization maps.
type PluralRule ¶
PluralRule defines a simple method that expects a dictionary and number and will find a matching dictionary entry.
func GetRule ¶
func GetRule(key string) PluralRule
GetRule provides the PluralRule for the given key.