github.com/gogf/gf@v1.16.9/i18n/gi18n/gi18n_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gi18n_test 8 9 import ( 10 "context" 11 "testing" 12 13 "github.com/gogf/gf/os/gres" 14 15 "github.com/gogf/gf/os/gtime" 16 "github.com/gogf/gf/util/gconv" 17 18 "github.com/gogf/gf/frame/g" 19 20 "github.com/gogf/gf/i18n/gi18n" 21 22 "github.com/gogf/gf/debug/gdebug" 23 "github.com/gogf/gf/os/gfile" 24 25 "github.com/gogf/gf/test/gtest" 26 27 _ "github.com/gogf/gf/os/gres/testdata/data" 28 ) 29 30 func Test_Basic(t *testing.T) { 31 gtest.C(t, func(t *gtest.T) { 32 i18n := gi18n.New(gi18n.Options{ 33 Path: gdebug.TestDataPath("i18n"), 34 }) 35 i18n.SetLanguage("none") 36 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 37 38 i18n.SetLanguage("ja") 39 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界") 40 41 i18n.SetLanguage("zh-CN") 42 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界") 43 i18n.SetDelimiters("{$", "}") 44 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 45 t.Assert(i18n.T(context.Background(), "{$hello}{$world}"), "你好世界") 46 }) 47 48 gtest.C(t, func(t *gtest.T) { 49 i18n := gi18n.New(gi18n.Options{ 50 Path: gdebug.TestDataPath("i18n-file"), 51 }) 52 i18n.SetLanguage("none") 53 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 54 55 i18n.SetLanguage("ja") 56 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界") 57 58 i18n.SetLanguage("zh-CN") 59 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界") 60 }) 61 62 gtest.C(t, func(t *gtest.T) { 63 i18n := gi18n.New(gi18n.Options{ 64 Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir", 65 }) 66 i18n.SetLanguage("none") 67 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 68 69 i18n.SetLanguage("ja") 70 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界") 71 72 i18n.SetLanguage("zh-CN") 73 t.Assert(i18n.T(context.Background(), "{#hello}{#world}"), "你好世界") 74 }) 75 } 76 77 func Test_TranslateFormat(t *testing.T) { 78 // Tf 79 gtest.C(t, func(t *gtest.T) { 80 i18n := gi18n.New(gi18n.Options{ 81 Path: gdebug.TestDataPath("i18n"), 82 }) 83 i18n.SetLanguage("none") 84 t.Assert(i18n.Tf(context.Background(), "{#hello}{#world} %d", 2020), "{#hello}{#world} 2020") 85 86 i18n.SetLanguage("ja") 87 t.Assert(i18n.Tf(context.Background(), "{#hello}{#world} %d", 2020), "こんにちは世界 2020") 88 }) 89 } 90 91 func Test_DefaultManager(t *testing.T) { 92 gtest.C(t, func(t *gtest.T) { 93 err := gi18n.SetPath(gdebug.TestDataPath("i18n")) 94 t.Assert(err, nil) 95 96 gi18n.SetLanguage("none") 97 t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 98 99 gi18n.SetLanguage("ja") 100 t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "こんにちは世界") 101 102 gi18n.SetLanguage("zh-CN") 103 t.Assert(gi18n.T(context.Background(), "{#hello}{#world}"), "你好世界") 104 }) 105 106 gtest.C(t, func(t *gtest.T) { 107 err := gi18n.SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir") 108 t.Assert(err, nil) 109 110 gi18n.SetLanguage("none") 111 t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 112 113 gi18n.SetLanguage("ja") 114 t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "こんにちは世界") 115 116 gi18n.SetLanguage("zh-CN") 117 t.Assert(gi18n.Translate(context.Background(), "{#hello}{#world}"), "你好世界") 118 }) 119 } 120 121 func Test_Instance(t *testing.T) { 122 gres.Dump() 123 gtest.C(t, func(t *gtest.T) { 124 m := gi18n.Instance() 125 err := m.SetPath("i18n-dir") 126 t.Assert(err, nil) 127 m.SetLanguage("zh-CN") 128 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界") 129 }) 130 131 gtest.C(t, func(t *gtest.T) { 132 m := gi18n.Instance() 133 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界") 134 }) 135 136 gtest.C(t, func(t *gtest.T) { 137 t.Assert(g.I18n().T(context.Background(), "{#hello}{#world}"), "你好世界") 138 }) 139 // Default language is: en 140 gtest.C(t, func(t *gtest.T) { 141 m := gi18n.Instance(gconv.String(gtime.TimestampNano())) 142 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "HelloWorld") 143 }) 144 } 145 146 func Test_Resource(t *testing.T) { 147 gtest.C(t, func(t *gtest.T) { 148 m := g.I18n("resource") 149 err := m.SetPath("i18n-dir") 150 t.Assert(err, nil) 151 152 m.SetLanguage("none") 153 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "{#hello}{#world}") 154 155 m.SetLanguage("ja") 156 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "こんにちは世界") 157 158 m.SetLanguage("zh-CN") 159 t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界") 160 }) 161 }