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