Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/npm/update-localization-extension.ts
4770 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as i18n from '../lib/i18n.ts';
7
import fs from 'fs';
8
import path from 'path';
9
import gulp from 'gulp';
10
import vfs from 'vinyl-fs';
11
import rimraf from 'rimraf';
12
import minimist from 'minimist';
13
14
interface Options {
15
_: string[];
16
location?: string;
17
externalExtensionsLocation?: string;
18
}
19
20
interface PackageJson {
21
contributes?: {
22
localizations?: Localization[];
23
};
24
}
25
26
interface Localization {
27
languageId: string;
28
languageName: string;
29
localizedLanguageName: string;
30
translations?: Array<{ id: string; path: string }>;
31
}
32
33
interface TranslationPath {
34
id: string;
35
resourceName: string;
36
}
37
38
function update(options: Options) {
39
const idOrPath = options._[0];
40
if (!idOrPath) {
41
throw new Error('Argument must be the location of the localization extension.');
42
}
43
const location = options.location;
44
if (location !== undefined && !fs.existsSync(location)) {
45
throw new Error(`${location} doesn't exist.`);
46
}
47
const externalExtensionsLocation = options.externalExtensionsLocation;
48
if (externalExtensionsLocation !== undefined && !fs.existsSync(externalExtensionsLocation)) {
49
throw new Error(`${externalExtensionsLocation} doesn't exist.`);
50
}
51
let locExtFolder: string = idOrPath;
52
if (/^\w{2,3}(-\w+)?$/.test(idOrPath)) {
53
locExtFolder = path.join('..', 'vscode-loc', 'i18n', `vscode-language-pack-${idOrPath}`);
54
}
55
const locExtStat = fs.statSync(locExtFolder);
56
if (!locExtStat || !locExtStat.isDirectory) {
57
throw new Error('No directory found at ' + idOrPath);
58
}
59
const packageJSON = JSON.parse(fs.readFileSync(path.join(locExtFolder, 'package.json')).toString()) as PackageJson;
60
const contributes = packageJSON['contributes'];
61
if (!contributes) {
62
throw new Error('The extension must define a "localizations" contribution in the "package.json"');
63
}
64
const localizations = contributes['localizations'];
65
if (!localizations) {
66
throw new Error('The extension must define a "localizations" contribution of type array in the "package.json"');
67
}
68
69
localizations.forEach(function (localization) {
70
if (!localization.languageId || !localization.languageName || !localization.localizedLanguageName) {
71
throw new Error('Each localization contribution must define "languageId", "languageName" and "localizedLanguageName" properties.');
72
}
73
let languageId = localization.languageId;
74
const translationDataFolder = path.join(locExtFolder, 'translations');
75
76
switch (languageId) {
77
case 'zh-cn':
78
languageId = 'zh-Hans';
79
break;
80
case 'zh-tw':
81
languageId = 'zh-Hant';
82
break;
83
case 'pt-br':
84
languageId = 'pt-BR';
85
break;
86
}
87
88
if (fs.existsSync(translationDataFolder) && fs.existsSync(path.join(translationDataFolder, 'main.i18n.json'))) {
89
console.log('Clearing \'' + translationDataFolder + '\'...');
90
rimraf.sync(translationDataFolder);
91
}
92
93
console.log(`Importing translations for ${languageId} form '${location}' to '${translationDataFolder}' ...`);
94
let translationPaths: TranslationPath[] | undefined = [];
95
gulp.src([
96
path.join(location!, '**', languageId, '*.xlf'),
97
...i18n.EXTERNAL_EXTENSIONS.map((extensionId: string) => path.join(externalExtensionsLocation!, extensionId, languageId, '*-new.xlf'))
98
], { silent: false })
99
.pipe(i18n.prepareI18nPackFiles(translationPaths))
100
.on('error', (error: unknown) => {
101
console.log(`Error occurred while importing translations:`);
102
translationPaths = undefined;
103
if (Array.isArray(error)) {
104
error.forEach(console.log);
105
} else if (error) {
106
console.log(error);
107
} else {
108
console.log('Unknown error');
109
}
110
})
111
.pipe(vfs.dest(translationDataFolder))
112
.on('end', function () {
113
if (translationPaths !== undefined) {
114
localization.translations = [];
115
for (const tp of translationPaths) {
116
localization.translations.push({ id: tp.id, path: `./translations/${tp.resourceName}` });
117
}
118
fs.writeFileSync(path.join(locExtFolder, 'package.json'), JSON.stringify(packageJSON, null, '\t') + '\n');
119
}
120
});
121
});
122
}
123
if (path.basename(process.argv[1]) === 'update-localization-extension.js') {
124
const options = minimist(process.argv.slice(2), {
125
string: ['location', 'externalExtensionsLocation']
126
}) as Options;
127
update(options);
128
}
129
130