Skip to content

Commit ee6558b

Browse files
authored
Add task to generate package.nls.json from English package xlf (#17233)
* Fix for paths of package.nls.*.json * added function to generate package.nls.json file from localizedPackage.json.enu.xlf * added generate-eng-package.nls * removed console.log from task * added message for build task needing to be run twice * moved require package to inside gulp task * remove duplicate message * added suggested changes * removed unused function * added comment * added detailed explanation comments and consolidation
1 parent af183bf commit ee6558b

File tree

2 files changed

+80
-55
lines changed

2 files changed

+80
-55
lines changed

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ gulp.task('ext:copy-js', () => {
236236
// Copy the files which aren't used in compilation
237237
gulp.task('ext:copy', gulp.series('ext:copy-tests', 'ext:copy-js', 'ext:copy-config', 'ext:copy-systemjs-config', 'ext:copy-dependencies', 'ext:copy-html', 'ext:copy-css', 'ext:copy-images'));
238238

239-
gulp.task('ext:localization', gulp.series('ext:localization:xliff-to-ts', 'ext:localization:xliff-to-json', 'ext:localization:xliff-to-package.nls'));
239+
gulp.task('ext:localization', gulp.series('ext:localization:generate-eng-package.nls', 'ext:localization:xliff-to-ts', 'ext:localization:xliff-to-json', 'ext:localization:xliff-to-package.nls'));
240240

241241
gulp.task('ext:build', gulp.series('ext:localization', 'ext:copy', 'ext:clean-library-ts-files', 'ext:compile', 'ext:compile-view')); // removed lint before copy
242242

tasks/localizationtasks.js

+79-54
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
var os = require('os');
12
var dom = require('xmldom').DOMParser
23
var gulp = require('gulp')
34
var config = require('./config')
45
var through = require('through2')
5-
var packageAllKeys = require('./../package.nls.json')
66

77
const iso639_3_to_2 = {
88
chs: 'zh-cn',
@@ -72,11 +72,6 @@ function escapeChars(input, escapeChar = true) {
7272
}
7373
}
7474

75-
// converts plain text json into a json object
76-
function convertJsonToDictionary(jsonInput) {
77-
return JSON.parse(jsonInput);
78-
}
79-
8075
// export json files from *.xlf
8176
// mirrors the file paths and names
8277
gulp.task('ext:localization:xliff-to-json', function () {
@@ -133,11 +128,11 @@ gulp.task('ext:localization:xliff-to-ts', function () {
133128
contents.push(instantiation);
134129
}
135130
}
136-
// end the function
131+
137132
contents.push('};');
138133

139134
// Join with new lines in between
140-
let fullFileContents = contents.join('\r\n') + '\r\n';
135+
let fullFileContents = contents.join(os.EOL) + os.EOL;
141136
file.contents = new Buffer(fullFileContents);
142137

143138
// Name our file
@@ -149,61 +144,91 @@ gulp.task('ext:localization:xliff-to-ts', function () {
149144
.pipe(gulp.dest(config.paths.project.root + '/src/constants/'));
150145
});
151146

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);
171151

172-
value = dict[key]['target'];
173-
}
174-
if (value === '') {
175-
value = packageAllKeys[key];
176-
}
152+
var contents = ['{'];
153+
var regxForReplacingQuotes = new RegExp('"', 'g');
177154

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;
183157

184-
});
158+
// If running in English package.nls.json mode:
185159

186-
// end the function
187-
contents.push('}');
160+
// Get keys from the XLF dictionary and use them directly for the file content.
188161

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:
192163

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

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

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) {
206228

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);
207232
// callback to notify we have completed the current file
208233
callback(null, file);
209234
}))

0 commit comments

Comments
 (0)