Release / Build image (amd64) (push) Successful in 10m49s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 20m40s
CI / Format and clippy (push) Successful in 5m43s
CI / Tests (Linux) (push) Successful in 16m5s
CI / Dependency audit (push) Successful in 11s
CI / Docker image (build and smoke test) (push) Skipped
Release / Build image (arm64) (push) Successful in 7m34s
Release / Publish tags (push) Successful in 15s
Release / Gitea release (push) Skipped
Release / CLI (linux-aarch64) (push) Skipped
Release / CLI (linux-x86_64) (push) Skipped
Renames the whole project, after the harpy eagle: crates (xexeu-core/xexeu-cli -> harpia-core/harpia-cli), binary (xexeu -> harpia), admin token env var (XEXEU_ADMIN_TOKEN -> HARPIA_ADMIN_TOKEN), compose env vars (XEXEU_* -> HARPIA_*), container image (ciberfauna/xexeu -> ciberfauna/harpia) and the Gitea repository (ciberfauna/xexeu -> ciberfauna/harpia). Logo updated to the new brand artwork. All docs (EN/PT-BR), CI workflows, scripts, GUI strings and the openspec change history updated to match. BREAKING CHANGE: binary, crate, env var and image names all changed; existing .env files, scripts and deployments referencing the old xexeu/XEXEU names must be updated. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VAyr8cd6jzwpJM9cS1oGjy
135 lines
4.1 KiB
JavaScript
135 lines
4.1 KiB
JavaScript
import { EN } from './i18n-en.js';
|
|
|
|
let lang = 'pt';
|
|
export const isEn = () => lang === 'en';
|
|
export const currentLang = () => lang;
|
|
export const LOC = () => (lang === 'en' ? 'en-US' : 'pt-BR');
|
|
|
|
export function resolveLang(pref) {
|
|
if (pref === 'pt' || pref === 'en') return pref;
|
|
return (navigator.language || 'pt').toLowerCase().startsWith('pt') ? 'pt' : 'en';
|
|
}
|
|
|
|
const ATTRS = ['placeholder', 'title', 'aria-label', 'alt', 'data-empty'];
|
|
const SKIP = '.mono, pre, code, input, textarea, script, style, [data-i18n-skip]';
|
|
|
|
const exact = new Map(Object.entries(EN).filter(([key]) => !key.includes('{n}')));
|
|
const patterns = Object.entries(EN)
|
|
.filter(([key]) => key.includes('{n}'))
|
|
.map(([key, value]) => {
|
|
const parts = key.split('{n}').map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
return { re: new RegExp('^' + parts.join('(.+?)') + '$', 's'), to: value };
|
|
})
|
|
.sort((a, b) => b.re.source.length - a.re.source.length);
|
|
|
|
const cache = new Map();
|
|
export function tr(text) {
|
|
if (!isEn()) return text;
|
|
const lead = text.match(/^\s*/)[0];
|
|
const trail = text.match(/\s*$/)[0];
|
|
const core = text.trim();
|
|
if (!core) return text;
|
|
if (cache.has(core)) return lead + cache.get(core) + trail;
|
|
let out = exact.get(core);
|
|
if (out === undefined) {
|
|
for (const pattern of patterns) {
|
|
const match = core.match(pattern.re);
|
|
if (match) {
|
|
let index = 0;
|
|
out = pattern.to.replace(/\{n\}/g, () => tr(match[++index] ?? ''));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (out === undefined) out = core;
|
|
if (cache.size > 5000) cache.clear();
|
|
cache.set(core, out);
|
|
return lead + out + trail;
|
|
}
|
|
|
|
const original = new WeakMap();
|
|
let observer = null;
|
|
|
|
function translateNode(node) {
|
|
if (node.nodeType === 3) {
|
|
if (node.parentElement?.closest(SKIP)) return;
|
|
const source = original.get(node) ?? node.nodeValue;
|
|
const translated = tr(source);
|
|
if (translated !== node.nodeValue) {
|
|
if (!original.has(node)) original.set(node, source);
|
|
node.nodeValue = translated;
|
|
}
|
|
} else if (node.nodeType === 1) {
|
|
if (node.matches?.(SKIP) && node.tagName !== 'INPUT') return;
|
|
for (const attribute of ATTRS) {
|
|
if (node.hasAttribute?.(attribute)) {
|
|
const key = `i18n-${attribute}`;
|
|
const source = node[key] ?? node.getAttribute(attribute);
|
|
const translated = tr(source);
|
|
if (translated !== node.getAttribute(attribute)) {
|
|
node[key] = source;
|
|
node.setAttribute(attribute, translated);
|
|
}
|
|
}
|
|
}
|
|
for (const child of node.childNodes) translateNode(child);
|
|
}
|
|
}
|
|
|
|
function restoreAll(root) {
|
|
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
let node;
|
|
while ((node = walker.nextNode())) {
|
|
if (original.has(node)) {
|
|
node.nodeValue = original.get(node);
|
|
original.delete(node);
|
|
}
|
|
}
|
|
root.querySelectorAll('*').forEach((element) => {
|
|
for (const attribute of ATTRS) {
|
|
const key = `i18n-${attribute}`;
|
|
if (element[key] !== undefined) {
|
|
element.setAttribute(attribute, element[key]);
|
|
delete element[key];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function start() {
|
|
translateNode(document.body);
|
|
if (!observer) {
|
|
observer = new MutationObserver((mutations) => {
|
|
if (!isEn()) return;
|
|
for (const mutation of mutations) {
|
|
if (mutation.type === 'childList') {
|
|
for (const added of mutation.addedNodes) translateNode(added);
|
|
} else if (mutation.type === 'characterData') {
|
|
translateNode(mutation.target);
|
|
}
|
|
}
|
|
});
|
|
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
|
|
}
|
|
}
|
|
|
|
export function setLang(next) {
|
|
if (next !== 'pt' && next !== 'en') next = 'pt';
|
|
const changed = lang !== next;
|
|
lang = next;
|
|
cache.clear();
|
|
document.documentElement.lang = next === 'en' ? 'en' : 'pt-BR';
|
|
if (changed) {
|
|
if (isEn()) {
|
|
start();
|
|
} else {
|
|
if (observer) {
|
|
observer.disconnect();
|
|
observer = null;
|
|
}
|
|
restoreAll(document.body);
|
|
}
|
|
document.dispatchEvent(new CustomEvent('harpia:lang', { detail: { lang } }));
|
|
}
|
|
}
|