From 1bbfc56121367026307d51c93cb59939201f4639 Mon Sep 17 00:00:00 2001 From: aries Date: Tue, 6 Jun 2023 21:03:25 +0800 Subject: [PATCH] improve key words transaction for package i18n (#2652) --- i18n/gi18n/gi18n_manager.go | 6 ++++-- i18n/gi18n/gi18n_z_unit_test.go | 6 ++++++ i18n/gi18n/testdata/i18n-file/zh-CN.json | 6 ++++-- i18n/gi18n/testdata/i18n/zh-CN.json | 6 ++++++ i18n/gi18n/testdata/i18n/zh-CN.toml | 2 -- 5 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 i18n/gi18n/testdata/i18n/zh-CN.json delete mode 100644 i18n/gi18n/testdata/i18n/zh-CN.toml diff --git a/i18n/gi18n/gi18n_manager.go b/i18n/gi18n/gi18n_manager.go index 4492cbb10..ad2b101b0 100644 --- a/i18n/gi18n/gi18n_manager.go +++ b/i18n/gi18n/gi18n_manager.go @@ -68,7 +68,7 @@ func New(options ...Options) *Manager { m := &Manager{ options: opts, pattern: fmt.Sprintf( - `%s(\w+)%s`, + `%s(.+?)%s`, gregex.Quote(opts.Delimiters[0]), gregex.Quote(opts.Delimiters[1]), ), @@ -122,7 +122,7 @@ func (m *Manager) SetLanguage(language string) { // SetDelimiters sets the delimiters for translator. func (m *Manager) SetDelimiters(left, right string) { - m.pattern = fmt.Sprintf(`%s(\w+)%s`, gregex.Quote(left), gregex.Quote(right)) + m.pattern = fmt.Sprintf(`%s(.+?)%s`, gregex.Quote(left), gregex.Quote(right)) intlog.Printf(context.TODO(), `SetDelimiters: %v`, m.pattern) } @@ -166,6 +166,8 @@ func (m *Manager) Translate(ctx context.Context, content string) string { if v, ok := data[match[1]]; ok { return v } + // return match[1] will return the content between delimiters + // return match[0] will return the original content return match[0] }) intlog.Printf(ctx, `Translate for language: %s`, transLang) diff --git a/i18n/gi18n/gi18n_z_unit_test.go b/i18n/gi18n/gi18n_z_unit_test.go index d34b16345..1a20c0cff 100644 --- a/i18n/gi18n/gi18n_z_unit_test.go +++ b/i18n/gi18n/gi18n_z_unit_test.go @@ -39,6 +39,10 @@ func Test_Basic(t *testing.T) { i18n.SetDelimiters("{$", "}") t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") t.Assert(i18n.T(context.Background(), "{$hello}{$world}"), "你好世界") + t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") + t.Assert(i18n.T(context.Background(), "{$你好} {$世界}"), "hello world") + // undefined variables. + t.Assert(i18n.T(context.Background(), "{$你好1}{$世界1}"), "{$你好1}{$世界1}") }) gtest.C(t, func(t *gtest.T) { @@ -53,6 +57,7 @@ func Test_Basic(t *testing.T) { i18n.SetLanguage("zh-CN") t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界") + t.Assert(i18n.T(context.Background(), "{#你好} {#世界}"), "hello world") }) gtest.C(t, func(t *gtest.T) { @@ -122,6 +127,7 @@ func Test_Instance(t *testing.T) { t.AssertNil(err) m.SetLanguage("zh-CN") t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界") + t.Assert(m.T(context.Background(), "{#你好} {#世界}"), "hello world") }) gtest.C(t, func(t *gtest.T) { diff --git a/i18n/gi18n/testdata/i18n-file/zh-CN.json b/i18n/gi18n/testdata/i18n-file/zh-CN.json index 1de75ba1a..e84004288 100644 --- a/i18n/gi18n/testdata/i18n-file/zh-CN.json +++ b/i18n/gi18n/testdata/i18n-file/zh-CN.json @@ -1,4 +1,6 @@ { - "hello": "你好", - "world": "世界" + "你好": "hello", + "世界": "world", + "hello": "你好", + "world": "世界" } \ No newline at end of file diff --git a/i18n/gi18n/testdata/i18n/zh-CN.json b/i18n/gi18n/testdata/i18n/zh-CN.json new file mode 100644 index 000000000..cf48f4e4a --- /dev/null +++ b/i18n/gi18n/testdata/i18n/zh-CN.json @@ -0,0 +1,6 @@ +{ + "你好": "hello", + "世界": "world", + "hello": "你好", + "world": "世界" +} \ No newline at end of file diff --git a/i18n/gi18n/testdata/i18n/zh-CN.toml b/i18n/gi18n/testdata/i18n/zh-CN.toml deleted file mode 100644 index b3a52c527..000000000 --- a/i18n/gi18n/testdata/i18n/zh-CN.toml +++ /dev/null @@ -1,2 +0,0 @@ -hello = "你好" -world = "世界" \ No newline at end of file