From aa7f589553787328c1c9cd20e6d798e6b4d16d3e Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 21:18:42 +0200 Subject: rename js/set_dom_strings.js to localize.js --- src/app/js/localize.js | 69 +++++++++++++++++++++++++++++++++++++++++++ src/app/js/set_dom_strings.js | 67 ----------------------------------------- src/app/main.js | 2 +- 3 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 src/app/js/localize.js delete mode 100644 src/app/js/set_dom_strings.js (limited to 'src/app') diff --git a/src/app/js/localize.js b/src/app/js/localize.js new file mode 100644 index 0000000..6776566 --- /dev/null +++ b/src/app/js/localize.js @@ -0,0 +1,69 @@ +// localizes `string`, removing instances of `%%string%%` with +// `lang("string")` and so forth +function localize_string(string) { + let parts = string.split("%%"); + + // basic checks to make sure `string` has lang strings + if (parts.length == 0 + || string.trim() == "" || ! string.match("%%")) { + return string; + } + + for (let i = 0; i < parts.length; i++) { + // simply checks to make sure it is actually a lang string. + if (parts[i][0] != " " && + parts[i][parts[i].length - 1] != " ") { + + // get string + let lang_str = lang(parts[i]); + + // make sure we got a string back, and if not, do nothing + if (typeof lang_str !== "string") { + continue; + } + + // replace this part with the lang string + parts[i] = lang_str; + } + } + + // return finalized formatted string + return parts.join(""); +} + +// runs `localize_string()` on `el`'s attributes, text nodes and children +function localize_el(el) { + // we don't want to mess with script tags + if (el.tagName == "SCRIPT") {return} + + let attributes = el.getAttributeNames(); + + // run through child nodes + for (let i = 0; i < el.childNodes.length; i++) { + // if the node isn't a text node, we do nothing + if (el.childNodes[i].nodeType != Node.TEXT_NODE) { + continue; + } + + // the node is a text node, so we set its `.textContent` by + // running `format_string()` on it + el.childNodes[i].textContent = + localize_string(el.childNodes[i].textContent) + } + + // run through attributes and run `format_string()` on their values + for (let i = 0; i < attributes.length; i++) { + let attr = el.getAttribute(attributes[i]); + el.setAttribute(attributes[i], localize_string(attr)) + } + + // run `replace_in_el()` on `el`'s children + for (let i = 0; i < el.children.length; i++) { + localize_el(el.children[i]); + } +} + +// localizes lang strings on (almost) all the elements inside `` +module.exports = () => { + localize_el(document.body); +} diff --git a/src/app/js/set_dom_strings.js b/src/app/js/set_dom_strings.js deleted file mode 100644 index e039ebd..0000000 --- a/src/app/js/set_dom_strings.js +++ /dev/null @@ -1,67 +0,0 @@ -function format_string(string) { - let parts = string.split("%%"); - - // basic checks to make sure `string` has lang strings - if (parts.length == 0 - || string.trim() == "" || ! string.match("%%")) { - return string; - } - - for (let i = 0; i < parts.length; i++) { - // simply checks to make sure it is actually a lang string. - if (parts[i][0] != " " && - parts[i][parts[i].length - 1] != " ") { - - // get string - let lang_str = lang(parts[i]); - - // make sure we got a string back, and if not, do nothing - if (typeof lang_str !== "string") { - continue; - } - - // replace this part with the lang string - parts[i] = lang_str; - } - } - - // return finalized formatted string - return parts.join(""); -} - -// runs `format_string()` on `el`'s attributes, text nodes and children -function replace_in_el(el) { - // we don't want to mess with script tags - if (el.tagName == "SCRIPT") {return} - - let attributes = el.getAttributeNames(); - - // run through child nodes - for (let i = 0; i < el.childNodes.length; i++) { - // if the node isn't a text node, we do nothing - if (el.childNodes[i].nodeType != Node.TEXT_NODE) { - continue; - } - - // the node is a text node, so we set its `.textContent` by - // running `format_string()` on it - el.childNodes[i].textContent = - format_string(el.childNodes[i].textContent) - } - - // run through attributes and run `format_string()` on their values - for (let i = 0; i < attributes.length; i++) { - let attr = el.getAttribute(attributes[i]); - el.setAttribute(attributes[i], format_string(attr)) - } - - // run `replace_in_el()` on `el`'s children - for (let i = 0; i < el.children.length; i++) { - replace_in_el(el.children[i]); - } -} - -// replaces lang strings on (almost) all the elements inside `` -module.exports = () => { - replace_in_el(document.body); -} diff --git a/src/app/main.js b/src/app/main.js index b33c88b..3adb96d 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -45,4 +45,4 @@ const is_running = require("./js/is_running"); const set_buttons = require("./js/set_buttons"); require("./js/dom_events"); -require("./js/set_dom_strings")(); +require("./js/localize")(); -- cgit v1.2.3