Files
team/webapp/scripts/gen_lang_imports.mjs
Harrison HealeyandGitHub 0faee9ef75 Migrate most remaining files to TypeScript (#36954)
* Migrate actions/emoji_actions to TS

* Migrate i18n/i18n to TS

* Migrate selectors/admin_console and selectors/admin_console.test to TS

* Migrate actions/views/cookie to TS

* Migrate actions/views/group to TS

* Migrate actions/admin_actions to TS

* Migrate actions/telemetry_actions, actions/announcement_bar, actions/notice, actions/search, and actions/system to TS

* Migrate actions/views/mfa to TS

* Migrate actions/views/posts and actions/views/textbox to TS

* Migrate suggestion_box/index to TS

* Rename create_selector files to TS files

* Migrate create_selector files to TS

* Move langmap out of channels and change it to an ES Module

* Revert accidentally committed changes to authorize.test.tsx

* Remove unused recomputations and resetRecomputations from createSelector

* Fix lint

* Merge ESLint changes
2026-06-22 15:55:03 -04:00

79 lines
2.1 KiB
JavaScript

#!/bin/node
import * as fs from 'fs';
import langmap from './langmap.mjs';
let lines = '';
const langIDs = [];
const langFiles = {};
const langLabels = {};
fs.readdirSync('./channels/src/i18n').forEach(file => {
if (file.endsWith('.json')) {
const langID = file.substr(0, file.indexOf('.json'));
// Skip default language
if (langID === 'en') {
return
}
langIDs.push(langID);
lines += `import ${langID.replace('-', '')} from './${file}';\n`;
langFiles[langID] = langID.replace('-', '');
let m = langmap[langID]
if (!m) {
// We fallback to the language code prefix if we can't find a map.
const id = langID.includes('-') ? langID.substr(0, langID.indexOf('-')) : langID;
for (const k of Object.keys(langmap)) {
if (k.startsWith(id)) {
m = langmap[k];
break;
}
}
}
langLabels[langID] = m ? m["nativeName"] : langID;
}
});
lines += `
export const langIDs = ${JSON.stringify(langIDs)};
export const langLabels = ${JSON.stringify(langLabels)};
`;
// To generate the file exports we need to do a bit more work to handle ids with dashes and also output a map of literals rather than strings.
lines += `
// TypeScript thinks it's importing these language files' contents directly, but Webpack rewrites the above imports
// to the URL the file for lazy loading. That's the reason for the ugly type assertions below.
export const langFiles: {
[langID: string]: string;
} = {
` + Object.keys(langFiles).reduce((out, id, idx) => {
out += ' ';
if (id.includes('-')) {
out += `'${id}': ${langFiles[id]}`;
} else {
out += `${id}: ${langFiles[id]}`;
}
out += ' as unknown as string';
if (idx !== (langIDs.length - 1)) {
out += ',';
}
out += '\n';
return out;
}, '') + '};';
const header = `// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.\n\n// DO NOT EDIT - This file is generated by gen_lang_imports through the gen-lang-imports script\n\n/* eslint-disable */\n\n`;
fs.writeFileSync('./channels/src/i18n/imports.ts', header+lines);