@@ -22,68 +22,110 @@ For quick/simple translations you can use the package level functions directly.
22
22
*/
23
23
package gotext
24
24
25
- import "fmt"
25
+ import (
26
+ "fmt"
27
+ "sync"
28
+ )
26
29
27
30
// Global environment variables
28
- var (
31
+ type config struct {
32
+ sync.RWMutex
33
+
29
34
// Default domain to look at when no domain is specified. Used by package level functions.
30
- domain = "default"
35
+ domain string
31
36
32
37
// Language set.
33
- language = "en_US"
38
+ language string
34
39
35
40
// Path to library directory where all locale directories and translation files are.
36
- library = "/usr/local/share/locale"
41
+ library string
37
42
38
43
// Storage for package level methods
39
44
storage * Locale
40
- )
45
+ }
46
+
47
+ var globalConfig * config
48
+
49
+ // Init default configuration
50
+ func init () {
51
+ globalConfig = & config {
52
+ domain : "default" ,
53
+ language : "en_US" ,
54
+ library : "/usr/local/share/locale" ,
55
+ storage : nil ,
56
+ }
57
+ }
41
58
42
59
// loadStorage creates a new Locale object at package level based on the Global variables settings.
43
60
// It's called automatically when trying to use Get or GetD methods.
44
61
func loadStorage (force bool ) {
45
- if storage == nil || force {
46
- storage = NewLocale (library , language )
62
+ globalConfig .Lock ()
63
+
64
+ if globalConfig .storage == nil || force {
65
+ globalConfig .storage = NewLocale (globalConfig .library , globalConfig .language )
47
66
}
48
67
49
- if _ , ok := storage .domains [domain ]; ! ok || force {
50
- storage .AddDomain (domain )
68
+ if _ , ok := globalConfig . storage .domains [globalConfig . domain ]; ! ok || force {
69
+ globalConfig . storage .AddDomain (globalConfig . domain )
51
70
}
71
+
72
+ globalConfig .Unlock ()
52
73
}
53
74
54
75
// GetDomain is the domain getter for the package configuration
55
76
func GetDomain () string {
56
- return domain
77
+ globalConfig .RLock ()
78
+ dom := globalConfig .domain
79
+ globalConfig .RUnlock ()
80
+
81
+ return dom
57
82
}
58
83
59
84
// SetDomain sets the name for the domain to be used at package level.
60
85
// It reloads the corresponding translation file.
61
86
func SetDomain (dom string ) {
62
- domain = dom
87
+ globalConfig .Lock ()
88
+ globalConfig .domain = dom
89
+ globalConfig .Unlock ()
90
+
63
91
loadStorage (true )
64
92
}
65
93
66
94
// GetLanguage is the language getter for the package configuration
67
95
func GetLanguage () string {
68
- return language
96
+ globalConfig .RLock ()
97
+ lang := globalConfig .language
98
+ globalConfig .RUnlock ()
99
+
100
+ return lang
69
101
}
70
102
71
103
// SetLanguage sets the language code to be used at package level.
72
104
// It reloads the corresponding translation file.
73
105
func SetLanguage (lang string ) {
74
- language = lang
106
+ globalConfig .Lock ()
107
+ globalConfig .language = lang
108
+ globalConfig .Unlock ()
109
+
75
110
loadStorage (true )
76
111
}
77
112
78
113
// GetLibrary is the library getter for the package configuration
79
114
func GetLibrary () string {
80
- return library
115
+ globalConfig .RLock ()
116
+ lib := globalConfig .library
117
+ globalConfig .RUnlock ()
118
+
119
+ return lib
81
120
}
82
121
83
122
// SetLibrary sets the root path for the loale directories and files to be used at package level.
84
123
// It reloads the corresponding translation file.
85
124
func SetLibrary (lib string ) {
86
- library = lib
125
+ globalConfig .Lock ()
126
+ globalConfig .library = lib
127
+ globalConfig .Unlock ()
128
+
87
129
loadStorage (true )
88
130
}
89
131
@@ -92,23 +134,27 @@ func SetLibrary(lib string) {
92
134
// This function is recommended to be used when changing more than one setting,
93
135
// as using each setter will introduce a I/O overhead because the translation file will be loaded after each set.
94
136
func Configure (lib , lang , dom string ) {
95
- library = lib
96
- language = lang
97
- domain = dom
137
+ globalConfig .Lock ()
138
+
139
+ globalConfig .library = lib
140
+ globalConfig .language = lang
141
+ globalConfig .domain = dom
142
+
143
+ globalConfig .Unlock ()
98
144
99
145
loadStorage (true )
100
146
}
101
147
102
148
// Get uses the default domain globally set to return the corresponding translation of a given string.
103
149
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
104
150
func Get (str string , vars ... interface {}) string {
105
- return GetD (domain , str , vars ... )
151
+ return GetD (GetDomain () , str , vars ... )
106
152
}
107
153
108
- // GetN retrieves the (N)th plural form of translation for the given string in the " default" domain.
154
+ // GetN retrieves the (N)th plural form of translation for the given string in the default domain.
109
155
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
110
156
func GetN (str , plural string , n int , vars ... interface {}) string {
111
- return GetND ("default" , str , plural , n , vars ... )
157
+ return GetND (GetDomain () , str , plural , n , vars ... )
112
158
}
113
159
114
160
// GetD returns the corresponding translation in the given domain for a given string.
@@ -124,19 +170,23 @@ func GetND(dom, str, plural string, n int, vars ...interface{}) string {
124
170
loadStorage (false )
125
171
126
172
// Return translation
127
- return storage .GetND (dom , str , plural , n , vars ... )
173
+ globalConfig .RLock ()
174
+ tr := globalConfig .storage .GetND (dom , str , plural , n , vars ... )
175
+ globalConfig .RUnlock ()
176
+
177
+ return tr
128
178
}
129
179
130
180
// GetC uses the default domain globally set to return the corresponding translation of the given string in the given context.
131
181
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
132
182
func GetC (str , ctx string , vars ... interface {}) string {
133
- return GetDC (domain , str , ctx , vars ... )
183
+ return GetDC (GetDomain () , str , ctx , vars ... )
134
184
}
135
185
136
- // GetNC retrieves the (N)th plural form of translation for the given string in the given context in the " default" domain.
186
+ // GetNC retrieves the (N)th plural form of translation for the given string in the given context in the default domain.
137
187
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
138
188
func GetNC (str , plural string , n int , ctx string , vars ... interface {}) string {
139
- return GetNDC ("default" , str , plural , n , ctx , vars ... )
189
+ return GetNDC (GetDomain () , str , plural , n , ctx , vars ... )
140
190
}
141
191
142
192
// GetDC returns the corresponding translation in the given domain for the given string in the given context.
@@ -152,7 +202,11 @@ func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) str
152
202
loadStorage (false )
153
203
154
204
// Return translation
155
- return storage .GetNDC (dom , str , plural , n , ctx , vars ... )
205
+ globalConfig .RLock ()
206
+ tr := globalConfig .storage .GetNDC (dom , str , plural , n , ctx , vars ... )
207
+ globalConfig .RUnlock ()
208
+
209
+ return tr
156
210
}
157
211
158
212
// printf applies text formatting only when needed to parse variables.
0 commit comments