Skip to content

Commit 37f474c

Browse files
Merge pull request #84 from leonelquinteros/revert-73-multilang
Revert "Add support for multiple languages"
2 parents dfd3da1 + 4c92353 commit 37f474c

File tree

3 files changed

+76
-210
lines changed

3 files changed

+76
-210
lines changed

gotext.go

+74-158
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,54 @@ package gotext
2424

2525
import (
2626
"encoding/gob"
27-
"strings"
2827
"sync"
2928
)
3029

3130
// Global environment variables
3231
type config struct {
3332
sync.RWMutex
3433

35-
// Path to library directory where all locale directories and Translation files are.
36-
library string
37-
3834
// Default domain to look at when no domain is specified. Used by package level functions.
3935
domain string
4036

4137
// 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
4342

4443
// Storage for package level methods
45-
locales []*Locale
44+
storage *Locale
4645
}
4746

4847
var globalConfig *config
4948

5049
func init() {
5150
// Init default configuration
5251
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,
5756
}
5857

5958
// Register Translator types for gob encoding
6059
gob.Register(TranslatorEncoding{})
6160
}
6261

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.
6563
// It's called automatically when trying to use Get or GetD methods.
6664
func loadStorage(force bool) {
6765
globalConfig.Lock()
6866

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)
7569
}
7670

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)
8273
}
74+
globalConfig.storage.SetDomain(globalConfig.domain)
8375

8476
globalConfig.Unlock()
8577
}
@@ -88,9 +80,8 @@ func loadStorage(force bool) {
8880
func GetDomain() string {
8981
var dom string
9082
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()
9485
}
9586
if dom == "" {
9687
dom = globalConfig.domain
@@ -105,42 +96,28 @@ func GetDomain() string {
10596
func SetDomain(dom string) {
10697
globalConfig.Lock()
10798
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)
112101
}
113102
globalConfig.Unlock()
114103

115104
loadStorage(true)
116105
}
117106

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
120108
func GetLanguage() string {
121-
return GetLanguages()[0]
122-
}
123-
124-
// GetLanguages returns all languages that have been supplied.
125-
func GetLanguages() []string {
126109
globalConfig.RLock()
127-
defer globalConfig.RUnlock()
128-
return globalConfig.languages
110+
lang := globalConfig.language
111+
globalConfig.RUnlock()
112+
113+
return lang
129114
}
130115

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.
132117
// It reloads the corresponding Translation file.
133118
func SetLanguage(lang string) {
134119
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)
144121
globalConfig.Unlock()
145122

146123
loadStorage(true)
@@ -155,7 +132,7 @@ func GetLibrary() string {
155132
return lib
156133
}
157134

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.
159136
// It reloads the corresponding Translation file.
160137
func SetLibrary(lib string) {
161138
globalConfig.Lock()
@@ -196,15 +173,7 @@ func SetStorage(storage *Locale) {
196173
func Configure(lib, lang, dom string) {
197174
globalConfig.Lock()
198175
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)
208177
globalConfig.domain = dom
209178
globalConfig.Unlock()
210179

@@ -229,20 +198,16 @@ func GetD(dom, str string, vars ...interface{}) string {
229198
// Try to load default package Locale storage
230199
loadStorage(false)
231200

201+
// Return Translation
232202
globalConfig.RLock()
233-
defer globalConfig.RUnlock()
234203

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)
245206
}
207+
208+
tr := globalConfig.storage.GetD(dom, str, vars...)
209+
globalConfig.RUnlock()
210+
246211
return tr
247212
}
248213

@@ -252,20 +217,16 @@ func GetND(dom, str, plural string, n int, vars ...interface{}) string {
252217
// Try to load default package Locale storage
253218
loadStorage(false)
254219

220+
// Return Translation
255221
globalConfig.RLock()
256-
defer globalConfig.RUnlock()
257222

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)
268225
}
226+
227+
tr := globalConfig.storage.GetND(dom, str, plural, n, vars...)
228+
globalConfig.RUnlock()
229+
269230
return tr
270231
}
271232

@@ -287,17 +248,11 @@ func GetDC(dom, str, ctx string, vars ...interface{}) string {
287248
// Try to load default package Locale storage
288249
loadStorage(false)
289250

251+
// Return Translation
290252
globalConfig.RLock()
291-
defer globalConfig.RUnlock()
253+
tr := globalConfig.storage.GetDC(dom, str, ctx, vars...)
254+
globalConfig.RUnlock()
292255

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-
}
301256
return tr
302257
}
303258

@@ -309,101 +264,62 @@ func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) str
309264

310265
// Return Translation
311266
globalConfig.RLock()
312-
defer globalConfig.RUnlock()
267+
tr := globalConfig.storage.GetNDC(dom, str, plural, n, ctx, vars...)
268+
globalConfig.RUnlock()
313269

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-
}
322270
return tr
323271
}
324272

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)
329276
}
330277

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)
335281
}
336282

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)
341286
}
342287

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 {
350290
loadStorage(false)
351291

352292
globalConfig.RLock()
353293
defer globalConfig.RUnlock()
354294

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)
364297
}
365-
return false
366-
}
367298

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)
372300
}
373301

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)
378305
}
379306

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)
384310
}
385311

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+
}
392316

317+
// IsTranslatedNDC reports whether a plural domain context string is translated
318+
func IsTranslatedNDC(dom, str string, n int, ctx string) bool {
393319
loadStorage(false)
394320

395321
globalConfig.RLock()
396322
defer globalConfig.RUnlock()
397323

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)
409325
}

0 commit comments

Comments
 (0)