From d9a92748ea9779058cee87ac8b379426d99cdb8b Mon Sep 17 00:00:00 2001 From: "Rob Swindell (on Debian Linux)" <rob@synchro.net> Date: Sun, 24 Sep 2023 02:22:19 -0700 Subject: [PATCH] Script that can load additional ctrl/text.*.ini (or .json) text override files e.g. as a logon script "?replace_text es" would replace strings from ctrl/text.es.ini (for Spanish translations) This is not really to be used to load ctrl/text.ini since that is support is built in (in load_cfg.c) and not necessary to be done in JS. --- exec/replace_text.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 exec/replace_text.js diff --git a/exec/replace_text.js b/exec/replace_text.js new file mode 100755 index 0000000000..8498c69e43 --- /dev/null +++ b/exec/replace_text.js @@ -0,0 +1,62 @@ +// Performs text.dat string replacements from ctrl/text.*.ini and/or ctrl/text.*.json + +// one or more replacement files must be passed on the command-line in the form: +// ctrl/text.<arg>.ini or ctrl/text.<arg>.json + +/*** example text.json: +{ + "On": "En", + "Off": "Apagado", + "Yes": "Si", + "None": "Nada", + "All": "Todo", + "Only": "Solamente", + "Unlimited": "ilimitado", + "Scanning": "Escaneo", + "Done": "Hecho", +} +******************************************/ + +/*** example text.ini (quotes are optional, CP437 encoded): + On: Auf + Off: Aus + Yes: Ja + None: Nichts + All: Alle + Only: Nur + Unlimited: unbergrenzt + Scanning: Abtastung + Done: Fertig +******************************************/ + +const text = require('text.js', 'TOTAL_TEXT'); + +"use strict"; + +function replace_text(fname) { + const ext = file_getext(fname).toLowerCase(); + if(ext != ".ini" && ext != ".json") { + if(file_exists(fname + ".ini")) + return replace_text(fname + ".ini") + if(file_exists(fname + ".json")) + return replace_text(fname + ".json"); + throw new Error("Filename does not exist: " + fname); + } + const file = new File(fname); + if(!file.open('r')) + throw new Error("Error " + file.error + " opening " + file.name); + var obj; + if(ext == ".ini") + obj = file.iniGetObject(); + else if(ext == ".json") + obj = JSON.parse(file.readAll().join("\n")); + file.close(); + for(var i in obj) { + if(!bbs.replace_text(text[i], obj[i])) + throw new Error(format('Failed to replace string (%s = %s)', i, text[i])); + } + return true; +} + +for(var i in argv) + replace_text(system.ctrl_dir + "text." + argv[i]); -- GitLab