Improve whitespace handling in translations

The HTML source will include line breaks and indentation that is only
for source formatting, and will not be displayed.
This commit is contained in:
Pierre Ossman 2022-12-27 15:25:06 +01:00
parent 367bfd2962
commit 022fc8c374
2 changed files with 14 additions and 3 deletions

View File

@ -103,13 +103,20 @@ export class Localizer {
return items.indexOf(searchElement) !== -1; return items.indexOf(searchElement) !== -1;
} }
function translateString(str) {
// We assume surrounding whitespace, and whitespace around line
// breaks is just for source formatting
str = str.split("\n").map(s => s.trim()).join(" ").trim();
return self.get(str);
}
function translateAttribute(elem, attr) { function translateAttribute(elem, attr) {
const str = self.get(elem.getAttribute(attr)); const str = translateString(elem.getAttribute(attr));
elem.setAttribute(attr, str); elem.setAttribute(attr, str);
} }
function translateTextNode(node) { function translateTextNode(node) {
const str = self.get(node.data.trim()); const str = translateString(node.data);
node.data = str; node.data = str;
} }

View File

@ -17,6 +17,10 @@ const opt = getopt.create([
const strings = {}; const strings = {};
function addString(str, location) { function addString(str, location) {
// We assume surrounding whitespace, and whitespace around line
// breaks, is just for source formatting
str = str.split("\n").map(s => s.trim()).join(" ").trim();
if (str.length == 0) { if (str.length == 0) {
return; return;
} }
@ -78,7 +82,7 @@ function process(elem, locator, enabled) {
if (node.nodeType === node.ELEMENT_NODE) { if (node.nodeType === node.ELEMENT_NODE) {
process(node, locator, enabled); process(node, locator, enabled);
} else if (node.nodeType === node.TEXT_NODE && enabled) { } else if (node.nodeType === node.TEXT_NODE && enabled) {
addString(node.data.trim(), locator(node)); addString(node.data, locator(node));
} }
} }
} }