Skip to content

Commit 1739d4d

Browse files
feat(i18n/gi18n): decoding and loading i18n files content by automatic file extension check (gogf#4662)
The i18n file handling now checks file extensions instead of content. If no extension info is found, it reverts to checking the file content.
1 parent 46cc4ce commit 1739d4d

4 files changed

Lines changed: 15 additions & 3 deletions

File tree

i18n/gi18n/gi18n_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ func (m *Manager) init(ctx context.Context) {
267267
if m.data[lang] == nil {
268268
m.data[lang] = make(map[string]string)
269269
}
270-
if j, err := gjson.LoadContent(file.Content()); err == nil {
270+
options := gjson.Options{Type: gfile.ExtName(name)}
271+
if j, err := gjson.LoadWithOptions(file.Content(), options); err == nil {
271272
for k, v := range j.Var().Map() {
272273
m.data[lang][k] = gconv.String(v)
273274
}

i18n/gi18n/gi18n_z_unit_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func Test_Resource(t *testing.T) {
159159

160160
m.SetLanguage("zh-CN")
161161
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
162+
163+
m.SetLanguage("ja")
164+
const key = "The year is 2128. AI has taken over the world, and all forms of racing is done by these DNA driven machines ; they are alive, and they have a will to win."
165+
t.Assert(m.T(context.Background(), "{#"+key+"}"), "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。")
162166
})
163167
}
164168

@@ -215,6 +219,9 @@ func Test_PathInResource(t *testing.T) {
215219
t.AssertNil(err)
216220
i18n.SetLanguage("ja")
217221
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
222+
223+
const key = "The year is 2128. AI has taken over the world, and all forms of racing is done by these DNA driven machines ; they are alive, and they have a will to win."
224+
t.Assert(i18n.T(context.Background(), "{#"+key+"}"), "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。")
218225
})
219226
}
220227

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
world: "世界"
1+
world: "世界"
2+
3+
"The year is 2128. AI has taken over the world, and all forms of racing is done by these DNA driven machines ; they are alive, and they have a will to win.": "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。"

i18n/gi18n/testdata/i18n/ja.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11

22
hello = "こんにちは"
3-
world = "世界"
3+
world = "世界"
4+
5+
"The year is 2128. AI has taken over the world, and all forms of racing is done by these DNA driven machines ; they are alive, and they have a will to win." = "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。"

0 commit comments

Comments
 (0)