From ef71a44d2d6c4033b48d88469c4911104af83ff1 Mon Sep 17 00:00:00 2001 From: "Rob Swindell (on Debian Linux)" <rob@synchro.net> Date: Mon, 14 Oct 2024 19:06:50 -0700 Subject: [PATCH] Add optional friendly-key name argument, pull text from [scriptname.js] Not all strings make good key names (e.g. those with control characters), so allow an additional, optional argument to gettext() that allows for more friendly key names to be used to specify/look-up (some) text strings. Also, in addition to the global [js] section for these text values, look (first) in [scriptfilename.js] - this allows more customization on a per-script basis (e.g. [default.js] can be used to override strings used in the Synchronet Classic command shell (default.js) but would not effect if/when those same strings are used in other scripts/shells. --- exec/load/gettext.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/exec/load/gettext.js b/exec/load/gettext.js index dbd7401f80..7c7cb3b9cd 100644 --- a/exec/load/gettext.js +++ b/exec/load/gettext.js @@ -1,30 +1,35 @@ // Localized/customized support for JavaScript user-visible text (i.e. strings not contained in text.dat) -// Customized text strings go in the [JS] section of ctrl/text.ini -// Localized (translated to non-default locale) text strings go in the [JS] section of ctrl/text.<lang>.ini +// Customized text strings go in the [scriptfilename.js] or [JS] sections of ctrl/text.ini +// Localized (translated to non-default locale) text strings go in the equivalent sections of ctrl/text.<lang>.ini + +// For strings that contain control characters, +// an optional 'key' is typically used/specified to look-up those text strings. "use strict"; var gettext_cache = {}; -function gettext(orig) { - function get_text_from_ini(ini_fname, orig) { +function gettext(orig, key) { + function get_text_from_ini(ini_fname, orig, key){ var f = new File(ini_fname); if(!f.open("r")) return undefined; - var text = f.iniGetValue("js", orig); + var text = f.iniGetValue(js.exec_file, key || orig); + if (text === undefined) + text = f.iniGetValue("js", key || orig); f.close(); return text; } - if (gettext_cache[orig] !== undefined) - return gettext_cache[orig]; + if (gettext_cache[key || orig] !== undefined) + return gettext_cache[key || orig]; var text; if (user.lang) - text = get_text_from_ini("text." + user.lang + ".ini", orig); + text = get_text_from_ini("text." + user.lang + ".ini", orig, key); if (text === undefined) - text = get_text_from_ini("text.ini", orig); + text = get_text_from_ini("text.ini", orig, key); if (text === undefined) text = orig; - return gettext_cache[orig] = text; + return gettext_cache[key || orig] = text; } this; -- GitLab