1+ /*
2+ buildlanguage.js - ESP3D WebUI language file builder
3+
4+ Copyright (c) 2021 Luc Lebosse. All rights reserved.
5+
6+ This code is free software; you can redistribute it and/or
7+ modify it under the terms of the GNU Lesser General Public
8+ License as published by the Free Software Foundation; either
9+ version 2.1 of the License, or (at your option) any later version.
10+ This code is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+ Lesser General Public License for more details.
14+ You should have received a copy of the GNU Lesser General Public
15+ License along with This code; if not, write to the Free Software
16+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+ */
18+ const chalk = require ( "chalk" )
19+ const path = require ( "path" )
20+ const fs = require ( "fs" )
21+ const { Compress } = require ( "gzipper" )
22+
23+ // Vérification des arguments
24+ if ( process . argv . length !== 3 ) {
25+ console . log ( chalk . red ( "Usage: node buildlanguage.js <translation-file.json>" ) )
26+ process . exit ( 1 )
27+ }
28+
29+ const languageFile = process . argv [ 2 ]
30+ const languagesPath = path . normalize ( __dirname + "/../languages/" )
31+
32+ // Liste des packs à traiter
33+ const targetPacks = [ "printerpack" , "cncgrblpack" , "cncgrblhalpack" , "sandtablepack" ]
34+
35+ // Lecture du fichier de traduction source
36+ let translations
37+ try {
38+ const translationPath = path . join ( languagesPath , languageFile )
39+ const translationContent = fs . readFileSync ( translationPath , "UTF-8" )
40+ translations = JSON . parse ( translationContent )
41+ console . log ( chalk . green ( `Loaded translations from ${ languageFile } ` ) )
42+ } catch ( error ) {
43+ console . log ( chalk . red ( `Error loading translation file: ${ error . message } ` ) )
44+ process . exit ( 1 )
45+ }
46+
47+ // Fonction pour compresser un fichier
48+ async function compressFile ( filePath ) {
49+ try {
50+ const sourceDir = path . dirname ( filePath )
51+ const gzipper = new Compress ( filePath , sourceDir , { verbose : false } )
52+ await gzipper . run ( )
53+
54+ // Renommer le fichier .gz pour correspondre au nom original
55+ fs . renameSync ( `${ filePath } .gz` , `${ filePath } .gz` )
56+
57+ const originalSize = fs . statSync ( filePath ) . size
58+ const compressedSize = fs . statSync ( `${ filePath } .gz` ) . size
59+
60+ // Afficher le message de génération du fichier gz
61+ console . log ( chalk . green ( `Generated ${ path . basename ( filePath ) } .gz for ${ path . basename ( path . dirname ( filePath ) ) } ` ) )
62+
63+ console . log ( chalk . blue ( `Compression for ${ path . basename ( filePath ) } :` ) )
64+ console . log (
65+ chalk . blue (
66+ `- Original: ${ originalSize } Bytes => Compressed: ${ compressedSize } Bytes` ,
67+ `(${ ( 100 - 100 * ( compressedSize / originalSize ) ) . toFixed ( 2 ) } % reduction)`
68+ )
69+ )
70+ } catch ( error ) {
71+ console . log ( chalk . red ( `Error compressing ${ filePath } : ${ error . message } ` ) )
72+ }
73+ }
74+
75+ // Traitement de chaque pack
76+ const processAllPacks = async ( ) => {
77+ for ( const packName of targetPacks ) {
78+ try {
79+ // Lecture du fichier en.json de référence pour ce pack
80+ const referenceFile = path . join ( languagesPath , packName , "en.json" )
81+ const referenceContent = fs . readFileSync ( referenceFile , "UTF-8" )
82+ const reference = JSON . parse ( referenceContent )
83+
84+ // Création du nouveau fichier de traduction
85+ const newTranslations = { }
86+
87+ // Ajout de l'identifiant de la target
88+ newTranslations [ "target_lang" ] = packName
89+
90+ // Copie des traductions correspondantes
91+ Object . keys ( reference ) . forEach ( key => {
92+ // Skip if reference value is null
93+ if ( reference [ key ] === null ) {
94+ console . log ( chalk . yellow ( `Skipping key "${ key } " in ${ packName } - null value in reference` ) )
95+ return
96+ }
97+
98+ const translationValue = translations [ key ]
99+ // Skip if translation is null or empty string
100+ if ( ! translationValue || translationValue === "" ) {
101+ console . log ( chalk . yellow ( `Skipping key "${ key } " in ${ packName } - translation empty or missing` ) )
102+ return
103+ }
104+
105+ // Add valid translation
106+ newTranslations [ key ] = translationValue
107+ } )
108+
109+ // Création du répertoire si nécessaire
110+ const outputDir = path . join ( languagesPath , packName )
111+ if ( ! fs . existsSync ( outputDir ) ) {
112+ fs . mkdirSync ( outputDir , { recursive : true } )
113+ }
114+
115+ // Sauvegarde du nouveau fichier
116+ const outputFile = path . join ( outputDir , languageFile )
117+ fs . writeFileSync ( outputFile , JSON . stringify ( newTranslations , null , 1 ) , "UTF-8" )
118+
119+ console . log ( chalk . green ( `Generated ${ languageFile } for ${ packName } ` ) )
120+
121+ // Compression du fichier généré
122+ await compressFile ( outputFile )
123+
124+ // Affichage des statistiques
125+ const totalKeys = Object . keys ( reference ) . length
126+ const translatedKeys = Object . keys ( newTranslations ) . length - 1 // -1 pour target_lang
127+ console . log ( chalk . blue ( `Statistics for ${ packName } :` ) )
128+ console . log ( chalk . blue ( `- Total keys: ${ totalKeys } ` ) )
129+ console . log ( chalk . blue ( `- Translated keys: ${ translatedKeys } ` ) )
130+ console . log ( chalk . blue ( `- Translation completion: ${ ( ( translatedKeys / totalKeys ) * 100 ) . toFixed ( 2 ) } %` ) )
131+
132+ } catch ( error ) {
133+ console . log ( chalk . red ( `Error processing ${ packName } : ${ error . message } ` ) )
134+ }
135+ }
136+ }
137+
138+ // Exécution du script
139+ processAllPacks ( ) . then ( ( ) => {
140+ console . log ( chalk . green ( "Translation processing and compression completed" ) )
141+ } )
0 commit comments