Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue reading resource from manage in function New of package gres #2961

Merged
merged 19 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion i18n/gi18n/gi18n_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ package gi18n

import "context"

// CtxLanguage is the context key for language name.
type CtxLanguage string
hailaz marked this conversation as resolved.
Show resolved Hide resolved

const (
ctxLanguage = "I18nLanguage"
ctxLanguage CtxLanguage = "I18nLanguage"
)

// WithLanguage append language setting to the context and returns a new context.
Expand Down
69 changes: 51 additions & 18 deletions i18n/gi18n/gi18n_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)

// PathType is the type for i18n file path.
type PathType string
hailaz marked this conversation as resolved.
Show resolved Hide resolved

const (
PathTypeNone PathType = ""
PathTypeNormal PathType = "normal"
PathTypeGres PathType = "gres"
)

// Manager for i18n contents, it is concurrent safe, supporting hot reload.
type Manager struct {
mu sync.RWMutex
Expand All @@ -33,9 +42,11 @@ type Manager struct {

// Options is used for i18n object configuration.
type Options struct {
Path string // I18n files storage path.
Language string // Default local language.
Delimiters []string // Delimiters for variable parsing.
Path string // I18n files storage path.
Language string // Default local language.
Delimiters []string // Delimiters for variable parsing.
pathType PathType // Path type for i18n files.
hailaz marked this conversation as resolved.
Show resolved Hide resolved
Resource *gres.Resource // Resource for i18n files.
}

var (
Expand Down Expand Up @@ -65,6 +76,9 @@ func New(options ...Options) *Manager {
if len(opts.Delimiters) == 0 {
opts.Delimiters = defaultDelimiters
}
if opts.Resource == nil {
opts.Resource = gres.Instance()
}
m := &Manager{
options: opts,
pattern: fmt.Sprintf(
Expand All @@ -79,38 +93,49 @@ func New(options ...Options) *Manager {

// DefaultOptions creates and returns a default options for i18n manager.
func DefaultOptions() Options {
var path string
opt := Options{
Language: defaultLanguage,
Delimiters: defaultDelimiters,
Resource: gres.Instance(),
}
for _, folder := range searchFolders {
path, _ = gfile.Search(folder)
if path != "" {
if opt.Resource.Contains(folder) {
opt.Path = folder
opt.pathType = PathTypeGres
break
}
hailaz marked this conversation as resolved.
Show resolved Hide resolved
opt.Path, _ = gfile.Search(folder)
if opt.Path != "" {
opt.pathType = PathTypeNormal
break
}
}
if path != "" {
if opt.Path != "" {
// To avoid of the source path of GoFrame: github.com/gogf/i18n/gi18n
if gfile.Exists(path + gfile.Separator + "gi18n") {
path = ""
if gfile.Exists(opt.Path + gfile.Separator + "gi18n") {
opt.Path = ""
}
}
return Options{
Path: path,
Language: "en",
Delimiters: defaultDelimiters,
}
return opt
}

// SetPath sets the directory path storing i18n files.
func (m *Manager) SetPath(path string) error {
if gres.Contains(path) {
if m.options.Resource.Contains(path) {
m.options.Path = path
m.options.pathType = PathTypeGres
} else {
realPath, _ := gfile.Search(path)
if realPath == "" {
return gerror.NewCodef(gcode.CodeInvalidParameter, `%s does not exist`, path)
}
m.options.Path = realPath
m.options.pathType = PathTypeNormal
}
intlog.Printf(context.TODO(), `SetPath: %s`, m.options.Path)
intlog.Printf(context.TODO(), `SetPath[%s]: %s`, m.options.pathType, m.options.Path)
m.mu.Lock()
m.data = nil
m.mu.Unlock()
return nil
}

Expand Down Expand Up @@ -201,17 +226,24 @@ func (m *Manager) init(ctx context.Context) {
}
m.mu.RUnlock()

defer func() {
intlog.Printf(ctx, `Manager init finish: %#v`, m)
}()

intlog.Printf(ctx, `init path: %s`, m.options.Path)

m.mu.Lock()
defer m.mu.Unlock()
if gres.Contains(m.options.Path) {
files := gres.ScanDirFile(m.options.Path, "*.*", true)
if m.options.Resource.Contains(m.options.Path) {
files := m.options.Resource.ScanDirFile(m.options.Path, "*.*", true)
if len(files) > 0 {
var (
path string
name string
lang string
array []string
)
m.options.pathType = PathTypeGres
m.data = make(map[string]map[string]string)
for _, file := range files {
name = file.Name()
Expand Down Expand Up @@ -244,6 +276,7 @@ func (m *Manager) init(ctx context.Context) {
lang string
array []string
)
m.options.pathType = PathTypeNormal
m.data = make(map[string]map[string]string)
for _, file := range files {
path = file[len(m.options.Path)+1:]
Expand Down
44 changes: 39 additions & 5 deletions i18n/gi18n/gi18n_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package gi18n_test

import (
"github.com/gogf/gf/v2/encoding/gbase64"
"github.com/gogf/gf/v2/os/gctx"
_ "github.com/gogf/gf/v2/os/gres/testdata/data"

"context"
"testing"
Expand Down Expand Up @@ -123,7 +123,7 @@ func Test_Instance(t *testing.T) {
gres.Dump()
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance()
err := m.SetPath("i18n-dir")
err := m.SetPath(gtest.DataPath("i18n-dir"))
t.AssertNil(err)
m.SetLanguage("zh-CN")
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
Expand All @@ -141,15 +141,15 @@ func Test_Instance(t *testing.T) {
// Default language is: en
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance(gconv.String(gtime.TimestampNano()))
m.SetPath("i18n-dir")
m.SetPath(gtest.DataPath("i18n-dir"))
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "HelloWorld")
})
}

func Test_Resource(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := g.I18n("resource")
err := m.SetPath("i18n-dir")
err := m.SetPath(gtest.DataPath("i18n-dir"))
t.AssertNil(err)

m.SetLanguage("none")
Expand Down Expand Up @@ -180,8 +180,42 @@ func Test_SetCtxLanguage(t *testing.T) {
})

gtest.C(t, func(t *gtest.T) {
ctx := gi18n.WithLanguage(nil, "zh-CN")
ctx := gi18n.WithLanguage(context.Background(), "zh-CN")
t.Assert(gi18n.LanguageFromCtx(ctx), "zh-CN")
})

}

func Test_PathInResource(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
binContent, err := gres.Pack(gtest.DataPath("i18n"))
t.AssertNil(err)
err = gres.Add(gbase64.EncodeToString(binContent))
t.AssertNil(err)

m := gi18n.New()
m.SetLanguage("zh-CN")
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")

m.SetPath("i18n")
gi18n.SetLanguage("ja")
t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
})
}

func Test_PathInNormal(t *testing.T) {
// Copy i18n files to current directory.
gfile.CopyDir(gtest.DataPath("i18n"), gfile.Join(gdebug.CallerDirectory(), "manifest/i18n"))
// Remove copied files after testing.
defer gfile.Remove(gfile.Join(gdebug.CallerDirectory(), "manifest"))

gtest.C(t, func(t *gtest.T) {
m := gi18n.New()
m.SetLanguage("zh-CN")
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")

m.SetPath("i18n")
gi18n.SetLanguage("ja")
t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
})
}
hailaz marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion i18n/gi18n/testdata/i18n-dir/zh-CN/hello.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"hello": "你好"
"你好": "hello",
"hello": "你好"
}
1 change: 1 addition & 0 deletions i18n/gi18n/testdata/i18n-dir/zh-CN/world.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"世界": "world",
"world": "世界"
}