Skip to content

Commit

Permalink
session增加重置方法
Browse files Browse the repository at this point in the history
修改多语言替换和参数无关
  • Loading branch information
林孝义 committed Aug 14, 2024
1 parent 999f822 commit 5c0a9ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion i18n/gi18n/gi18n_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package gi18n
import (
"context"
"fmt"
"github.com/gogf/gf/v2/text/gstr"
"strings"
"sync"

Expand Down Expand Up @@ -166,8 +167,21 @@ func (m *Manager) Tf(ctx context.Context, format string, values ...interface{})

// TranslateFormat translates, formats and returns the `format` with configured language
// and given `values`.
// When values[0] is of type map[string]string, parameter replacement is order-independent.
// the values' format :{key}
func (m *Manager) TranslateFormat(ctx context.Context, format string, values ...interface{}) string {
return fmt.Sprintf(m.Translate(ctx, format), values...)
result := m.Translate(ctx, format)
if len(values) > 0 {
val := values[0]
if valMapStrStr, ok := val.(map[string]string); ok {
for k, v := range valMapStrStr {
rpStr := ":" + k
result = gstr.Replace(result, rpStr, v)
}
return result
}
}
return fmt.Sprintf(result, values...)
}

// Translate translates `content` with configured language.
Expand Down
13 changes: 13 additions & 0 deletions i18n/gi18n/gi18n_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gi18n_test

import (
"fmt"
"time"

"github.com/gogf/gf/v2/encoding/gbase64"
Expand Down Expand Up @@ -121,6 +122,18 @@ func Test_DefaultManager(t *testing.T) {
})
}

func Test_MapStrStrRelace(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gtest.DataPath("i18n-file"),
})
i18n.SetLanguage("zh-CN")
str := i18n.Tf(context.Background(), "replace_map_test", map[string]string{"score": "3000", "name": "blue"})
fmt.Println(str)
t.Assert(str, "hello 3000 blue is ok")
})
}

func Test_Instance(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance()
Expand Down
3 changes: 2 additions & 1 deletion i18n/gi18n/testdata/i18n-file/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"你好": "hello",
"世界": "world",
"hello": "你好",
"world": "世界"
"world": "世界",
"replace_map_test": "hello :score :name is ok"
}

0 comments on commit 5c0a9ce

Please sign in to comment.