added translation lib and scripts

This commit is contained in:
Nuno Coração 2024-03-10 22:42:10 +00:00
parent 554e25716c
commit 902b0d9c86
4 changed files with 198 additions and 2 deletions

63
genArticle.js Normal file
View file

@ -0,0 +1,63 @@
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
const defaultLang = "en";
const targetLang = process.argv[2] || "en";
const filePath = process.argv[3];
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
async function convert(text, from, to) {
var options = {
from: from,
to: to
};
var translated_text = await translate(text, options);
return translated_text.text;
}
console.log(filePath);
console.log(targetFilePath);
async function processFrontMatter(block) {
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0] == "title" ||
elements[0] == "description" ||
elements[0] == "summary" ||
elements[0] == "description" ||
elements[0] == "categories" ||
elements[0] == "tags") {
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
newElement = elements[0] + ": " + translatedElement;
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
async function main() {
const fileContent = fs.readFileSync(filePath, 'utf8');
var array = fileContent.split("---\n")
var frontMatter = array[1];
var content = array[2];
var translatedFrontMatter = await processFrontMatter(frontMatter);
var translatedContent = await convert(content, defaultLang, targetLang);
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
fs.writeFileSync(targetFilePath, newFileContent, 'utf8');
}
main();

101
genLang.js Normal file
View file

@ -0,0 +1,101 @@
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
const configDir = "./config/_default";
const contentDir = "./content";
const defaultLang = "en";
const targetLang = process.argv[2] || "en";
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
function createConfigs() {
const files = fs.readdirSync(configDir);
files.forEach(file => {
const filePath = `${configDir}/${file}`;
if (filePath.indexOf("languages.en.toml") > -1 ||
filePath.indexOf("menus.en.toml") > -1) {
var fileContent = fs.readFileSync(filePath, 'utf8');
fs.writeFileSync(filePath.replace(".en.toml", "." + targetLangIso + ".toml"), fileContent, 'utf8');
}
});
}
async function convert(text, from, to) {
var options = {
from: from,
to: to
};
var translated_text = await translate(text, options);
return translated_text.text;
}
async function processFrontMatter(block) {
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0] == "title" ||
elements[0] == "description" ||
elements[0] == "summary" ||
elements[0] == "description" ||
elements[0] == "categories" ||
elements[0] == "tags") {
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
newElement = elements[0] + ": " + translatedElement;
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
async function processFile(filePath) {
console.log("translating", filePath)
if (filePath.indexOf("index.md") > -1) {
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
const fileContent = fs.readFileSync(filePath, 'utf8');
var array = fileContent.split("---\n")
var frontMatter = array[1];
var content = array[2];
var translatedFrontMatter = await processFrontMatter(frontMatter);
var translatedContent = await convert(content, defaultLang, targetLang);
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
fs.writeFileSync(targetFilePath, newFileContent, 'utf8');
} else
return
}
async function processFolder(folderPath) {
const files = fs.readdirSync(folderPath);
for (var i in files) {
const file = files[i];
const filePath = `${folderPath}/${file}`;
const isDir = fs.lstatSync(filePath).isDirectory();
console.log(filePath, isDir);
console.log();
if (isDir) {
await processFolder(filePath);
} else {
await processFile(filePath);
}
}
}
async function createContent() {
processFolder(contentDir);
}
createConfigs();
createContent();

35
package-lock.json generated
View file

@ -1,17 +1,18 @@
{
"name": "hugo-blowfish-theme",
"version": "2.61.0",
"version": "2.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hugo-blowfish-theme",
"version": "2.61.0",
"version": "2.62.0",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"@headlessui/react": "^1.7.18",
"@heroicons/react": "^2.1.1",
"@iamtraction/google-translate": "^2.0.1",
"@tailwindcss/forms": "^0.5.7",
"commander": "^12.0.0"
},
@ -88,6 +89,14 @@
"integrity": "sha512-s3jaWicZd0pkP0jf5ysyHUI/RE7MHos6qlToFcGWXVp+ykHOy77OUMrfbgJ9it2C5bow7OIQwYYaHjk9XlBQ2A==",
"dev": true
},
"node_modules/@fastify/busboy": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz",
"integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==",
"engines": {
"node": ">=14"
}
},
"node_modules/@headlessui/react": {
"version": "1.7.18",
"resolved": "https://registry.npmjs.org/@headlessui/react/-/react-1.7.18.tgz",
@ -112,6 +121,17 @@
"react": ">= 16"
}
},
"node_modules/@iamtraction/google-translate": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@iamtraction/google-translate/-/google-translate-2.0.1.tgz",
"integrity": "sha512-kOTkt23zflxpgxLIe9tLvDU8IvNscXYbgMSXydNkZGWDtJ75YJiU3v7EBz+t7h/gSiY7sT5r4dlFkMrHCl+N6A==",
"dependencies": {
"undici": "^5.12.0"
},
"engines": {
"node": ">=16.0.0"
}
},
"node_modules/@isaacs/cliui": {
"version": "8.0.2",
"resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz",
@ -4118,6 +4138,17 @@
"through": "^2.3.8"
}
},
"node_modules/undici": {
"version": "5.28.3",
"resolved": "https://registry.npmjs.org/undici/-/undici-5.28.3.tgz",
"integrity": "sha512-3ItfzbrhDlINjaP0duwnNsKpDQk3acHI3gVJ1z4fmwMK31k5G9OVIAMLSIaP6w4FaGkaAkN6zaQO9LUvZ1t7VA==",
"dependencies": {
"@fastify/busboy": "^2.0.0"
},
"engines": {
"node": ">=14.0"
}
},
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",

View file

@ -110,6 +110,7 @@
"dependencies": {
"@headlessui/react": "^1.7.18",
"@heroicons/react": "^2.1.1",
"@iamtraction/google-translate": "^2.0.1",
"@tailwindcss/forms": "^0.5.7",
"commander": "^12.0.0"
}