Repository navigation
Expand file tree
/
Copy pathgi18n_unit_test.go
More file actions
168 lines (133 loc) · 4.42 KB
/
Copy pathgi18n_unit_test.go
File metadata and controls
168 lines (133 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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"
"github.com/gogf/gf/os/gres"
"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"
_ "github.com/gogf/gf/os/gres/testdata/data"
)
func Test_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gdebug.TestDataPath("i18n"),
})
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
i18n.SetDelimiters("{$", "}")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
t.Assert(i18n.T("{$hello}{$world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gdebug.TestDataPath("i18n-file"),
})
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir",
})
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
})
}
func Test_TranslateFormat(t *testing.T) {
// Tf
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gdebug.TestDataPath("i18n"),
})
i18n.SetLanguage("none")
t.Assert(i18n.Tf("{#hello}{#world} %d", 2020), "{#hello}{#world} 2020")
i18n.SetLanguage("ja")
t.Assert(i18n.Tf("{#hello}{#world} %d", 2020), "こんにちは世界 2020")
})
// Tfl
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gdebug.TestDataPath("i18n"),
})
t.Assert(i18n.Tfl("ja", "{#hello}{#world} %d", 2020), "こんにちは世界 2020")
t.Assert(i18n.Tfl("zh-CN", "{#hello}{#world} %d", 2020), "你好世界 2020")
})
}
func Test_DefaultManager(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := gi18n.SetPath(gdebug.TestDataPath("i18n"))
t.Assert(err, nil)
gi18n.SetLanguage("none")
t.Assert(gi18n.T("{#hello}{#world}"), "{#hello}{#world}")
gi18n.SetLanguage("ja")
t.Assert(gi18n.T("{#hello}{#world}"), "こんにちは世界")
gi18n.SetLanguage("zh-CN")
t.Assert(gi18n.T("{#hello}{#world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
err := gi18n.SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir")
t.Assert(err, nil)
gi18n.SetLanguage("none")
t.Assert(gi18n.Translate("{#hello}{#world}"), "{#hello}{#world}")
gi18n.SetLanguage("ja")
t.Assert(gi18n.Translate("{#hello}{#world}"), "こんにちは世界")
gi18n.SetLanguage("zh-CN")
t.Assert(gi18n.Translate("{#hello}{#world}"), "你好世界")
})
}
func Test_Instance(t *testing.T) {
gres.Dump()
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance()
err := m.SetPath("i18n-dir")
t.Assert(err, nil)
m.SetLanguage("zh-CN")
t.Assert(m.T("{#hello}{#world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance()
t.Assert(m.T("{#hello}{#world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.I18n().T("{#hello}{#world}"), "你好世界")
})
// Default language is: en
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance(gconv.String(gtime.TimestampNano()))
t.Assert(m.T("{#hello}{#world}"), "HelloWorld")
})
}
func Test_Resource(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := g.I18n("resource")
err := m.SetPath("i18n-dir")
t.Assert(err, nil)
m.SetLanguage("none")
t.Assert(m.T("{#hello}{#world}"), "{#hello}{#world}")
m.SetLanguage("ja")
t.Assert(m.T("{#hello}{#world}"), "こんにちは世界")
m.SetLanguage("zh-CN")
t.Assert(m.T("{#hello}{#world}"), "你好世界")
})
}