mirror of
https://gitee.com/johng/gf
synced 2026-06-05 18:15:43 +08:00
feat(i18n/gi18n): decoding and loading i18n files content by automatic file extension check (#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.
This commit is contained in:
@ -267,7 +267,8 @@ func (m *Manager) init(ctx context.Context) {
|
||||
if m.data[lang] == nil {
|
||||
m.data[lang] = make(map[string]string)
|
||||
}
|
||||
if j, err := gjson.LoadContent(file.Content()); err == nil {
|
||||
options := gjson.Options{Type: gfile.ExtName(name)}
|
||||
if j, err := gjson.LoadWithOptions(file.Content(), options); err == nil {
|
||||
for k, v := range j.Var().Map() {
|
||||
m.data[lang][k] = gconv.String(v)
|
||||
}
|
||||
|
||||
@ -159,6 +159,10 @@ func Test_Resource(t *testing.T) {
|
||||
|
||||
m.SetLanguage("zh-CN")
|
||||
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
||||
|
||||
m.SetLanguage("ja")
|
||||
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."
|
||||
t.Assert(m.T(context.Background(), "{#"+key+"}"), "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。")
|
||||
})
|
||||
}
|
||||
|
||||
@ -215,6 +219,9 @@ func Test_PathInResource(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
i18n.SetLanguage("ja")
|
||||
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
||||
|
||||
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."
|
||||
t.Assert(i18n.T(context.Background(), "{#"+key+"}"), "2128年です。AIが世界を支配し、あらゆる形式のレースはDNA駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
4
i18n/gi18n/testdata/i18n-dir/ja/world.yaml
vendored
4
i18n/gi18n/testdata/i18n-dir/ja/world.yaml
vendored
@ -1 +1,3 @@
|
||||
world: "世界"
|
||||
world: "世界"
|
||||
|
||||
"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駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。"
|
||||
4
i18n/gi18n/testdata/i18n/ja.toml
vendored
4
i18n/gi18n/testdata/i18n/ja.toml
vendored
@ -1,3 +1,5 @@
|
||||
|
||||
hello = "こんにちは"
|
||||
world = "世界"
|
||||
world = "世界"
|
||||
|
||||
"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駆動の機械によって行われています。彼らは生きており、勝つ意志を持っています。"
|
||||
|
||||
Reference in New Issue
Block a user