fix sonar findings and translation structure
Sonarqube Scanner / Prepare environment (push) Successful in 19s
Sonarqube Scanner / Build and test (push) Successful in 43s
Sonarqube Scanner / SonarQube analysis (push) Failing after 2m28s
Sonarqube Scanner / Deploy to Server (push) Successful in 16s

This commit is contained in:
Florian Zumpe
2026-10-01 16:31:46 +02:00
parent 2b42507f13
commit 5c4d20bd1d
30 changed files with 1753 additions and 894 deletions
+3 -13
View File
@@ -1,31 +1,21 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import vm from 'node:vm';
import { localizeHtml } from '../scripts/localize-html.js';
const root = path.resolve(import.meta.dirname, '..');
export async function loadI18n(context) {
for (const name of ['texts-de', 'i18n']) {
for (const name of ['texts-de', 'texts-en', 'i18n']) {
const filename = path.join(root, `public/js/${name}.js`);
vm.runInNewContext(await fs.readFile(filename, 'utf8'), context, { filename });
}
return context.Wegwichtel.I18n;
}
const escapeText = value => value.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
export async function localizedHtml(source) {
const i18n = await loadI18n({});
let result = source.replace(/(<[^>]+\bdata-i18n="([^"]+)"[^>]*>)([^<]*)(<\/)/g,
(_match, opening, key, _text, closing) => opening + escapeText(i18n.t(key)) + closing);
result = result.replace(/<[^>]+>/g, opening => {
let translated = opening;
for (const attribute of ['title', 'aria-label', 'placeholder', 'alt']) {
const key = new RegExp(`data-i18n-${attribute}="([^"]+)"`).exec(opening)?.[1];
if (key) translated = translated.replace(new RegExp(`\\b${attribute}="[^"]*"`), `${attribute}="${escapeText(i18n.t(key)).replaceAll('"', '&quot;')}"`);
}
return translated;
});
const result = localizeHtml(source, key => i18n.t(key));
return result.replace(/\sdata-i18n(?:-[\w-]+)?="[^"]*"/g, '');
}