mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 23:45:59 +00:00
feat: add add-missing command to translations cli
This commit is contained in:
43
common/translations-cli/addMissing.js
Normal file
43
common/translations-cli/addMissing.js
Normal file
@@ -0,0 +1,43 @@
|
||||
//@ts-check
|
||||
const { getDefaultTranslations, getLanguageTranslations } = require('./helpers');
|
||||
|
||||
/**
|
||||
* @param {string} language
|
||||
*/
|
||||
function getMissingTranslations(language) {
|
||||
const source = getDefaultTranslations();
|
||||
/** @type {Record<string, string>} */
|
||||
let target;
|
||||
|
||||
try {
|
||||
target = getLanguageTranslations(language);
|
||||
} catch {
|
||||
console.log(`Language ${language} not found, creating a new one`);
|
||||
target = {};
|
||||
}
|
||||
|
||||
let added = 0;
|
||||
let removed = 0;
|
||||
|
||||
for (const key in source) {
|
||||
if (!target[key]) {
|
||||
target[key] = `*** ${source[key]}`;
|
||||
added++;
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in target) {
|
||||
if (!source[key]) {
|
||||
delete target[key];
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
|
||||
const newLength = Object.keys(target).length;
|
||||
|
||||
return { result: target, stats: { added, removed, newLength } };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMissingTranslations,
|
||||
};
|
||||
@@ -122,6 +122,46 @@ const getTranslationChanges = (existingTranslations, newTranslations) => {
|
||||
return { added, removed, updated };
|
||||
};
|
||||
|
||||
const defaultLanguage = 'en-US';
|
||||
|
||||
function getDefaultTranslations() {
|
||||
return getLanguageTranslations(defaultLanguage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} language
|
||||
*
|
||||
* @returns {Record<string, string>}
|
||||
*/
|
||||
function getLanguageTranslations(language) {
|
||||
const file = resolveFile(`translations/${language}.json`);
|
||||
const content = fs.readFileSync(file, 'utf-8');
|
||||
|
||||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} language
|
||||
* @param {Record<string, string>} translations
|
||||
*/
|
||||
function setLanguageTranslations(language, translations) {
|
||||
const file = resolveFile(`translations/${language}.json`);
|
||||
fs.writeFileSync(file, JSON.stringify(translations, null, 2));
|
||||
}
|
||||
|
||||
function getAllLanguages() {
|
||||
const dir = resolveFile('translations');
|
||||
|
||||
const files = fs.readdirSync(dir);
|
||||
const languages = files.filter(file => file.endsWith('.json')).map(file => file.replace('.json', ''));
|
||||
|
||||
return languages;
|
||||
}
|
||||
|
||||
function getAllNonDefaultLanguages() {
|
||||
return getAllLanguages().filter(language => language !== defaultLanguage);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hasValidExtension,
|
||||
getFiles,
|
||||
@@ -131,4 +171,9 @@ module.exports = {
|
||||
resolveExtensions,
|
||||
ensureFileDirExists,
|
||||
getTranslationChanges,
|
||||
getDefaultTranslations,
|
||||
getLanguageTranslations,
|
||||
setLanguageTranslations,
|
||||
getAllLanguages,
|
||||
getAllNonDefaultLanguages,
|
||||
};
|
||||
|
||||
@@ -7,15 +7,18 @@ const {
|
||||
resolveFile,
|
||||
ensureFileDirExists,
|
||||
getTranslationChanges,
|
||||
setLanguageTranslations,
|
||||
getAllNonDefaultLanguages,
|
||||
} = require('./helpers');
|
||||
const { extractAllTranslations } = require('./extract');
|
||||
const { getMissingTranslations } = require('./addMissing');
|
||||
|
||||
/**
|
||||
* @typedef {{ extensions: string[], directories: string[], outputFile: string}} Config
|
||||
* @typedef {Config & { verbose?: boolean }} Options
|
||||
* @typedef {{ extensions: string[], directories: string[], outputFile: string}} ExtractConfig
|
||||
* @typedef {ExtractConfig & { verbose?: boolean }} ExtractOptions
|
||||
*/
|
||||
|
||||
/** @type {Config} */
|
||||
/** @type {ExtractConfig} */
|
||||
const defaultConfig = {
|
||||
extensions: ['.js', '.ts', '.svelte'],
|
||||
directories: ['app', 'packages/web'],
|
||||
@@ -31,7 +34,7 @@ program
|
||||
.option('-e, --extensions <extensions...>', 'file extensions to process', defaultConfig.extensions)
|
||||
.option('-o, --outputFile <file>', 'output file path', defaultConfig.outputFile)
|
||||
.option('-v, --verbose', 'verbose mode')
|
||||
.action(async (/** @type {Options} */ options) => {
|
||||
.action(async (/** @type {ExtractOptions} */ options) => {
|
||||
try {
|
||||
const { directories, extensions, outputFile, verbose } = options;
|
||||
|
||||
@@ -86,4 +89,42 @@ program
|
||||
}
|
||||
});
|
||||
|
||||
const ALL_LANGUAGES = 'all';
|
||||
|
||||
/**
|
||||
* @param {string} target
|
||||
*/
|
||||
function addMissingTranslations(target) {
|
||||
console.log(`Adding missing keys for language: ${target}`);
|
||||
const { result, stats } = getMissingTranslations(target);
|
||||
console.log(`Added: ${stats.added}, Removed: ${stats.removed}, Total: ${stats.newLength}`);
|
||||
setLanguageTranslations(target, result);
|
||||
console.log(`New translations for ${target} were saved.`);
|
||||
}
|
||||
|
||||
program
|
||||
.command('add-missing')
|
||||
.description('Add missing keys for a langauge to the translation file')
|
||||
.option('-t, --target <target>', 'language to add missing translations to', ALL_LANGUAGES)
|
||||
.action(options => {
|
||||
try {
|
||||
const { target } = options;
|
||||
const languages = getAllNonDefaultLanguages();
|
||||
|
||||
if (target === ALL_LANGUAGES) {
|
||||
console.log('Adding missing keys for all languages\n');
|
||||
for (const language of languages) {
|
||||
addMissingTranslations(language);
|
||||
console.log();
|
||||
}
|
||||
} else {
|
||||
addMissingTranslations(target);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error('Error during add-missing:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = { program };
|
||||
|
||||
Reference in New Issue
Block a user