Skip to content

Commit

Permalink
Add more supported languages
Browse files Browse the repository at this point in the history
Add new scripts to build language packs
  • Loading branch information
luc-github committed Dec 21, 2024
1 parent c145b01 commit 046b037
Show file tree
Hide file tree
Showing 148 changed files with 46,950 additions and 18 deletions.
141 changes: 141 additions & 0 deletions config/buildlangpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
buildlanguage.js - ESP3D WebUI language file builder
Copyright (c) 2021 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
const chalk = require("chalk")
const path = require("path")
const fs = require("fs")
const { Compress } = require("gzipper")

// Vérification des arguments
if (process.argv.length !== 3) {
console.log(chalk.red("Usage: node buildlanguage.js <translation-file.json>"))
process.exit(1)
}

const languageFile = process.argv[2]
const languagesPath = path.normalize(__dirname + "/../languages/")

// Liste des packs à traiter
const targetPacks = ["printerpack", "cncgrblpack", "cncgrblhalpack", "sandtablepack"]

// Lecture du fichier de traduction source
let translations
try {
const translationPath = path.join(languagesPath, languageFile)
const translationContent = fs.readFileSync(translationPath, "UTF-8")
translations = JSON.parse(translationContent)
console.log(chalk.green(`Loaded translations from ${languageFile}`))
} catch (error) {
console.log(chalk.red(`Error loading translation file: ${error.message}`))
process.exit(1)
}

// Fonction pour compresser un fichier
async function compressFile(filePath) {
try {
const sourceDir = path.dirname(filePath)
const gzipper = new Compress(filePath, sourceDir, { verbose: false })
await gzipper.run()

// Renommer le fichier .gz pour correspondre au nom original
fs.renameSync(`${filePath}.gz`, `${filePath}.gz`)

const originalSize = fs.statSync(filePath).size
const compressedSize = fs.statSync(`${filePath}.gz`).size

// Afficher le message de génération du fichier gz
console.log(chalk.green(`Generated ${path.basename(filePath)}.gz for ${path.basename(path.dirname(filePath))}`))

console.log(chalk.blue(`Compression for ${path.basename(filePath)}:`))
console.log(
chalk.blue(
`- Original: ${originalSize} Bytes => Compressed: ${compressedSize} Bytes`,
`(${(100 - 100 * (compressedSize / originalSize)).toFixed(2)}% reduction)`
)
)
} catch (error) {
console.log(chalk.red(`Error compressing ${filePath}: ${error.message}`))
}
}

// Traitement de chaque pack
const processAllPacks = async () => {
for (const packName of targetPacks) {
try {
// Lecture du fichier en.json de référence pour ce pack
const referenceFile = path.join(languagesPath, packName, "en.json")
const referenceContent = fs.readFileSync(referenceFile, "UTF-8")
const reference = JSON.parse(referenceContent)

// Création du nouveau fichier de traduction
const newTranslations = {}

// Ajout de l'identifiant de la target
newTranslations["target_lang"] = packName

// Copie des traductions correspondantes
Object.keys(reference).forEach(key => {
// Skip if reference value is null
if (reference[key] === null) {
console.log(chalk.yellow(`Skipping key "${key}" in ${packName} - null value in reference`))
return
}

const translationValue = translations[key]
// Skip if translation is null or empty string
if (!translationValue || translationValue === "") {
console.log(chalk.yellow(`Skipping key "${key}" in ${packName} - translation empty or missing`))
return
}

// Add valid translation
newTranslations[key] = translationValue
})

// Création du répertoire si nécessaire
const outputDir = path.join(languagesPath, packName)
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}

// Sauvegarde du nouveau fichier
const outputFile = path.join(outputDir, languageFile)
fs.writeFileSync(outputFile, JSON.stringify(newTranslations, null, 1), "UTF-8")

console.log(chalk.green(`Generated ${languageFile} for ${packName}`))

// Compression du fichier généré
await compressFile(outputFile)

// Affichage des statistiques
const totalKeys = Object.keys(reference).length
const translatedKeys = Object.keys(newTranslations).length - 1 // -1 pour target_lang
console.log(chalk.blue(`Statistics for ${packName}:`))
console.log(chalk.blue(`- Total keys: ${totalKeys}`))
console.log(chalk.blue(`- Translated keys: ${translatedKeys}`))
console.log(chalk.blue(`- Translation completion: ${((translatedKeys/totalKeys)*100).toFixed(2)}%`))

} catch (error) {
console.log(chalk.red(`Error processing ${packName}: ${error.message}`))
}
}
}

// Exécution du script
processAllPacks().then(() => {
console.log(chalk.green("Translation processing and compression completed"))
})
18 changes: 17 additions & 1 deletion config/buildtemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const cncgrblhalpack = [
]
const printerpack = [
{ id: "globals", path: "src/targets/translations/en.json" },

{
id: "printers3D",
path: "src/targets/Printer3D/translations/en.json",
Expand Down Expand Up @@ -86,8 +85,13 @@ const processList = [
{ targetPath: "printerpack", files: printerpack },
{ targetPath: "sandtablepack", files: sandtablepack },
]

// Object to store all unique translations
let masterTranslations = {}

if (readable) console.log(chalk.green("Processing in readable mode"))
else console.log(chalk.green("Processing in production mode"))

processList.map((element) => {
let resultfile = {}

Expand All @@ -101,6 +105,10 @@ processList.map((element) => {
Object.keys(currentFile).map((key) => {
if (!resultfile[key] && typeof currentFile[key] != "undefined") {
resultfile[key] = currentFile[key]
// Add to master translations if not already present
if (!masterTranslations[key]) {
masterTranslations[key] = currentFile[key]
}
} else {
if (resultfile[key] != currentFile[key]) {
console.log(chalk.red("In " + sourcepath))
Expand Down Expand Up @@ -132,4 +140,12 @@ processList.map((element) => {
)
})

// Save master translations file
console.log(chalk.green("Saving master translations file"))
fs.writeFileSync(
path.join(path.normalize(__dirname + "/../languages/"), "master_translations.json"),
JSON.stringify(masterTranslations, "", readable ? " " : ""),
"UTF-8"
)

console.log(chalk.green("Processing done"))
Binary file removed dist/Plotter/HP-GL/index.html.gz
Binary file not shown.
4 changes: 1 addition & 3 deletions languages/cncgrblhalpack/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"S12": "About %s",
"S13": "Dashboard",
"S14": "Settings",
"S15": "Donation for ESP3D",
"S16": "Firmware",
"S17": "Interface",
"S18": "Browser version",
Expand Down Expand Up @@ -146,8 +145,6 @@
"S140": "GCODE command",
"S141": "File name",
"S142": "Command",
"S143": "SD",
"S144": "SD",
"S145": "Authentication Required",
"S146": "User Name",
"S147": "Password",
Expand Down Expand Up @@ -395,6 +392,7 @@
"resume_script": "Resume script",
"stop_script": "Stop script",
"polling_on": "Polling enabled",
"wifi mode": "Wifi mode",
"language": "Language",
"CN1": "mm/min",
"CN2": "XY feed rate",
Expand Down
Loading

0 comments on commit 046b037

Please sign in to comment.