2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-01 00:30:01 +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 gi18n_test
|
|
|
|
|
|
|
|
import (
|
2023-02-23 10:07:40 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2022-11-01 20:12:21 +08:00
|
|
|
_ "github.com/gogf/gf/v2/os/gres/testdata/data"
|
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
"context"
|
2019-09-01 00:30:01 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/i18n/gi18n"
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gres"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-09-01 00:30:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Basic(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n := gi18n.New(gi18n.Options{
|
2022-03-17 16:58:04 +08:00
|
|
|
Path: gtest.DataPath("i18n"),
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetDelimiters("{$", "}")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
|
|
|
t.Assert(i18n.T(context.Background(), "{$hello}{$world}"), "你好世界")
|
2023-06-06 21:03:25 +08:00
|
|
|
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}")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n := gi18n.New(gi18n.Options{
|
2022-03-17 16:58:04 +08:00
|
|
|
Path: gtest.DataPath("i18n-file"),
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2023-06-06 21:03:25 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#你好} {#世界}"), "hello world")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n := gi18n.New(gi18n.Options{
|
2019-09-01 00:30:01 +08:00
|
|
|
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir",
|
|
|
|
})
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
i18n.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-08 16:46:52 +08:00
|
|
|
func Test_TranslateFormat(t *testing.T) {
|
2020-10-25 17:33:14 +08:00
|
|
|
// Tf
|
2020-08-08 16:46:52 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
i18n := gi18n.New(gi18n.Options{
|
2022-03-17 16:58:04 +08:00
|
|
|
Path: gtest.DataPath("i18n"),
|
2020-08-08 16:46:52 +08:00
|
|
|
})
|
|
|
|
i18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.Tf(context.Background(), "{#hello}{#world} %d", 2020), "{#hello}{#world} 2020")
|
2020-08-08 16:46:52 +08:00
|
|
|
|
|
|
|
i18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(i18n.Tf(context.Background(), "{#hello}{#world} %d", 2020), "こんにちは世界 2020")
|
2020-08-08 16:46:52 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-01 00:30:01 +08:00
|
|
|
func Test_DefaultManager(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-03-17 16:58:04 +08:00
|
|
|
err := gi18n.SetPath(gtest.DataPath("i18n"))
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-01 00:30:01 +08:00
|
|
|
err := gi18n.SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir")
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
gi18n.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Instance(t *testing.T) {
|
2019-09-04 19:23:19 +08:00
|
|
|
gres.Dump()
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-01 00:30:01 +08:00
|
|
|
m := gi18n.Instance()
|
2019-09-04 19:23:19 +08:00
|
|
|
err := m.SetPath("i18n-dir")
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-01 00:30:01 +08:00
|
|
|
m.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2023-06-06 21:03:25 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#你好} {#世界}"), "hello world")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-01 00:30:01 +08:00
|
|
|
m := gi18n.Instance()
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(g.I18n().T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
2020-05-13 19:35:56 +08:00
|
|
|
// Default language is: en
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
m := gi18n.Instance(gconv.String(gtime.TimestampNano()))
|
2022-05-12 15:25:28 +08:00
|
|
|
m.SetPath("i18n-dir")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "HelloWorld")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Resource(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-01 00:30:01 +08:00
|
|
|
m := g.I18n("resource")
|
2019-09-04 19:23:19 +08:00
|
|
|
err := m.SetPath("i18n-dir")
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
m.SetLanguage("none")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
m.SetLanguage("ja")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "こんにちは世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
|
|
|
|
m.SetLanguage("zh-CN")
|
2021-05-13 00:16:45 +08:00
|
|
|
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
|
2019-09-01 00:30:01 +08:00
|
|
|
})
|
|
|
|
}
|
2023-02-23 10:07:40 +08:00
|
|
|
|
|
|
|
func Test_SetCtxLanguage(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx := gctx.New()
|
|
|
|
t.Assert(gi18n.LanguageFromCtx(ctx), "")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(gi18n.LanguageFromCtx(nil), "")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx := gctx.New()
|
|
|
|
ctx = gi18n.WithLanguage(ctx, "zh-CN")
|
|
|
|
t.Assert(gi18n.LanguageFromCtx(ctx), "zh-CN")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx := gi18n.WithLanguage(nil, "zh-CN")
|
|
|
|
t.Assert(gi18n.LanguageFromCtx(ctx), "zh-CN")
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|