@@ -24,62 +24,54 @@ package gotext
24
24
25
25
import (
26
26
"encoding/gob"
27
- "strings"
28
27
"sync"
29
28
)
30
29
31
30
// Global environment variables
32
31
type config struct {
33
32
sync.RWMutex
34
33
35
- // Path to library directory where all locale directories and Translation files are.
36
- library string
37
-
38
34
// Default domain to look at when no domain is specified. Used by package level functions.
39
35
domain string
40
36
41
37
// Language set.
42
- languages []string
38
+ language string
39
+
40
+ // Path to library directory where all locale directories and Translation files are.
41
+ library string
43
42
44
43
// Storage for package level methods
45
- locales [] * Locale
44
+ storage * Locale
46
45
}
47
46
48
47
var globalConfig * config
49
48
50
49
func init () {
51
50
// Init default configuration
52
51
globalConfig = & config {
53
- domain : "default" ,
54
- languages : [] string { "en_US" } ,
55
- library : "/usr/local/share/locale" ,
56
- locales : nil ,
52
+ domain : "default" ,
53
+ language : "en_US" ,
54
+ library : "/usr/local/share/locale" ,
55
+ storage : nil ,
57
56
}
58
57
59
58
// Register Translator types for gob encoding
60
59
gob .Register (TranslatorEncoding {})
61
60
}
62
61
63
- // loadStorage creates a new Locale object at package level based on the Global variables settings
64
- // for every language specified using Configure.
62
+ // loadStorage creates a new Locale object at package level based on the Global variables settings.
65
63
// It's called automatically when trying to use Get or GetD methods.
66
64
func loadStorage (force bool ) {
67
65
globalConfig .Lock ()
68
66
69
- if globalConfig .locales == nil || force {
70
- var locales []* Locale
71
- for _ , language := range globalConfig .languages {
72
- locales = append (locales , NewLocale (globalConfig .library , language ))
73
- }
74
- globalConfig .locales = locales
67
+ if globalConfig .storage == nil || force {
68
+ globalConfig .storage = NewLocale (globalConfig .library , globalConfig .language )
75
69
}
76
70
77
- for _ , locale := range globalConfig .locales {
78
- if _ , ok := locale .Domains [globalConfig .domain ]; ! ok || force {
79
- locale .AddDomain (globalConfig .domain )
80
- }
81
- locale .SetDomain (globalConfig .domain )
71
+ if _ , ok := globalConfig .storage .Domains [globalConfig .domain ]; ! ok || force {
72
+ globalConfig .storage .AddDomain (globalConfig .domain )
82
73
}
74
+ globalConfig .storage .SetDomain (globalConfig .domain )
83
75
84
76
globalConfig .Unlock ()
85
77
}
@@ -88,9 +80,8 @@ func loadStorage(force bool) {
88
80
func GetDomain () string {
89
81
var dom string
90
82
globalConfig .RLock ()
91
- if globalConfig .locales != nil {
92
- // All locales have the same domain
93
- dom = globalConfig .locales [0 ].GetDomain ()
83
+ if globalConfig .storage != nil {
84
+ dom = globalConfig .storage .GetDomain ()
94
85
}
95
86
if dom == "" {
96
87
dom = globalConfig .domain
@@ -105,42 +96,28 @@ func GetDomain() string {
105
96
func SetDomain (dom string ) {
106
97
globalConfig .Lock ()
107
98
globalConfig .domain = dom
108
- if globalConfig .locales != nil {
109
- for _ , locale := range globalConfig .locales {
110
- locale .SetDomain (dom )
111
- }
99
+ if globalConfig .storage != nil {
100
+ globalConfig .storage .SetDomain (dom )
112
101
}
113
102
globalConfig .Unlock ()
114
103
115
104
loadStorage (true )
116
105
}
117
106
118
- // GetLanguage returns the language gotext will translate into.
119
- // If multiple languages have been supplied, the first one will be returned.
107
+ // GetLanguage is the language getter for the package configuration
120
108
func GetLanguage () string {
121
- return GetLanguages ()[0 ]
122
- }
123
-
124
- // GetLanguages returns all languages that have been supplied.
125
- func GetLanguages () []string {
126
109
globalConfig .RLock ()
127
- defer globalConfig .RUnlock ()
128
- return globalConfig .languages
110
+ lang := globalConfig .language
111
+ globalConfig .RUnlock ()
112
+
113
+ return lang
129
114
}
130
115
131
- // SetLanguage sets the language code (or colon separated language codes) to be used at package level.
116
+ // SetLanguage sets the language code to be used at package level.
132
117
// It reloads the corresponding Translation file.
133
118
func SetLanguage (lang string ) {
134
119
globalConfig .Lock ()
135
- var languages []string
136
- for _ , language := range strings .Split (lang , ":" ) {
137
- language = SimplifiedLocale (language )
138
- languages = append (languages , language )
139
- if language == "C" {
140
- break
141
- }
142
- }
143
- globalConfig .languages = languages
120
+ globalConfig .language = SimplifiedLocale (lang )
144
121
globalConfig .Unlock ()
145
122
146
123
loadStorage (true )
@@ -155,7 +132,7 @@ func GetLibrary() string {
155
132
return lib
156
133
}
157
134
158
- // SetLibrary sets the root path for the locale directories and files to be used at package level.
135
+ // SetLibrary sets the root path for the loale directories and files to be used at package level.
159
136
// It reloads the corresponding Translation file.
160
137
func SetLibrary (lib string ) {
161
138
globalConfig .Lock ()
@@ -196,15 +173,7 @@ func SetStorage(storage *Locale) {
196
173
func Configure (lib , lang , dom string ) {
197
174
globalConfig .Lock ()
198
175
globalConfig .library = lib
199
- var languages []string
200
- for _ , language := range strings .Split (lang , ":" ) {
201
- language = SimplifiedLocale (language )
202
- languages = append (languages , language )
203
- if language == "C" {
204
- break
205
- }
206
- }
207
- globalConfig .languages = languages
176
+ globalConfig .language = SimplifiedLocale (lang )
208
177
globalConfig .domain = dom
209
178
globalConfig .Unlock ()
210
179
@@ -229,20 +198,16 @@ func GetD(dom, str string, vars ...interface{}) string {
229
198
// Try to load default package Locale storage
230
199
loadStorage (false )
231
200
201
+ // Return Translation
232
202
globalConfig .RLock ()
233
- defer globalConfig .RUnlock ()
234
203
235
- var tr string
236
- for i , locale := range globalConfig .locales {
237
- if _ , ok := locale .Domains [dom ]; ! ok {
238
- locale .AddDomain (dom )
239
- }
240
- if ! locale .IsTranslatedD (dom , str ) && i < (len (globalConfig .locales )- 1 ) {
241
- continue
242
- }
243
- tr = locale .GetD (dom , str , vars ... )
244
- break
204
+ if _ , ok := globalConfig .storage .Domains [dom ]; ! ok {
205
+ globalConfig .storage .AddDomain (dom )
245
206
}
207
+
208
+ tr := globalConfig .storage .GetD (dom , str , vars ... )
209
+ globalConfig .RUnlock ()
210
+
246
211
return tr
247
212
}
248
213
@@ -252,20 +217,16 @@ func GetND(dom, str, plural string, n int, vars ...interface{}) string {
252
217
// Try to load default package Locale storage
253
218
loadStorage (false )
254
219
220
+ // Return Translation
255
221
globalConfig .RLock ()
256
- defer globalConfig .RUnlock ()
257
222
258
- var tr string
259
- for i , locale := range globalConfig .locales {
260
- if _ , ok := locale .Domains [dom ]; ! ok {
261
- locale .AddDomain (dom )
262
- }
263
- if ! locale .IsTranslatedND (dom , str , n ) && i < (len (globalConfig .locales )- 1 ) {
264
- continue
265
- }
266
- tr = locale .GetND (dom , str , plural , n , vars ... )
267
- break
223
+ if _ , ok := globalConfig .storage .Domains [dom ]; ! ok {
224
+ globalConfig .storage .AddDomain (dom )
268
225
}
226
+
227
+ tr := globalConfig .storage .GetND (dom , str , plural , n , vars ... )
228
+ globalConfig .RUnlock ()
229
+
269
230
return tr
270
231
}
271
232
@@ -287,17 +248,11 @@ func GetDC(dom, str, ctx string, vars ...interface{}) string {
287
248
// Try to load default package Locale storage
288
249
loadStorage (false )
289
250
251
+ // Return Translation
290
252
globalConfig .RLock ()
291
- defer globalConfig .RUnlock ()
253
+ tr := globalConfig .storage .GetDC (dom , str , ctx , vars ... )
254
+ globalConfig .RUnlock ()
292
255
293
- var tr string
294
- for i , locale := range globalConfig .locales {
295
- if ! locale .IsTranslatedDC (dom , str , ctx ) && i < (len (globalConfig .locales )- 1 ) {
296
- continue
297
- }
298
- tr = locale .GetDC (dom , str , ctx , vars ... )
299
- break
300
- }
301
256
return tr
302
257
}
303
258
@@ -309,101 +264,62 @@ func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) str
309
264
310
265
// Return Translation
311
266
globalConfig .RLock ()
312
- defer globalConfig .RUnlock ()
267
+ tr := globalConfig .storage .GetNDC (dom , str , plural , n , ctx , vars ... )
268
+ globalConfig .RUnlock ()
313
269
314
- var tr string
315
- for i , locale := range globalConfig .locales {
316
- if ! locale .IsTranslatedNDC (dom , str , n , ctx ) && i < (len (globalConfig .locales )- 1 ) {
317
- continue
318
- }
319
- tr = locale .GetNDC (dom , str , plural , n , ctx , vars ... )
320
- break
321
- }
322
270
return tr
323
271
}
324
272
325
- // IsTranslated reports whether a string is translated in given languages.
326
- // When the langs argument is omitted, the output of GetLanguages is used.
327
- func IsTranslated (str string , langs ... string ) bool {
328
- return IsTranslatedND (GetDomain (), str , 0 , langs ... )
273
+ // IsTranslated reports whether a string is translated
274
+ func IsTranslated (str string ) bool {
275
+ return IsTranslatedND (GetDomain (), str , 0 )
329
276
}
330
277
331
- // IsTranslatedN reports whether a plural string is translated in given languages.
332
- // When the langs argument is omitted, the output of GetLanguages is used.
333
- func IsTranslatedN (str string , n int , langs ... string ) bool {
334
- return IsTranslatedND (GetDomain (), str , n , langs ... )
278
+ // IsTranslatedN reports whether a plural string is translated
279
+ func IsTranslatedN (str string , n int ) bool {
280
+ return IsTranslatedND (GetDomain (), str , n )
335
281
}
336
282
337
- // IsTranslatedD reports whether a domain string is translated in given languages.
338
- // When the langs argument is omitted, the output of GetLanguages is used.
339
- func IsTranslatedD (dom , str string , langs ... string ) bool {
340
- return IsTranslatedND (dom , str , 0 , langs ... )
283
+ // IsTranslatedD reports whether a domain string is translated
284
+ func IsTranslatedD (dom , str string ) bool {
285
+ return IsTranslatedND (dom , str , 0 )
341
286
}
342
287
343
- // IsTranslatedND reports whether a plural domain string is translated in any of given languages.
344
- // When the langs argument is omitted, the output of GetLanguages is used.
345
- func IsTranslatedND (dom , str string , n int , langs ... string ) bool {
346
- if len (langs ) == 0 {
347
- langs = GetLanguages ()
348
- }
349
-
288
+ // IsTranslatedND reports whether a plural domain string is translated
289
+ func IsTranslatedND (dom , str string , n int ) bool {
350
290
loadStorage (false )
351
291
352
292
globalConfig .RLock ()
353
293
defer globalConfig .RUnlock ()
354
294
355
- for _ , lang := range langs {
356
- lang = SimplifiedLocale (lang )
357
-
358
- for _ , supportedLocale := range globalConfig .locales {
359
- if lang != supportedLocale .GetActualLanguage (dom ) {
360
- continue
361
- }
362
- return supportedLocale .IsTranslatedND (dom , str , n )
363
- }
295
+ if _ , ok := globalConfig .storage .Domains [dom ]; ! ok {
296
+ globalConfig .storage .AddDomain (dom )
364
297
}
365
- return false
366
- }
367
298
368
- // IsTranslatedC reports whether a context string is translated in given languages.
369
- // When the langs argument is omitted, the output of GetLanguages is used.
370
- func IsTranslatedC (str , ctx string , langs ... string ) bool {
371
- return IsTranslatedNDC (GetDomain (), str , 0 , ctx , langs ... )
299
+ return globalConfig .storage .IsTranslatedND (dom , str , n )
372
300
}
373
301
374
- // IsTranslatedNC reports whether a plural context string is translated in given languages.
375
- // When the langs argument is omitted, the output of GetLanguages is used.
376
- func IsTranslatedNC (str string , n int , ctx string , langs ... string ) bool {
377
- return IsTranslatedNDC (GetDomain (), str , n , ctx , langs ... )
302
+ // IsTranslatedC reports whether a context string is translated
303
+ func IsTranslatedC (str , ctx string ) bool {
304
+ return IsTranslatedNDC (GetDomain (), str , 0 , ctx )
378
305
}
379
306
380
- // IsTranslatedDC reports whether a domain context string is translated in given languages.
381
- // When the langs argument is omitted, the output of GetLanguages is used.
382
- func IsTranslatedDC (dom , str , ctx string , langs ... string ) bool {
383
- return IsTranslatedNDC (dom , str , 0 , ctx , langs ... )
307
+ // IsTranslatedNC reports whether a plural context string is translated
308
+ func IsTranslatedNC (str string , n int , ctx string ) bool {
309
+ return IsTranslatedNDC (GetDomain (), str , n , ctx )
384
310
}
385
311
386
- // IsTranslatedNDC reports whether a plural domain context string is translated in any of given languages.
387
- // When the langs argument is omitted, the output of GetLanguages is used.
388
- func IsTranslatedNDC (dom , str string , n int , ctx string , langs ... string ) bool {
389
- if len (langs ) == 0 {
390
- langs = GetLanguages ()
391
- }
312
+ // IsTranslatedDC reports whether a domain context string is translated
313
+ func IsTranslatedDC (dom , str , ctx string ) bool {
314
+ return IsTranslatedNDC (dom , str , 0 , ctx )
315
+ }
392
316
317
+ // IsTranslatedNDC reports whether a plural domain context string is translated
318
+ func IsTranslatedNDC (dom , str string , n int , ctx string ) bool {
393
319
loadStorage (false )
394
320
395
321
globalConfig .RLock ()
396
322
defer globalConfig .RUnlock ()
397
323
398
- for _ , lang := range langs {
399
- lang = SimplifiedLocale (lang )
400
-
401
- for _ , locale := range globalConfig .locales {
402
- if lang != locale .GetActualLanguage (dom ) {
403
- continue
404
- }
405
- return locale .IsTranslatedNDC (dom , str , n , ctx )
406
- }
407
- }
408
- return false
324
+ return globalConfig .storage .IsTranslatedNDC (dom , str , n , ctx )
409
325
}
0 commit comments