1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-11 05:55:57 +00:00

i18n: expose the LoaderConfig from the main i18n instance and add an example

Signed-off-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com>
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-10 05:17:03 +03:00
parent b77227a0f9
commit 777ef0cd3e
8 changed files with 134 additions and 23 deletions

View File

@@ -49,6 +49,7 @@ type I18n struct {
localizer Localizer
matcher *Matcher
Loader *LoaderConfig
loader Loader
mu sync.Mutex
@@ -106,11 +107,18 @@ func makeTags(languages ...string) (tags []language.Tag) {
// New returns a new `I18n` instance. Use its `Load` or `LoadAssets` to load languages.
func New() *I18n {
return &I18n{
i := &I18n{
Loader: &LoaderConfig{
Left: "{{",
Right: "}}",
Strict: false,
},
URLParameter: "lang",
Subdomain: true,
PathRedirect: true,
}
return i
}
// Load is a method shortcut to load files using a filepath.Glob pattern.
@@ -118,7 +126,7 @@ func New() *I18n {
//
// See `New` and `Glob` package-level functions for more.
func (i *I18n) Load(globPattern string, languages ...string) error {
return i.Reset(Glob(globPattern), languages...)
return i.Reset(Glob(globPattern, i.Loader), languages...)
}
// LoadAssets is a method shortcut to load files using go-bindata.
@@ -126,7 +134,7 @@ func (i *I18n) Load(globPattern string, languages ...string) error {
//
// See `New` and `Asset` package-level functions for more.
func (i *I18n) LoadAssets(assetNames func() []string, asset func(string) ([]byte, error), languages ...string) error {
return i.Reset(Assets(assetNames, asset), languages...)
return i.Reset(Assets(assetNames, asset, i.Loader), languages...)
}
// Reset sets the locales loader and languages.

View File

@@ -21,20 +21,39 @@ import (
// some options about how the template loader should act.
//
// See `Glob` and `Assets` package-level functions.
type LoaderConfig struct {
// Template delimeters, defaults to {{ }}.
Left, Right string
// Template functions map, defaults to nil.
FuncMap template.FuncMap
// If true then it will return error on invalid templates instead of moving them to simple string-line keys.
// Also it will report whether the registered languages matched the loaded ones.
// Defaults to false.
Strict bool
}
type (
LoaderConfig struct {
// Template delimeters, defaults to {{ }}.
Left, Right string
// Template functions map, defaults to nil.
FuncMap template.FuncMap
// If true then it will return error on invalid templates instead of moving them to simple string-line keys.
// Also it will report whether the registered languages matched the loaded ones.
// Defaults to false.
Strict bool
}
// LoaderOption is a type which accepts a pointer to `LoaderConfig`
// and can be optionally passed to the second
// variadic input argument of the `Glob` and `Assets` functions.
LoaderOption interface {
Apply(*LoaderConfig)
}
)
// LoaderOption is a type which accepts a pointer to `LoaderConfig`
// and can be optionally passed to the second variadic input argument of the `Glob` and `Assets` functions.
type LoaderOption func(*LoaderConfig)
// Apply implements the `LoaderOption` interface.
func (c *LoaderConfig) Apply(cfg *LoaderConfig) {
for k, v := range c.FuncMap {
if cfg.FuncMap == nil {
cfg.FuncMap = make(template.FuncMap)
}
cfg.FuncMap[k] = v
}
cfg.Left = c.Left
cfg.Right = c.Right
cfg.Strict = c.Strict
}
// Glob accepts a glob pattern (see: https://golang.org/pkg/path/filepath/#Glob)
// and loads the locale files based on any "options".
@@ -73,10 +92,18 @@ func load(assetNames []string, asset func(string) ([]byte, error), options ...Lo
Left: "{{",
Right: "}}",
Strict: false,
FuncMap: template.FuncMap{
// get returns the value of a translate key, can be used inside other template keys
// to translate different words based on the current locale.
"tr": func(locale context.Locale, key string, args ...interface{}) string {
return locale.GetMessage(key, args...)
},
// ^ Alternative to {{call .tr "Dog" | plural }}
},
}
for _, opt := range options {
opt(&c)
opt.Apply(&c)
}
return func(m *Matcher) (Localizer, error) {
@@ -250,9 +277,11 @@ func (l *defaultLocale) getMessage(langInput, key string, args ...interface{}) s
// search on templates.
if tmpl, ok := l.templateKeys[key]; ok {
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, args[0]); err == nil {
return buf.String()
err := tmpl.Execute(buf, args[0])
if err != nil {
return err.Error()
}
return buf.String()
}
}