Documentation
¶
Index ¶
- func DefaultPluralizationFunc(count int) string
- func ExecuteTemplate(templateStr string, data any) string
- type Config
- type Data
- type I18n
- func (t *I18n) AddLanguage(languageName string, translateStrings TranslateStrings) []string
- func (t *I18n) CheckLanguageConsistency(langNameToCheck string) (bool, []string)
- func (t *I18n) GetLanguages() *map[string]TranslateStrings
- func (t *I18n) HasLanguage(languageName string) bool
- func (t *I18n) NewLangTranslateFunc(languageName string) func(translateKey string, options ...Options) string
- func (t *I18n) NewTemplatingTranslateFunc() func(args ...interface{}) string
- func (t *I18n) SetPluralizationFunc(languageName string, fn PluralizationFunc)
- func (t *I18n) T(languageName string, translateKey string, options ...Options) string
- func (t *I18n) Translate(languageName string, translateKey string, options ...Options) string
- type Options
- type PluralizationFunc
- type TranslateString
- type TranslateStrings
- func LoadFromJsonBytes(jsonBytes []byte) (TranslateStrings, error)
- func LoadFromJsonFS(fileSystem fs.FS, filesOrGlobs ...string) (TranslateStrings, error)
- func LoadFromJsonFiles(filesOrGlobs ...string) (TranslateStrings, error)
- func LoadFromJsonString(jsonString string) (TranslateStrings, error)
- func LoadFromYamlBytes(yamlBytes []byte) (TranslateStrings, error)
- func LoadFromYamlFS(fileSystem fs.FS, filesOrGlobs ...string) (TranslateStrings, error)
- func LoadFromYamlFiles(filesOrGlobs ...string) (TranslateStrings, error)
- func LoadFromYamlString(yamlString string) (TranslateStrings, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultPluralizationFunc ¶
DefaultPluralizationFunc is the function that is used if no custom pluralization function is set for the language
func ExecuteTemplate ¶
Types ¶
type Config ¶
type Config struct { // Default: "en" FallbackLanguageName string // Default: false DisableConsistencyCheck bool }
Config is used to configure the i18n object
type I18n ¶
type I18n struct {
// contains filtered or unexported fields
}
func (*I18n) AddLanguage ¶
func (t *I18n) AddLanguage( languageName string, translateStrings TranslateStrings, ) []string
AddLanguage adds a language to the i18n object with its translations and after that it check if the language is consistent with the other languages (can be disabled with the config)
It returns a slice of errors as strings if the language is not consistent with the other languages and the consistency check is enabled
func (*I18n) CheckLanguageConsistency ¶
CheckLanguageConsistency checks if a language is consistent with the other languages, it checks if the translations keys are the same in all languages
func (*I18n) GetLanguages ¶
func (t *I18n) GetLanguages() *map[string]TranslateStrings
Return languages
func (*I18n) HasLanguage ¶
HasLanguage checks if a language is available (if is loaded)
func (*I18n) NewLangTranslateFunc ¶
func (t *I18n) NewLangTranslateFunc( languageName string, ) func( translateKey string, options ...Options, ) string
NewLangTranslateFunc creates a function to translate a string in a specific language without the need to pass the language name every time.
func (*I18n) NewTemplatingTranslateFunc ¶
NewTemplatingTranslateFunc creates a function that can be used in text/template and html/template. Just pass the created function to template.FuncMap.
Example:
tempTemplate := template.New("main").Funcs( template.FuncMap{ // 👇 "Translate" could be just "T" (for simplicity) or any other name you want. "Translate": i18n.NewTemplatingTranslateFunc(), }, )
Then you can use it in your template like this:
{{ Translate "lang" "en" "key" "hello_emails" "gender" "nonbinary" "count" "100" "SomeData" "Anything" }}
The format is key-value based and the order doesn't matter. This is the format:
{{ Translate "key1" "value1" "key2" "value2" ... }}
Arguments:
- "lang" "en": Language code (e.g., "en", "es").
- "key" "hello_emails": Translation key.
- "gender" "nonbinary": Gender for the translation (optional).
- "count" "100": Count for pluralization (optional).
- Additional key-value pairs will be added to the Data map.
Arguments are passed in pairs. The first item in each pair is the key, and the second is the value.
Key-Value Explanation:
- Each argument is processed as a pair: the first string is considered the key and the second string is the value.
- For example, in "lang" "en", "lang" is the key and "en" is the value.
As you can imagine, "lang", "key", "gender", and "count" are reserved keys. You can use any other key you want to pass data to translation.
Note: All arguments are strings. The function will attempt to convert "count" to an integer.
func (*I18n) SetPluralizationFunc ¶
func (t *I18n) SetPluralizationFunc(languageName string, fn PluralizationFunc)
SetPluralizationFunc sets the pluralization function for a language
type Options ¶
type Options struct { Data any Count *int Gender *string // male, female, nonbinary, non-binary (case insensitive) }
Options are the additional options for the Translate function
type PluralizationFunc ¶
type TranslateString ¶
type TranslateString struct { Key string Default string // For pluralization Zero string // Optional One string // Optional Two string // Optional Few string // Optional Many string // Optional // For genders Male string // Optional Female string // Optional NonBinary string // Optional // For pluralization with male gender ZeroMale string // Optional OneMale string // Optional TwoMale string // Optional FewMale string // Optional ManyMale string // Optional // For pluralization with female gender ZeroFemale string // Optional OneFemale string // Optional TwoFemale string // Optional FewFemale string // Optional ManyFemale string // Optional // For pluralization with non binary gender ZeroNonBinary string // Optional OneNonBinary string // Optional TwoNonBinary string // Optional FewNonBinary string // Optional ManyNonBinary string // Optional }
type TranslateStrings ¶
type TranslateStrings []TranslateString
func LoadFromJsonBytes ¶
func LoadFromJsonBytes( jsonBytes []byte, ) (TranslateStrings, error)
LoadFromJsonBytes loads a list of TranslateString from the provided JSON bytes.
func LoadFromJsonFS ¶
func LoadFromJsonFS( fileSystem fs.FS, filesOrGlobs ...string, ) (TranslateStrings, error)
LoadFromJsonFS loads a list of TranslateString from one or multiple JSON files located within a provided filesystem (fs.FS), allowing glob patterns like "path/to/files/*.json".
func LoadFromJsonFiles ¶
func LoadFromJsonFiles( filesOrGlobs ...string, ) (TranslateStrings, error)
LoadFromJsonFiles loads a list of TranslateString from one or multiple JSON files, allowing glob patterns like "path/to/files/*.json".
func LoadFromJsonString ¶
func LoadFromJsonString( jsonString string, ) (TranslateStrings, error)
LoadFromJsonString loads a list of TranslateString from the provided JSON string.
func LoadFromYamlBytes ¶
func LoadFromYamlBytes( yamlBytes []byte, ) (TranslateStrings, error)
LoadFromYamlBytes loads a list of TranslateString from the provided YAML bytes.
func LoadFromYamlFS ¶
func LoadFromYamlFS( fileSystem fs.FS, filesOrGlobs ...string, ) (TranslateStrings, error)
LoadFromYamlFS loads a list of TranslateString from one or multiple YAML files located within a provided filesystem (fs.FS), allowing glob patterns like "path/to/files/*.yaml".
func LoadFromYamlFiles ¶
func LoadFromYamlFiles( filesOrGlobs ...string, ) (TranslateStrings, error)
LoadFromYamlFiles loads a list of TranslateString from one or multiple YAML files, allowing glob patterns like "path/to/files/*.yaml".
func LoadFromYamlString ¶
func LoadFromYamlString( yamlString string, ) (TranslateStrings, error)
LoadFromYamlString loads a list of TranslateString from the provided YAML string.