translate-table
Version:
A powerful CLI tool for translating PostgreSQL table content to multiple languages and generating translations for internationalization (i18n).
110 lines • 4.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const citty_1 = require("citty");
const consola_1 = __importDefault(require("consola"));
const db_1 = require("./lib/db");
const sql_1 = require("./utils/sql");
const translate_1 = require("./utils/translate");
/**
* Available languages for translation
*/
const AVAILABLE_LANGUAGES = [
{ label: "English (en)", value: "en" },
{ label: "Spanish (es)", value: "es" },
{ label: "Portuguese (pt)", value: "pt" },
{ label: "French (fr)", value: "fr" },
{ label: "German (de)", value: "de" },
{ label: "Japanese (ja)", value: "ja" },
];
/**
* Gets table values and performs translation
*
* @param tableName - Table name
* @param originalLanguage - Original language of the texts
* @param targetLanguages - Languages to translate to
* @returns A Promise with translation results
*/
async function processTranslation(url, tableName, originalLanguage, targetLanguages) {
// Fetch data from database
consola_1.default.start("Connecting to database...");
const values = await (0, db_1.getTableAndValues)(url, tableName);
if (!values.length) {
throw new Error(`No values found in table ${tableName}`);
}
consola_1.default.info(`Received ${values.length} records from database`);
// Perform translation
consola_1.default.start(`Translating ${values.length} items from ${originalLanguage.toUpperCase()} to ${targetLanguages
.map((value) => value.toUpperCase())
.join(", ")}`);
const translatedText = await (0, translate_1.translateText)(values, targetLanguages, originalLanguage.toLowerCase());
consola_1.default.success("Translation completed!");
return translatedText;
}
/**
* Main CLI command
*/
const translateCommand = (0, citty_1.defineCommand)({
meta: {
name: "translate",
version: "0.0.1",
description: "Translate database tables to other languages",
},
args: {
url: {
type: "string",
description: "Database connection URL",
required: true,
},
table: {
type: "string",
description: "Table to translate",
required: true,
},
"original-language": {
type: "string",
description: "Language of the text to translate. Put the language code (You can find the list of language codes in https://github.com/AidanWelch/google-translate-api/blob/master/lib/languages.cjs)",
required: true,
},
sql: {
type: "boolean",
description: "Output SQL instead of database insertion. (default: false)",
default: false,
},
},
async run({ args }) {
try {
// Request language selection for translation
const selectedLanguages = (await consola_1.default.prompt("Select languages", {
type: "multiselect",
required: true,
options: AVAILABLE_LANGUAGES,
}));
if (!selectedLanguages.length) {
consola_1.default.error("No languages selected. Operation canceled.");
return;
}
const { "original-language": originalLanguage, table, sql, url } = args;
// Process translation
const translatedText = await processTranslation(url, table, originalLanguage, selectedLanguages);
// Save results (SQL or database)
if (sql) {
(0, sql_1.generateFiles)(table, translatedText);
}
else {
consola_1.default.start("Inserting translated values into database...");
await (0, db_1.insertIntoDatabase)(url, table, translatedText);
}
}
catch (error) {
consola_1.default.error("Error during process:", error.message);
process.exit(1);
}
},
});
// Run the main command
(0, citty_1.runMain)(translateCommand);
//# sourceMappingURL=index.js.map