1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 13:35:59 +00:00

add support for fs.FS, embed.FS (in addition of string and http.FileSystem) for i18n locales and view engine's templates

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-25 20:40:56 +03:00
parent 4cd0621018
commit 512ed6ffc0
29 changed files with 276 additions and 202 deletions

View File

@@ -4,6 +4,7 @@ package i18n
import (
"fmt"
"io/fs"
"net/http"
"os"
"strings"
@@ -142,12 +143,12 @@ func (i *I18n) LoadAssets(assetNames func() []string, asset func(string) ([]byte
// 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.
// The "pattern" is a classic glob pattern.
//
// 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)
func (i *I18n) LoadFS(fileSystem fs.FS, pattern string, languages ...string) error {
loader, err := FS(fileSystem, pattern, i.Loader)
if err != nil {
return err
}

View File

@@ -4,11 +4,11 @@ import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/i18n/internal"
"github.com/BurntSushi/toml"
@@ -50,17 +50,13 @@ func Assets(assetNames func() []string, asset func(string) ([]byte, error), opti
// 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.
// The "pattern" is a classic glob pattern.
//
// See `Glob`, `Assets`, `New` and `LoaderConfig` too.
func FS(fsOrDir interface{}, sub string, options LoaderConfig) (Loader, error) {
if sub == "" {
sub = "."
}
func FS(fileSystem fs.FS, pattern string, options LoaderConfig) (Loader, error) {
pattern = strings.TrimPrefix(pattern, "./")
fileSystem := context.ResolveFS(fsOrDir)
assetNames, err := context.FindNames(fileSystem, sub)
assetNames, err := fs.Glob(fileSystem, pattern)
if err != nil {
return nil, err
}