2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-08-31 18:04:12 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gview_test
|
|
|
|
|
|
|
|
import (
|
2021-05-13 00:16:45 +08:00
|
|
|
"context"
|
2019-08-31 18:04:12 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2021-11-15 20:49:02 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2019-08-31 18:04:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_I18n(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-31 18:04:12 +08:00
|
|
|
content := `{{.name}} says "{#hello}{#world}!"`
|
|
|
|
expect1 := `john says "你好世界!"`
|
|
|
|
expect2 := `john says "こんにちは世界!"`
|
|
|
|
expect3 := `john says "{#hello}{#world}!"`
|
|
|
|
|
2020-03-21 23:47:33 +08:00
|
|
|
g.I18n().SetPath(gdebug.TestDataPath("i18n"))
|
2019-08-31 18:04:12 +08:00
|
|
|
|
|
|
|
g.I18n().SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
result1, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-08-31 18:04:12 +08:00
|
|
|
"name": "john",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result1, expect1)
|
2019-08-31 18:04:12 +08:00
|
|
|
|
|
|
|
g.I18n().SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
result2, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-08-31 18:04:12 +08:00
|
|
|
"name": "john",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result2, expect2)
|
2019-08-31 18:04:12 +08:00
|
|
|
|
|
|
|
g.I18n().SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
result3, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-08-31 18:04:12 +08:00
|
|
|
"name": "john",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result3, expect3)
|
2019-08-31 18:04:12 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-01 15:31:26 +08:00
|
|
|
content := `{{.name}} says "{#hello}{#world}!"`
|
|
|
|
expect1 := `john says "你好世界!"`
|
|
|
|
expect2 := `john says "こんにちは世界!"`
|
|
|
|
expect3 := `john says "{#hello}{#world}!"`
|
|
|
|
|
|
|
|
g.I18n().SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n")
|
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
result1, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-11-01 15:31:26 +08:00
|
|
|
"name": "john",
|
|
|
|
"I18nLanguage": "zh-CN",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result1, expect1)
|
2019-11-01 15:31:26 +08:00
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
result2, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-11-01 15:31:26 +08:00
|
|
|
"name": "john",
|
|
|
|
"I18nLanguage": "ja",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result2, expect2)
|
2019-11-01 15:31:26 +08:00
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
result3, err := g.View().ParseContent(context.TODO(), content, g.Map{
|
2019-11-01 15:31:26 +08:00
|
|
|
"name": "john",
|
|
|
|
"I18nLanguage": "none",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result3, expect3)
|
2019-11-01 15:31:26 +08:00
|
|
|
})
|
2019-08-31 18:04:12 +08:00
|
|
|
}
|