mirror of
https://github.com/kataras/iris.git
synced 2026-01-22 11:25:59 +00:00
add LoadKV method on Iris.Application.I18N instance
This commit is contained in:
@@ -73,6 +73,70 @@ func FS(fileSystem fs.FS, pattern string, options LoaderConfig) (Loader, error)
|
||||
return load(assetNames, assetFunc, options), nil
|
||||
}
|
||||
|
||||
// LangMap key as language (e.g. "el-GR") and value as a map of key-value pairs (e.g. "hello": "Γειά").
|
||||
type LangMap = map[string]map[string]interface{}
|
||||
|
||||
// KV is a loader which accepts a map of language(key) and the available key-value pairs.
|
||||
// Example Code:
|
||||
//
|
||||
// m := i18n.LangMap{
|
||||
// "en-US": map[string]interface{}{
|
||||
// "hello": "Hello",
|
||||
// },
|
||||
// "el-GR": map[string]interface{}{
|
||||
// "hello": "Γειά",
|
||||
// },
|
||||
// }
|
||||
//
|
||||
// app := iris.New()
|
||||
// [...]
|
||||
// app.I18N.LoadKV(m)
|
||||
// app.I18N.SetDefault("en-US")
|
||||
func KV(langMap LangMap, opts ...LoaderConfig) Loader {
|
||||
return func(m *Matcher) (Localizer, error) {
|
||||
options := DefaultLoaderConfig
|
||||
if len(opts) > 0 {
|
||||
options = opts[0]
|
||||
}
|
||||
|
||||
languageIndexes := make([]int, 0, len(langMap))
|
||||
keyValuesMulti := make([]map[string]interface{}, 0, len(langMap))
|
||||
|
||||
for languageName, pairs := range langMap {
|
||||
langIndex := parseLanguageName(m, languageName) // matches and adds the language tag to m.Languages.
|
||||
languageIndexes = append(languageIndexes, langIndex)
|
||||
keyValuesMulti = append(keyValuesMulti, pairs)
|
||||
}
|
||||
|
||||
cat, err := internal.NewCatalog(m.Languages, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, langIndex := range languageIndexes {
|
||||
if langIndex == -1 {
|
||||
// If loader has more languages than defined for use in New function,
|
||||
// e.g. when New(KV(m), "en-US") contains el-GR and en-US but only "en-US" passed.
|
||||
continue
|
||||
}
|
||||
|
||||
kv := keyValuesMulti[langIndex]
|
||||
err := cat.Store(langIndex, kv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if n := len(cat.Locales); n == 0 {
|
||||
return nil, fmt.Errorf("locales not found in map")
|
||||
} else if options.Strict && n < len(m.Languages) {
|
||||
return nil, fmt.Errorf("locales expected to be %d but %d parsed", len(m.Languages), n)
|
||||
}
|
||||
|
||||
return cat, nil
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultLoaderConfig represents the default loader configuration.
|
||||
var DefaultLoaderConfig = LoaderConfig{
|
||||
Left: "{{",
|
||||
@@ -88,7 +152,7 @@ var DefaultLoaderConfig = LoaderConfig{
|
||||
// and any Loader options.
|
||||
// It returns a valid `Loader` which loads and maps the locale files.
|
||||
//
|
||||
// See `Glob`, `Assets` and `LoaderConfig` too.
|
||||
// See `FS`, `Glob`, `Assets` and `LoaderConfig` too.
|
||||
func load(assetNames []string, asset func(string) ([]byte, error), options LoaderConfig) Loader {
|
||||
return func(m *Matcher) (Localizer, error) {
|
||||
languageFiles, err := m.ParseLanguageFiles(assetNames)
|
||||
|
||||
Reference in New Issue
Block a user