1
+ var os = require ( 'os' ) ;
1
2
var dom = require ( 'xmldom' ) . DOMParser
2
3
var gulp = require ( 'gulp' )
3
4
var config = require ( './config' )
4
5
var through = require ( 'through2' )
5
- var packageAllKeys = require ( './../package.nls.json' )
6
6
7
7
const iso639_3_to_2 = {
8
8
chs : 'zh-cn' ,
@@ -72,11 +72,6 @@ function escapeChars(input, escapeChar = true) {
72
72
}
73
73
}
74
74
75
- // converts plain text json into a json object
76
- function convertJsonToDictionary ( jsonInput ) {
77
- return JSON . parse ( jsonInput ) ;
78
- }
79
-
80
75
// export json files from *.xlf
81
76
// mirrors the file paths and names
82
77
gulp . task ( 'ext:localization:xliff-to-json' , function ( ) {
@@ -133,11 +128,11 @@ gulp.task('ext:localization:xliff-to-ts', function () {
133
128
contents . push ( instantiation ) ;
134
129
}
135
130
}
136
- // end the function
131
+
137
132
contents . push ( '};' ) ;
138
133
139
134
// Join with new lines in between
140
- let fullFileContents = contents . join ( '\r\n' ) + '\r\n' ;
135
+ let fullFileContents = contents . join ( os . EOL ) + os . EOL ;
141
136
file . contents = new Buffer ( fullFileContents ) ;
142
137
143
138
// Name our file
@@ -149,61 +144,91 @@ gulp.task('ext:localization:xliff-to-ts', function () {
149
144
. pipe ( gulp . dest ( config . paths . project . root + '/src/constants/' ) ) ;
150
145
} ) ;
151
146
152
- // Generates a localized package.nls.*.json
153
- gulp . task ( 'ext:localization:xliff-to-package.nls' , function ( ) {
154
- return gulp . src ( [ config . paths . project . localization + '/xliff/**/localizedPackage.json.*.xlf' , '!' + config . paths . project . localization + '/xliff/enu/localizedPackage.json.enu.xlf' ] , { base : '' } )
155
- . pipe ( through . obj ( function ( file , enc , callback ) {
156
- // convert xliff into json document
157
- let dict = convertXmlToDictionary ( String ( file . contents ) , false ) ;
158
-
159
- var contents = [ '{' ] ;
160
- var regxForReplacingQuots = new RegExp ( '"' , 'g' ) ;
161
-
162
- // Get all the keys from package.nls.json which is the English version and get the localized value from xlf
163
- // Use the English value if not translated, right now there's no fall back to English if the text is not localized.
164
- // So all the keys have to exist in all package.nls.*.json
165
- Object . keys ( packageAllKeys ) . forEach ( key => {
166
- let value = packageAllKeys [ key ] ;
167
- if ( contents . length >= 2 ) {
168
- contents [ contents . length - 1 ] += ',' ;
169
- }
170
- if ( dict . hasOwnProperty ( key ) ) {
147
+ // Main function used for creating the package.nls.json files, both the original English and localizedFiles.
148
+ function dictionaryMapping ( file , packageAllKeys = undefined ) {
149
+ // convert xliff into json document (get the contents of the input XLF file)
150
+ let dict = convertXmlToDictionary ( String ( file . contents ) , false ) ;
171
151
172
- value = dict [ key ] [ 'target' ] ;
173
- }
174
- if ( value === '' ) {
175
- value = packageAllKeys [ key ] ;
176
- }
152
+ var contents = [ '{' ] ;
153
+ var regxForReplacingQuotes = new RegExp ( '"' , 'g' ) ;
177
154
178
- if ( value && value . indexOf ( '"' ) >= 0 ) {
179
- value = value . replace ( regxForReplacingQuots , '\'' ) ;
180
- }
181
- let instantiation = '"' + key + '":"' + value + '"' ;
182
- contents . push ( instantiation ) ;
155
+ // If packageAllKeys is provided, that means we are updating a localized package.nls.*.json file.
156
+ let mainKeys = packageAllKeys ? packageAllKeys : dict ;
183
157
184
- } ) ;
158
+ // If running in English package.nls.json mode:
185
159
186
- // end the function
187
- contents . push ( '}' ) ;
160
+ // Get keys from the XLF dictionary and use them directly for the file content.
188
161
189
- // Join with new lines in between
190
- let fullFileContents = contents . join ( '\r\n' ) + '\r\n' ;
191
- file . contents = new Buffer ( fullFileContents ) ;
162
+ // If running in Localized package.nls.*.json mode:
192
163
193
- let indexToStart = 'localizedPackage.json.' . length + 1 ;
194
- let languageIndex = file . basename . indexOf ( '.' , indexToStart ) ;
195
- let language = file . basename . substr ( indexToStart - 1 , ( languageIndex - indexToStart ) + 1 ) ;
164
+ // Get all the keys from package.nls.json which is the English version and get the localized value from xlf
165
+ // Use the English value if not translated, right now there's no fall back to English if the text is not localized.
166
+ // So all the keys have to exist in all package.nls.*.json
167
+ Object . keys ( mainKeys ) . forEach ( key => {
168
+ let value = '' ;
169
+ if ( contents . length >= 2 ) {
170
+ contents [ contents . length - 1 ] += ',' ;
171
+ }
172
+ if ( dict . hasOwnProperty ( key ) ) {
173
+ value = dict [ key ] [ 'target' ] ;
174
+ }
175
+ if ( packageAllKeys && value === '' ) {
176
+ // If localizing and value is not provided, use original English value.
177
+ value = packageAllKeys [ key ] ;
178
+ }
196
179
197
- // Name our file
198
- if ( language === 'enu' ) {
199
- file . basename = 'package.nls.json' ;
200
- } else {
201
- file . basename = 'package.nls.' + iso639_3_to_2 [ language ] + '.json' ;
202
- }
180
+ if ( value && value . indexOf ( '"' ) >= 0 ) {
181
+ value = value . replace ( regxForReplacingQuotes , '\'' ) ;
182
+ }
183
+ let instantiation = '"' + key + '":"' + value + '"' ;
184
+ contents . push ( instantiation ) ;
185
+
186
+ } ) ;
187
+
188
+ contents . push ( '}' ) ;
189
+
190
+ // Join with new lines in between
191
+ let fullFileContents = contents . join ( os . EOL ) + os . EOL ;
192
+ file . contents = new Buffer ( fullFileContents ) ;
193
+
194
+ let indexToStart = 'localizedPackage.json.' . length + 1 ;
195
+ let languageIndex = file . basename . indexOf ( '.' , indexToStart ) ;
196
+ let language = file . basename . substr ( indexToStart - 1 , ( languageIndex - indexToStart ) + 1 ) ;
197
+
198
+ // Name our file
199
+ if ( language === 'enu' ) {
200
+ file . basename = 'package.nls.json' ;
201
+ } else {
202
+ file . basename = 'package.nls.' + iso639_3_to_2 [ language ] + '.json' ;
203
+ }
204
+
205
+ // Make the new file create on root.
206
+ // the original directory path was 'vscode-mssql\localization\xliff\<lang>'
207
+ file . dirname = file . dirname . replace ( language , '' ) . replace ( 'localization' , '' ) . replace ( 'xliff' , '' ) ;
208
+ }
203
209
204
- // Make the new file create on root
205
- file . dirname = file . dirname . replace ( language , '' ) ;
210
+ // Generates a package.nls.json file from localizedPackage.json.enu.xlf by using the
211
+ // dictionaryMapping function to write the contents of the xlf file into package.nls.json.
212
+ gulp . task ( 'ext:localization:generate-eng-package.nls' , function ( ) {
213
+ return gulp . src ( [ config . paths . project . localization + '/xliff/enu/localizedPackage.json.enu.xlf' ] , { base : '.' } )
214
+ . pipe ( through . obj ( function ( file , enc , callback ) {
215
+ dictionaryMapping ( file ) ;
216
+ // callback to notify we have completed the current file
217
+ callback ( null , file ) ;
218
+ } ) )
219
+ . pipe ( gulp . dest ( config . paths . project . root ) ) ;
220
+ } )
221
+
222
+ // Generates a localized package.nls.*.json file from localizedPackage.json.*.xlf files by
223
+ // using the dictionaryMapping function to replace contents of the package.nls.json file with
224
+ // localized content if possible and write them to a new localized package.nls file.
225
+ gulp . task ( 'ext:localization:xliff-to-package.nls' , function ( ) {
226
+ return gulp . src ( [ config . paths . project . localization + '/xliff/**/localizedPackage.json.*.xlf' , '!' + config . paths . project . localization + '/xliff/enu/localizedPackage.json.enu.xlf' ] , { base : '.' } )
227
+ . pipe ( through . obj ( function ( file , enc , callback ) {
206
228
229
+ // Get the latest changes from package.nls.json after being updated (base English strings).
230
+ var packageAllKeys = require ( './../package.nls.json' )
231
+ dictionaryMapping ( file , packageAllKeys ) ;
207
232
// callback to notify we have completed the current file
208
233
callback ( null , file ) ;
209
234
} ) )
0 commit comments