gf/i18n/gi18n/gi18n_unit_test.go

147 lines
3.9 KiB
Go
Raw Normal View History

2019-09-01 00:30:01 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 gi18n_test
import (
"testing"
2019-09-04 19:23:19 +08:00
"github.com/gogf/gf/os/gres"
2019-09-01 00:30:01 +08:00
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/i18n/gi18n"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
2019-09-04 19:23:19 +08:00
_ "github.com/gogf/gf/os/gres/testdata/data"
2019-09-01 00:30:01 +08:00
)
func Test_Basic(t *testing.T) {
gtest.Case(t, func() {
t := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n",
})
t.SetLanguage("none")
gtest.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
gtest.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
gtest.Assert(t.T("{#hello}{#world}"), "你好世界")
t.SetDelimiters("{$", "}")
gtest.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
gtest.Assert(t.T("{$hello}{$world}"), "你好世界")
})
gtest.Case(t, func() {
t := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-file",
})
t.SetLanguage("none")
gtest.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
gtest.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
gtest.Assert(t.T("{#hello}{#world}"), "你好世界")
})
gtest.Case(t, func() {
t := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir",
})
t.SetLanguage("none")
gtest.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
gtest.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
gtest.Assert(t.T("{#hello}{#world}"), "你好世界")
})
}
func Test_DefaultManager(t *testing.T) {
gtest.Case(t, func() {
err := gi18n.SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n")
gtest.Assert(err, nil)
gi18n.SetLanguage("none")
gtest.Assert(gi18n.T("{#hello}{#world}"), "{#hello}{#world}")
gi18n.SetLanguage("ja")
gtest.Assert(gi18n.T("{#hello}{#world}"), "こんにちは世界")
gi18n.SetLanguage("zh-CN")
gtest.Assert(gi18n.T("{#hello}{#world}"), "你好世界")
})
gtest.Case(t, func() {
err := gi18n.SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir")
gtest.Assert(err, nil)
gi18n.SetLanguage("none")
gtest.Assert(gi18n.Translate("{#hello}{#world}"), "{#hello}{#world}")
gi18n.SetLanguage("ja")
gtest.Assert(gi18n.Translate("{#hello}{#world}"), "こんにちは世界")
gi18n.SetLanguage("zh-CN")
gtest.Assert(gi18n.Translate("{#hello}{#world}"), "你好世界")
})
}
func Test_Instance(t *testing.T) {
2019-09-04 19:23:19 +08:00
gres.Dump()
2019-09-01 00:30:01 +08:00
gtest.Case(t, func() {
m := gi18n.Instance()
2019-09-04 19:23:19 +08:00
err := m.SetPath("i18n-dir")
2019-09-01 00:30:01 +08:00
gtest.Assert(err, nil)
m.SetLanguage("zh-CN")
gtest.Assert(m.T("{#hello}{#world}"), "你好世界")
})
gtest.Case(t, func() {
m := gi18n.Instance()
gtest.Assert(m.T("{#hello}{#world}"), "你好世界")
})
gtest.Case(t, func() {
gtest.Assert(g.I18n().T("{#hello}{#world}"), "你好世界")
})
gtest.Case(t, func() {
m := gi18n.Instance(gconv.String(gtime.Nanosecond()))
gtest.Assert(m.T("{#hello}{#world}"), "{#hello}{#world}")
})
}
func Test_Resource(t *testing.T) {
gtest.Case(t, func() {
m := g.I18n("resource")
2019-09-04 19:23:19 +08:00
err := m.SetPath("i18n-dir")
2019-09-01 00:30:01 +08:00
gtest.Assert(err, nil)
m.SetLanguage("none")
gtest.Assert(m.T("{#hello}{#world}"), "{#hello}{#world}")
m.SetLanguage("ja")
gtest.Assert(m.T("{#hello}{#world}"), "こんにちは世界")
m.SetLanguage("zh-CN")
gtest.Assert(m.T("{#hello}{#world}"), "你好世界")
})
}