-
Notifications
You must be signed in to change notification settings - Fork 1
/
xlanguage_test.go
202 lines (171 loc) · 5.44 KB
/
xlanguage_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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package xcore
import (
"fmt"
"io/ioutil"
"testing"
"golang.org/x/text/language"
)
func ExampleNewXLanguage() {
langES := NewXLanguage("messages", language.Spanish)
langEN := NewXLanguage("messages", language.English)
langFR := NewXLanguage("messages", language.French)
// You can load this from your system files for instance and keep your translation tables apart
langES.Set("panicerror", "Error crítico del sistema")
langEN.Set("panicerror", "System panic error")
langFR.Set("panicerror", "Erreur grave dans le système")
// pick a random general system language (for instance the client's OS language)
lang := langFR
// launch a panic errors
fmt.Println("Launch a panic error message in the selected language:", lang.Get("panicerror"))
// Output:
// Launch a panic error message in the selected language: Erreur grave dans le système
}
func ExampleNewXLanguageFromXMLFile() {
langES, _ := NewXLanguageFromXMLFile("./testunit/a.es.language")
langEN, _ := NewXLanguageFromXMLFile("./testunit/a.en.language")
langFR, _ := NewXLanguageFromXMLFile("./testunit/a.fr.language")
// pick a random general system language (for instance the client's OS language)
lang := langES
// Say hello main language
fmt.Println(lang.Get("entry1") + " " + lang.Get("entry2"))
// Say hello in other languages
fmt.Println(langEN.Get("entry1") + " " + langEN.Get("entry2"))
fmt.Println(langFR.Get("entry1") + " " + langFR.Get("entry2"))
// Output:
// Bienvenido a XCore
// Welcome to XCore
// Bienvenue à XCore
}
func TestXLanguage(t *testing.T) {
// Those strings are the same in errors.es.txt and errors.es.xml to make the tests
teststrings := map[string]string{
"panicerror": "Error crítico del sistema",
"systemerror": "Error del sistema",
"fileerror": "Error leyendo el archivo",
}
manualES := NewXLanguage("messages", language.Spanish)
for id, val := range teststrings {
manualES.Set(id, val)
}
// Load from xml file
loadxmlES, err := NewXLanguageFromXMLFile("./testunit/errors.es.xml")
if err != nil {
t.Errorf("Error loading XML File into language %s", err)
return
}
// Load from text file
loadtextES, err := NewXLanguageFromFile("./testunit/errors.es.txt")
if err != nil {
t.Errorf("Error loading Text File into language %s", err)
return
}
// from xml string
xmlstr, err := ioutil.ReadFile("./testunit/errors.es.xml")
if err != nil {
t.Errorf("Error loading XML File errors into string %s", err)
return
}
loadstringxmlES, err := NewXLanguageFromXMLString(string(xmlstr))
if err != nil {
t.Errorf("Error loading XML File into language %s", err)
return
}
// from text string
textstr, err := ioutil.ReadFile("./testunit/errors.es.txt")
if err != nil {
t.Errorf("Error loading Text File errors into string %s", err)
return
}
loadstringtxtES, err := NewXLanguageFromString(string(textstr))
if err != nil {
t.Errorf("Error loading Text File into language %s", err)
return
}
// verify all
for id, val := range teststrings {
v1 := manualES.Get(id)
if v1 != val {
t.Errorf("Error reading value of manualES::%s", v1)
return
}
v2 := loadxmlES.Get(id)
if v2 != val {
t.Errorf("Error reading value of loadxmlES::%s", v2)
return
}
v3 := loadtextES.Get(id)
if v3 != val {
t.Errorf("Error reading value of loadtextES::%s", v3)
return
}
v4 := loadstringxmlES.Get(id)
if v4 != val {
t.Errorf("Error reading value of loadstringxmlES::%s", v4)
return
}
v5 := loadstringtxtES.Get(id)
if v5 != val {
t.Errorf("Error reading value of loadstringtxtES::%s", v5)
return
}
}
}
func TestXLanguageAssign(t *testing.T) {
// Those strings are the same in errors.es.txt and errors.es.xml to make the tests
teststrings := map[string]string{
"panicerror": "System critical error",
"systemerror": "System error",
"fileerror": "File error",
}
manualES := NewXLanguage("messages", language.Spanish)
for id, val := range teststrings {
manualES.Set(id, val)
}
manualES.SetName("errors")
manualES.SetLanguage(language.Spanish)
manualES.Set("noerror", "There is no error")
// Overload the spanish entries
err := manualES.LoadFile("./testunit/errors.es.txt")
if err != nil {
t.Errorf("Error loading Text File into language %s", err)
return
}
v1 := manualES.Get("panicerror")
if v1 != "Error crítico del sistema" {
t.Errorf("The value of panicerror is not correct %s", v1)
return
}
v2 := manualES.Get("noerror") // should still be in english
if v2 != "There is no error" {
t.Errorf("The value of noerror is not correct %s", v2)
return
}
manualES.Del("noerror")
v3 := manualES.Get("noerror") // should be empty
if v3 != "" {
t.Errorf("The value of noerror is not correct (empty) %s", v3)
return
}
name := manualES.GetName()
if name != "errors" {
t.Errorf("The value of language name is not correct %s", name)
return
}
lang := manualES.GetLanguage()
if lang != language.Spanish {
t.Errorf("The value of language is not correct %s", lang)
return
}
// String
str := fmt.Sprint(manualES)
if str != "xcore.XLanguage{fileerror:Error leyendo el archivo panicerror:Error crítico del sistema systemerror:Error del sistema}" {
t.Errorf("The print value language is not correct %s", str)
return
}
// Gostring
str = fmt.Sprintf("%#v", manualES)
if str != "#xcore.XLanguage{fileerror:\"Error leyendo el archivo\" panicerror:\"Error crítico del sistema\" systemerror:\"Error del sistema\"}" {
t.Errorf("The print #value language is not correct %s", str)
return
}
}