@@ -71,70 +71,20 @@ function trueOrMatch(needle, haystack) {
71
71
return false ;
72
72
}
73
73
74
-
75
- /**
76
- * Loads the dictionaries from the locale directory.
77
- * @param {Object } options
78
- */
79
- function load ( options ) {
80
- if ( cache [ options . locales ] ) {
81
- options . verbose && gutil . log ( 'Skip loading cached translations from' , options . locales ) ;
82
- return dictionaries = cache [ options . locales ] ;
83
- }
84
- try {
85
- options . verbose && gutil . log ( 'Loading translations from' , options . locales ) ;
86
- var files = fs . readdirSync ( options . locales ) ;
87
- var count = 0 ;
88
- for ( var i in files ) {
89
- var file = files [ i ] ;
90
- switch ( path . extname ( file ) ) {
91
- case '.json' :
92
- case '.js' :
93
- dictionaries [ path . basename ( file , path . extname ( file ) ) ] = flat ( require ( path . join ( process . cwd ( ) , options . locales , file ) ) ) ;
94
- options . verbose && gutil . log ( 'Added translations from' , file ) ;
95
- count ++ ;
96
- break ;
97
- case '.ini' :
98
- var iniData = fs . readFileSync ( path . join ( process . cwd ( ) , options . locales , file ) ) ;
99
- dictionaries [ path . basename ( file , path . extname ( file ) ) ] = flat ( ini2json ( iniData ) ) ;
100
- options . verbose && gutil . log ( 'Added translations from' , file ) ;
101
- count ++ ;
102
- break ;
103
- case '.csv' :
104
- var csvData = fs . readFileSync ( path . join ( process . cwd ( ) , options . locales , file ) ) ;
105
- dictionaries [ path . basename ( file , path . extname ( file ) ) ] = csv2json ( csvData ) ;
106
- options . verbose && gutil . log ( 'Added translations from' , file ) ;
107
- count ++ ;
108
- break ;
109
- default :
110
- options . verbose && gutil . log ( 'Ignored file' , file ) ;
111
- }
112
- }
113
- options . verbose && gutil . log ( 'Loaded' , count , 'translations from' , options . locales ) ;
114
- if ( options . cache ) {
115
- options . verbose && gutil . log ( 'Cashing translations from' , options . locales ) ;
116
- cache [ options . locales ] = dictionaries ;
117
- }
118
- } catch ( e ) {
119
- e . message = 'No translation dictionaries have been found!' ;
120
- throw e ;
121
- }
122
- }
123
-
124
74
/**
125
75
* Splits a line from an ini file into 2. Any subsequent '=' are ignored.
126
76
* @param {String } line
127
77
* @returns {String[] }
128
78
*/
129
79
function splitIniLine ( line ) {
130
80
var separator = line . indexOf ( '=' ) ;
131
- if ( separator == - 1 ) {
81
+ if ( separator === - 1 ) {
132
82
return [ line ] ;
133
83
}
134
84
return [
135
85
line . substr ( 0 , separator ) ,
136
86
line . substr ( separator + 1 )
137
- ]
87
+ ] ;
138
88
}
139
89
140
90
/**
@@ -152,8 +102,8 @@ function ini2json(iniData) {
152
102
fields [ j ] = fields [ j ] . trim ( ) ;
153
103
}
154
104
if ( fields [ 0 ] . length ) {
155
- if ( fields [ 0 ] . indexOf ( '[' ) == 0 ) {
156
- context = fields [ 0 ] . substring ( 1 , fields [ 0 ] . length - 1 )
105
+ if ( fields [ 0 ] . indexOf ( '[' ) === 0 ) {
106
+ context = fields [ 0 ] . substring ( 1 , fields [ 0 ] . length - 1 ) ;
157
107
} else {
158
108
if ( context ) {
159
109
if ( ! result [ context ] ) {
@@ -228,6 +178,55 @@ function csv2json(csvData) {
228
178
return result ;
229
179
}
230
180
181
+ /**
182
+ * Loads the dictionaries from the locale directory.
183
+ * @param {Object } options
184
+ */
185
+ function load ( options ) {
186
+ if ( cache [ options . locales ] ) {
187
+ options . verbose && gutil . log ( 'Skip loading cached translations from' , options . locales ) ;
188
+ return dictionaries = cache [ options . locales ] ;
189
+ }
190
+ try {
191
+ options . verbose && gutil . log ( 'Loading translations from' , options . locales ) ;
192
+ var files = fs . readdirSync ( options . locales ) ;
193
+ var count = 0 ;
194
+ for ( var i in files ) {
195
+ var file = files [ i ] ;
196
+ switch ( path . extname ( file ) ) {
197
+ case '.json' :
198
+ case '.js' :
199
+ dictionaries [ path . basename ( file , path . extname ( file ) ) ] = flat ( require ( path . join ( process . cwd ( ) , options . locales , file ) ) ) ;
200
+ options . verbose && gutil . log ( 'Added translations from' , file ) ;
201
+ count ++ ;
202
+ break ;
203
+ case '.ini' :
204
+ var iniData = fs . readFileSync ( path . join ( process . cwd ( ) , options . locales , file ) ) ;
205
+ dictionaries [ path . basename ( file , path . extname ( file ) ) ] = flat ( ini2json ( iniData ) ) ;
206
+ options . verbose && gutil . log ( 'Added translations from' , file ) ;
207
+ count ++ ;
208
+ break ;
209
+ case '.csv' :
210
+ var csvData = fs . readFileSync ( path . join ( process . cwd ( ) , options . locales , file ) ) ;
211
+ dictionaries [ path . basename ( file , path . extname ( file ) ) ] = csv2json ( csvData ) ;
212
+ options . verbose && gutil . log ( 'Added translations from' , file ) ;
213
+ count ++ ;
214
+ break ;
215
+ default :
216
+ options . verbose && gutil . log ( 'Ignored file' , file ) ;
217
+ }
218
+ }
219
+ options . verbose && gutil . log ( 'Loaded' , count , 'translations from' , options . locales ) ;
220
+ if ( options . cache ) {
221
+ options . verbose && gutil . log ( 'Cashing translations from' , options . locales ) ;
222
+ cache [ options . locales ] = dictionaries ;
223
+ }
224
+ } catch ( e ) {
225
+ e . message = 'No translation dictionaries have been found!' ;
226
+ throw e ;
227
+ }
228
+ }
229
+
231
230
/**
232
231
* Helper function that detects whether a buffer is binary or utf8.
233
232
* @param {Buffer } buffer
@@ -304,10 +303,10 @@ function translate(options, contents, copied, filePath) {
304
303
i = next ;
305
304
}
306
305
}
307
- for ( var lang in processed ) {
308
- if ( ! processed [ lang ] . length ) {
309
- options . verbose && gutil . log ( 'Copying original content to target language' , lang , 'because no replacements have happened' ) ;
310
- processed [ lang ] = contents ;
306
+ for ( var procLang in processed ) {
307
+ if ( ! processed [ procLang ] . length ) {
308
+ options . verbose && gutil . log ( 'Copying original content to target language' , procLang , 'because no replacements have happened' ) ;
309
+ processed [ procLang ] = contents ;
311
310
}
312
311
}
313
312
return processed ;
0 commit comments