update theme

This commit is contained in:
Christoph Cullmann 2025-07-18 21:45:32 +02:00
parent fc43dba964
commit 93fea0ed81
No known key found for this signature in database
183 changed files with 6015 additions and 6241 deletions

View file

@ -1,5 +1,5 @@
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
const fs = require("fs/promises");
const translate = require("@iamtraction/google-translate");
const configDir = "./exampleSite/config/_default";
const contentDir = "./exampleSite/content";
@ -7,98 +7,104 @@ 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 createConfigs() {
const files = await fs.readdir(configDir);
for (const file of files) {
const filePath = `${configDir}/${file}`;
if (
filePath.indexOf("languages.en.toml") > -1 ||
filePath.indexOf("menus.en.toml") > -1
) {
const fileContent = await fs.readFile(filePath, "utf8");
await fs.writeFile(
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)
.catch(err => {
console.error(err);
});;
return translated_text && translated_text.text? translated_text.text : '';
const options = { from, to };
const translated_text = await translate(text, options).catch((err) => {
console.error(err);
});
return translated_text && translated_text.text ? 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";
const array = block.split("\n");
let translatedBlock = "";
for (const line of array) {
let newElement = line;
if (line.indexOf(":") > -1) {
const elements = line.split(":");
if (
elements[0] == "title" ||
elements[0] == "description" ||
elements[0] == "summary" ||
elements[0] == "description" ||
elements[0] == "categories" ||
elements[0] == "tags"
) {
const translatedElement = elements[1]
? await convert(elements[1], defaultLang, targetLang)
: elements[1];
newElement = elements[0] + ": " + translatedElement;
}
}
return translatedBlock;
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
async function processFile(filePath) {
console.log("translating", filePath)
if (filePath.indexOf("index.md") > -1) {
console.log("translating", filePath);
if (filePath.indexOf("index.md") > -1) {
const targetFilePath = filePath.replace(
".md",
"." + targetLangIso + ".md"
);
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
const fileContent = await fs.readFile(filePath, "utf8");
const fileContent = fs.readFileSync(filePath, 'utf8');
const array = fileContent.split("---\n");
const frontMatter = array[1];
const content = array[2];
var array = fileContent.split("---\n")
var frontMatter = array[1];
var content = array[2];
const translatedFrontMatter = await processFrontMatter(frontMatter);
const translatedContent = await convert(content, defaultLang, targetLang);
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
const newFileContent =
"---\n" + translatedFrontMatter + "---\n" + translatedContent;
await fs.writeFile(targetFilePath, newFileContent, "utf8");
} else {
return;
}
}
async function processFolder(folderPath) {
const files = fs.readdirSync(folderPath);
const files = await fs.readdir(folderPath);
for (var i in files) {
const file = files[i];
const filePath = `${folderPath}/${file}`;
const isDir = fs.lstatSync(filePath).isDirectory();
console.log(filePath, isDir);
if (isDir) {
await processFolder(filePath);
} else {
await processFile(filePath);
}
for (const file of files) {
const filePath = `${folderPath}/${file}`;
const isDir = (await fs.lstat(filePath)).isDirectory();
console.log(filePath, isDir);
if (isDir) {
await processFolder(filePath);
} else {
await processFile(filePath);
}
}
}
async function createContent() {
processFolder(contentDir);
await processFolder(contentDir);
}
createConfigs();
createContent();
(async () => {
await createConfigs();
await createContent();
})();