1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 20:41:57 +00:00

add support for the go standard embed tag for locale files

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-23 01:28:47 +03:00
parent fd1db640a0
commit 4cd0621018
14 changed files with 230 additions and 57 deletions

View File

@@ -140,6 +140,21 @@ func (i *I18n) LoadAssets(assetNames func() []string, asset func(string) ([]byte
return i.Reset(Assets(assetNames, asset, i.Loader), languages...)
}
// LoadFS is a method shortcut to load files using `embed.FS` or `fs.FS` or
// `http.FileSystem` or `string` (local directory).
// With this method, all the embedded files into "sub" MUST be locale files.
//
// See `New` and `FS` package-level functions for more.
// Example: https://github.com/kataras/iris/blob/master/_examples/i18n/template-embedded/main.go.
func (i *I18n) LoadFS(fsOrDir interface{}, sub string, languages ...string) error {
loader, err := FS(fsOrDir, sub, i.Loader)
if err != nil {
return err
}
return i.Reset(loader, languages...)
}
// Reset sets the locales loader and languages.
// It is not meant to be used by users unless
// a custom `Loader` must be used instead of the default one.
@@ -474,7 +489,9 @@ func (i *I18n) setLangWithoutContext(w http.ResponseWriter, r *http.Request, lan
SameSite: http.SameSiteLaxMode,
})
} else if i.URLParameter != "" {
r.URL.Query().Set(i.URLParameter, lang)
q := r.URL.Query()
q.Set(i.URLParameter, lang)
r.URL.RawQuery = q.Encode()
}
r.Header.Set(acceptLanguageHeaderKey, lang)

View File

@@ -3,10 +3,12 @@ package i18n
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/i18n/internal"
"github.com/BurntSushi/toml"
@@ -41,11 +43,40 @@ func Glob(globPattern string, options LoaderConfig) Loader {
// and any Loader options. Go-bindata usage.
// It returns a valid `Loader` which loads and maps the locale files.
//
// See `Glob`, `Assets`, `New` and `LoaderConfig` too.
// See `Glob`, `FS`, `New` and `LoaderConfig` too.
func Assets(assetNames func() []string, asset func(string) ([]byte, error), options LoaderConfig) Loader {
return load(assetNames(), asset, options)
}
// LoadFS loads the files using embed.FS or fs.FS or
// http.FileSystem or string (local directory).
// With this method, all the embedded files into "sub" MUST be locale files.
//
// See `Glob`, `Assets`, `New` and `LoaderConfig` too.
func FS(fsOrDir interface{}, sub string, options LoaderConfig) (Loader, error) {
if sub == "" {
sub = "."
}
fileSystem := context.ResolveFS(fsOrDir)
assetNames, err := context.FindNames(fileSystem, sub)
if err != nil {
return nil, err
}
assetFunc := func(name string) ([]byte, error) {
f, err := fileSystem.Open(name)
if err != nil {
return nil, err
}
return io.ReadAll(f)
}
return load(assetNames, assetFunc, options), nil
}
// DefaultLoaderConfig represents the default loader configuration.
var DefaultLoaderConfig = LoaderConfig{
Left: "{{",