-
Notifications
You must be signed in to change notification settings - Fork 57
/
mo.go
297 lines (256 loc) · 7.44 KB
/
mo.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
package gotext
import (
"bytes"
"encoding/binary"
"io/fs"
)
const (
// MoMagicLittleEndian encoding
MoMagicLittleEndian = 0x950412de
// MoMagicBigEndian encoding
MoMagicBigEndian = 0xde120495
// EotSeparator msgctxt and msgid separator
EotSeparator = "\x04"
// NulSeparator msgid and msgstr separator
NulSeparator = "\x00"
)
/*
Mo parses the content of any MO file and provides all the Translation functions needed.
It's the base object used by all package methods.
And it's safe for concurrent use by multiple goroutines by using the sync package for locking.
Example:
import (
"fmt"
"github.com/leonelquinteros/gotext"
)
func main() {
// Create mo object
mo := gotext.NewMo()
// Parse .mo file
mo.ParseFile("/path/to/po/file/translations.mo")
// Get Translation
fmt.Println(mo.Get("Translate this"))
}
*/
type Mo struct {
// these three public members are for backwards compatibility. they are just set to the value in the domain
Headers HeaderMap
Language string
PluralForms string
domain *Domain
fs fs.FS
}
// NewMo should always be used to instantiate a new Mo object
func NewMo() *Mo {
mo := new(Mo)
mo.domain = NewDomain()
return mo
}
// NewMoFS works like NewMO but adds an optional fs.FS
func NewMoFS(filesystem fs.FS) *Mo {
mo := NewMo()
mo.fs = filesystem
return mo
}
func (mo *Mo) GetDomain() *Domain {
return mo.domain
}
// all of the Get functions are for convenience and aid in backwards compatibility
func (mo *Mo) Get(str string, vars ...interface{}) string {
return mo.domain.Get(str, vars...)
}
func (mo *Mo) Append(b []byte, str string, vars ...interface{}) []byte {
return mo.domain.Append(b, str, vars...)
}
func (mo *Mo) GetN(str, plural string, n int, vars ...interface{}) string {
return mo.domain.GetN(str, plural, n, vars...)
}
func (mo *Mo) AppendN(b []byte, str, plural string, n int, vars ...interface{}) []byte {
return mo.domain.AppendN(b, str, plural, n, vars...)
}
func (mo *Mo) GetC(str, ctx string, vars ...interface{}) string {
return mo.domain.GetC(str, ctx, vars...)
}
func (mo *Mo) AppendC(b []byte, str, ctx string, vars ...interface{}) []byte {
return mo.domain.AppendC(b, str, ctx, vars...)
}
func (mo *Mo) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
return mo.domain.GetNC(str, plural, n, ctx, vars...)
}
func (mo *Mo) AppendNC(b []byte, str, plural string, n int, ctx string, vars ...interface{}) []byte {
return mo.domain.AppendNC(b, str, plural, n, ctx, vars...)
}
func (mo *Mo) IsTranslated(str string) bool {
return mo.domain.IsTranslated(str)
}
func (mo *Mo) IsTranslatedN(str string, n int) bool {
return mo.domain.IsTranslatedN(str, n)
}
func (mo *Mo) IsTranslatedC(str, ctx string) bool {
return mo.domain.IsTranslatedC(str, ctx)
}
func (mo *Mo) IsTranslatedNC(str string, n int, ctx string) bool {
return mo.domain.IsTranslatedNC(str, n, ctx)
}
func (mo *Mo) MarshalBinary() ([]byte, error) {
return mo.domain.MarshalBinary()
}
func (mo *Mo) UnmarshalBinary(data []byte) error {
return mo.domain.UnmarshalBinary(data)
}
func (mo *Mo) ParseFile(f string) {
data, err := getFileData(f, mo.fs)
if err != nil {
return
}
mo.Parse(data)
}
// Parse loads the translations specified in the provided byte slice, in the GNU gettext .mo format
func (mo *Mo) Parse(buf []byte) {
// Lock while parsing
mo.domain.trMutex.Lock()
mo.domain.pluralMutex.Lock()
defer mo.domain.trMutex.Unlock()
defer mo.domain.pluralMutex.Unlock()
r := bytes.NewReader(buf)
var magicNumber uint32
if err := binary.Read(r, binary.LittleEndian, &magicNumber); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
var bo binary.ByteOrder
switch magicNumber {
case MoMagicLittleEndian:
bo = binary.LittleEndian
case MoMagicBigEndian:
bo = binary.BigEndian
default:
return
// return fmt.Errorf("gettext: %v", "invalid magic number")
}
var header struct {
MajorVersion uint16
MinorVersion uint16
MsgIDCount uint32
MsgIDOffset uint32
MsgStrOffset uint32
HashSize uint32
HashOffset uint32
}
if err := binary.Read(r, bo, &header); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if v := header.MajorVersion; v != 0 && v != 1 {
return
// return fmt.Errorf("gettext: %v", "invalid version number")
}
if v := header.MinorVersion; v != 0 && v != 1 {
return
// return fmt.Errorf("gettext: %v", "invalid version number")
}
msgIDStart := make([]uint32, header.MsgIDCount)
msgIDLen := make([]uint32, header.MsgIDCount)
if _, err := r.Seek(int64(header.MsgIDOffset), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
for i := 0; i < int(header.MsgIDCount); i++ {
if err := binary.Read(r, bo, &msgIDLen[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if err := binary.Read(r, bo, &msgIDStart[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
}
msgStrStart := make([]int32, header.MsgIDCount)
msgStrLen := make([]int32, header.MsgIDCount)
if _, err := r.Seek(int64(header.MsgStrOffset), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
for i := 0; i < int(header.MsgIDCount); i++ {
if err := binary.Read(r, bo, &msgStrLen[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if err := binary.Read(r, bo, &msgStrStart[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
}
for i := 0; i < int(header.MsgIDCount); i++ {
if _, err := r.Seek(int64(msgIDStart[i]), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
msgIDData := make([]byte, msgIDLen[i])
if _, err := r.Read(msgIDData); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if _, err := r.Seek(int64(msgStrStart[i]), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
msgStrData := make([]byte, msgStrLen[i])
if _, err := r.Read(msgStrData); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if len(msgIDData) == 0 {
mo.addTranslation(msgIDData, msgStrData)
} else {
mo.addTranslation(msgIDData, msgStrData)
}
}
// Parse headers
mo.domain.parseHeaders()
// set values on this struct
// this is for backwards compatibility
mo.Language = mo.domain.Language
mo.PluralForms = mo.domain.PluralForms
mo.Headers = mo.domain.Headers
}
func (mo *Mo) addTranslation(msgid, msgstr []byte) {
translation := NewTranslation()
var msgctxt []byte
var msgidPlural []byte
d := bytes.Split(msgid, []byte(EotSeparator))
if len(d) == 1 {
msgid = d[0]
} else {
msgid, msgctxt = d[1], d[0]
}
dd := bytes.Split(msgid, []byte(NulSeparator))
if len(dd) > 1 {
msgid = dd[0]
dd = dd[1:]
}
translation.ID = string(msgid)
msgidPlural = bytes.Join(dd, []byte(NulSeparator))
if len(msgidPlural) > 0 {
translation.PluralID = string(msgidPlural)
}
ddd := bytes.Split(msgstr, []byte(NulSeparator))
if len(ddd) > 0 {
for i, s := range ddd {
translation.Trs[i] = string(s)
}
}
if len(msgctxt) > 0 {
// With context...
if _, ok := mo.domain.contextTranslations[string(msgctxt)]; !ok {
mo.domain.contextTranslations[string(msgctxt)] = make(map[string]*Translation)
}
mo.domain.contextTranslations[string(msgctxt)][translation.ID] = translation
} else {
mo.domain.translations[translation.ID] = translation
}
}