-
Notifications
You must be signed in to change notification settings - Fork 1
/
xlanguage.go
227 lines (200 loc) · 5.97 KB
/
xlanguage.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package xcore
import (
"bufio"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"golang.org/x/text/language"
)
// XLanguage is the oficial structure for the user
type XLanguage struct {
Name string
Language language.Tag
Entries map[string]string
}
// NewXLanguage will create an empty Language structure with a name and a language
func NewXLanguage(name string, lang language.Tag) *XLanguage {
return &XLanguage{Name: name, Language: lang, Entries: make(map[string]string)}
}
// NewXLanguageFromXMLFile will create an XLanguage structure with the data into the XML file
// Returns nil if there is an error
func NewXLanguageFromXMLFile(file string) (*XLanguage, error) {
lang := &XLanguage{Entries: make(map[string]string)}
err := lang.LoadXMLFile(file)
if err != nil {
return nil, err
}
return lang, nil
}
// NewXLanguageFromXMLString will create an XLanguage structure with the data into the XML String
// Returns nil if there is an error
func NewXLanguageFromXMLString(xml string) (*XLanguage, error) {
lang := &XLanguage{Entries: make(map[string]string)}
err := lang.LoadXMLString(xml)
if err != nil {
return nil, err
}
return lang, nil
}
// NewXLanguageFromFile will create an XLanguage structure with the data into the text file
// Returns nil if there is an error
func NewXLanguageFromFile(file string) (*XLanguage, error) {
l := &XLanguage{Entries: make(map[string]string)}
err := l.LoadFile(file)
if err != nil {
return nil, err
}
return l, nil
}
// NewXLanguageFromString will create an XLanguage structure with the data into the string
// Returns nil if there is an error
func NewXLanguageFromString(data string) (*XLanguage, error) {
l := &XLanguage{Entries: make(map[string]string)}
err := l.LoadString(data)
if err != nil {
return nil, err
}
return l, nil
}
// LoadXMLFile will Load a language from an XML file and replace the content of the XLanguage structure with the new data
// Returns nil if there is an error
func (l *XLanguage) LoadXMLFile(file string) error {
xmlFile, err := os.Open(file)
if err != nil {
return err
}
data, err := ioutil.ReadAll(xmlFile)
if err != nil {
return err
}
err = xmlFile.Close()
if err != nil {
return err
}
return l.LoadXMLString(string(data))
}
// LoadXMLString will Load a language from an XML file and replace the content of the XLanguage structure with the new data
// Returns nil if there is an error
func (l *XLanguage) LoadXMLString(data string) error {
// Temporal structures for XML loading
type xentry struct {
ID string `xml:"id,attr"`
Entry string `xml:",chardata"`
}
type xlang struct {
Name string `xml:"id,attr"`
Language string `xml:"lang,attr"`
Entries []xentry `xml:"entry"`
}
// Unmarshal
temp := &xlang{}
err := xml.Unmarshal([]byte(data), temp)
if err != nil {
return err
}
// Scan to our XLanguage Object
l.Name = temp.Name
l.Language, _ = language.Parse(temp.Language)
for _, e := range temp.Entries {
l.Entries[e.ID] = e.Entry
}
return nil
}
// LoadFile will Load a language from a file and replace the content of the XLanguage structure with the new data
// Returns nil if there is an error
func (l *XLanguage) LoadFile(file string) error {
flatFile, err := os.Open(file)
if err != nil {
return err
}
data, err := ioutil.ReadAll(flatFile)
if err != nil {
return err
}
err = flatFile.Close()
if err != nil {
return err
}
return l.LoadString(string(data))
}
// LoadString will Load a language from a string and replace the content of the XLanguage structure with the new data
// Returns nil if there is an error
func (l *XLanguage) LoadString(data string) error {
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
posequal := strings.Index(line, "=")
// we ignore empty and comments lines, no key=value lines too
if len(line) == 0 || line[0] == '#' || line[0] == ';' || posequal < 0 {
continue
}
// we separate the key. if there is no key, we ignore the data
key := strings.TrimSpace(line[:posequal])
if len(key) == 0 {
continue
}
// we capture the value if it exists. If not, the key entry is initialized with a nil value
value := ""
if len(line) > posequal {
value = strings.TrimSpace(line[posequal+1:])
}
l.Entries[key] = value
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// SetName will set the name of the language table
func (l *XLanguage) SetName(name string) {
l.Name = name
}
// SetLanguage will set the language ISO code (2 letters) of the language table
func (l *XLanguage) SetLanguage(lang language.Tag) {
l.Language = lang
}
// GetName will return the name of the language table
func (l *XLanguage) GetName() string {
return l.Name
}
// GetLanguage will return the language of the language table
func (l *XLanguage) GetLanguage() language.Tag {
return l.Language
}
// Set will add an entry id-value into the language table
func (l *XLanguage) Set(entry string, value string) {
l.Entries[entry] = value
}
// Get will read an entry id-value from the language table
func (l *XLanguage) Get(entry string) string {
v, ok := l.Entries[entry]
if ok {
return v
}
return ""
}
// Del will remove an entry id-value from the language table
func (l *XLanguage) Del(entry string) {
delete(l.Entries, entry)
}
// String will transform the XDataset into a readable string for humans
func (l *XLanguage) String() string {
sdata := []string{}
for key, val := range l.Entries {
sdata = append(sdata, key+":"+fmt.Sprintf("%v", val))
}
sort.Strings(sdata) // Lets be sure the print is always the same presentation
return "xcore.XLanguage{" + strings.Join(sdata, " ") + "}"
}
// GoString will transform the XDataset into a readable string for humans
func (l *XLanguage) GoString() string {
sdata := []string{}
for key, val := range l.Entries {
sdata = append(sdata, key+":"+fmt.Sprintf("%#v", val))
}
sort.Strings(sdata) // Lets be sure the print is always the same presentation
return "#xcore.XLanguage{" + strings.Join(sdata, " ") + "}"
}