-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_test.go
98 lines (91 loc) · 2.01 KB
/
i18n_test.go
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
package i18next_test
import (
"fmt"
"github.com/yuangwei/go-i18next"
"testing"
)
func TestI18nFromJsonLocal(t *testing.T) {
i18n, err := i18next.Init(i18next.I18nOptions{
Lng: []string{"en", "cn"},
DefaultLng: "en",
Ns: "json",
Backend: i18next.Backend{
LoadPath: []string{
"./examples/datas/{{.Ns}}/{{.Lng}}/home.json",
},
},
})
if err != nil {
t.Fatalf("i18n Initial error %s", err)
}
val, err := i18n.T("title", struct {
Name string
}{Name: "Mike"})
if err != nil {
t.Fatalf("i18n Format error %s", err)
}
if val != "Hello, Mike" {
t.Fatalf("i18n Format error")
}
fmt.Println(val)
}
func TestI18nChangeLanguage(t *testing.T) {
i18n, err := i18next.Init(i18next.I18nOptions{
Lng: []string{"en", "cn"},
DefaultLng: "en",
Ns: "json",
Backend: i18next.Backend{
LoadPath: []string{
"./examples/datas/{{.Ns}}/{{.Lng}}/home.json",
},
},
})
if err != nil {
t.Fatalf("i18n Initial error %s", err)
}
val, err := i18n.T("title", struct {
Name string
}{Name: "Mike"})
if err != nil {
t.Fatalf("i18n Format error %s", err)
}
if val != "Hello, Mike" {
t.Fatalf("i18n Format error")
}
err = i18n.ChangeLanguage("cn")
if err != nil {
t.Fatalf("i18n changeLanguage error %s", err)
}
val, err = i18n.T("title", struct {
Name string
}{Name: "麦克"})
if val != "你好, 麦克" {
t.Fatalf("i18n Format error")
}
fmt.Println(val)
}
func TestI18nFromJsonNetwork(t *testing.T) {
i18n, err := i18next.Init(i18next.I18nOptions{
Lng: []string{"en", "cn"},
DefaultLng: "en",
Ns: "json",
Backend: i18next.Backend{
LoadPath: []string{
"https://res.cloudinary.com/dvbgkny62/raw/upload/v1703728229/i18n/{{.Ns}}/{{.Lng}}/home.json",
},
},
})
if err != nil {
t.Fatalf("i18n Initial error %s", err)
}
val, err := i18n.T("title", struct {
Name string
}{Name: "Mike"})
if err != nil {
t.Fatalf("i18n Format error %s", err)
}
if val != "Hello, Mike" {
t.Fatalf("i18n Format error")
}
fmt.Println(val)
}