first commit
This commit is contained in:
commit
c30064b531
93 files changed
+26367
No files matched your search
@@ -0,0 +1,193 @@
|
||||
/* simply-dvb – UI translation engine.
|
||||
* The UI source text is English and doubles as the translation key (gettext style).
|
||||
* Language packs live in langs/<locale>.json ({meta, translations, patterns}); build.rs
|
||||
* validates them and embeds them in the bundle as window.SDVB_LANGS. When a language other
|
||||
* than English is active, every text node inserted into the DOM (and placeholder/title/
|
||||
* aria-label) is translated; missing entries fall back to the English source text.
|
||||
* Pattern replacements use $1..$9 for raw groups and $t1..$t9 for groups translated again. */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var LANG_KEY = 'sdvb-lang';
|
||||
var DEFAULT = 'en_US';
|
||||
var LANGS = {};
|
||||
(window.SDVB_LANGS || []).forEach(function (p) {
|
||||
var code = p.meta.locale;
|
||||
LANGS[code] = {
|
||||
name: p.meta.native_name || p.meta.name || code,
|
||||
english: p.meta.name || code,
|
||||
dir: p.meta.direction || 'ltr',
|
||||
dict: p.translations || {},
|
||||
rx: (p.patterns || []).map(function (x) { return [new RegExp(x.match), x.replace]; })
|
||||
};
|
||||
});
|
||||
if (!LANGS[DEFAULT]) LANGS[DEFAULT] = { name: 'English', english: 'English', dir: 'ltr', dict: {}, rx: [] };
|
||||
|
||||
/** Resolves a stored or requested code ("pl", "pl-PL", "pl_PL") to an available locale. */
|
||||
function resolve(code) {
|
||||
if (!code) return null;
|
||||
var c = String(code).replace('-', '_');
|
||||
if (LANGS[c]) return c;
|
||||
var base = c.split('_')[0].toLowerCase();
|
||||
var keys = Object.keys(LANGS);
|
||||
for (var i = 0; i < keys.length; i++) if (keys[i].split('_')[0].toLowerCase() === base) return keys[i];
|
||||
return null;
|
||||
}
|
||||
|
||||
var saved = null;
|
||||
try { saved = localStorage.getItem(LANG_KEY); } catch (e) {}
|
||||
var lang = resolve(saved) || DEFAULT;
|
||||
var active = lang !== DEFAULT;
|
||||
|
||||
function expand(tpl, m) {
|
||||
return tpl.replace(/\$(t?)(\d)/g, function (_, t, n) {
|
||||
var g = m[+n] == null ? '' : m[+n];
|
||||
return t ? tr(g) : g;
|
||||
});
|
||||
}
|
||||
|
||||
function lookup(k) {
|
||||
var L = LANGS[lang];
|
||||
if (Object.prototype.hasOwnProperty.call(L.dict, k)) return L.dict[k];
|
||||
for (var i = 0; i < L.rx.length; i++) {
|
||||
var m = L.rx[i][0].exec(k);
|
||||
if (m) return expand(L.rx[i][1], m);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function tr(s) {
|
||||
if (!active || s == null) return s;
|
||||
var str = String(s);
|
||||
var k = str.trim();
|
||||
if (!k) return str;
|
||||
var v = lookup(k);
|
||||
if (v === undefined || v === '') return str;
|
||||
return str.replace(k, function () { return v; });
|
||||
}
|
||||
|
||||
var SKIP = { SCRIPT: 1, STYLE: 1, CODE: 1, TEXTAREA: 1 };
|
||||
var ATTRS = ['placeholder', 'title', 'aria-label'];
|
||||
|
||||
function translateTree(root) {
|
||||
if (!active || !root) return;
|
||||
if (root.nodeType === 3) {
|
||||
var p = root.parentNode;
|
||||
if (p && !SKIP[p.nodeName]) { var n = tr(root.nodeValue); if (n !== root.nodeValue) root.nodeValue = n; }
|
||||
return;
|
||||
}
|
||||
if (root.nodeType !== 1 || SKIP[root.nodeName] || (root.hasAttribute && root.hasAttribute('data-no-i18n'))) return;
|
||||
var els = [root].concat(Array.prototype.slice.call(root.querySelectorAll('[placeholder],[title],[aria-label]')));
|
||||
els.forEach(function (el) {
|
||||
ATTRS.forEach(function (a) {
|
||||
if (el.hasAttribute(a)) { var v = el.getAttribute(a), n = tr(v); if (n !== v) el.setAttribute(a, n); }
|
||||
});
|
||||
});
|
||||
var w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
|
||||
acceptNode: function (node) {
|
||||
for (var e = node.parentNode; e && e !== root.parentNode; e = e.parentNode) {
|
||||
if (SKIP[e.nodeName] || (e.hasAttribute && e.hasAttribute('data-no-i18n'))) return NodeFilter.FILTER_REJECT;
|
||||
}
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
});
|
||||
var node;
|
||||
while ((node = w.nextNode())) {
|
||||
var nv = tr(node.nodeValue);
|
||||
if (nv !== node.nodeValue) node.nodeValue = nv;
|
||||
}
|
||||
}
|
||||
|
||||
function boot() {
|
||||
document.documentElement.lang = lang.replace('_', '-');
|
||||
document.documentElement.dir = LANGS[lang].dir;
|
||||
if (!active) return;
|
||||
translateTree(document.body);
|
||||
new MutationObserver(function (list) {
|
||||
list.forEach(function (m) {
|
||||
if (m.type === 'attributes') { translateTree(m.target); return; }
|
||||
m.addedNodes.forEach(translateTree);
|
||||
});
|
||||
}).observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ATTRS });
|
||||
}
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot);
|
||||
else Promise.resolve().then(boot);
|
||||
|
||||
window.I18N = {
|
||||
get lang() { return lang; },
|
||||
/** Short UI code of the active language, e.g. "EN". */
|
||||
get short() { return lang.split('_')[0].toUpperCase(); },
|
||||
tr: tr,
|
||||
/** Available languages: [{code, short, name, english}], English first, then by name. */
|
||||
languages: function () {
|
||||
return Object.keys(LANGS).sort(function (a, b) {
|
||||
return (a === DEFAULT ? -1 : b === DEFAULT ? 1 : 0) || LANGS[a].english.localeCompare(LANGS[b].english);
|
||||
}).map(function (c) {
|
||||
return { code: c, short: c.split('_')[0].toUpperCase(), name: LANGS[c].name, english: LANGS[c].english };
|
||||
});
|
||||
},
|
||||
setLang: function (l) {
|
||||
try { localStorage.setItem(LANG_KEY, resolve(l) || DEFAULT); } catch (e) {}
|
||||
location.reload();
|
||||
},
|
||||
/** For tooling: English keys with no translation in the given language. */
|
||||
missing: function (code) {
|
||||
var c = resolve(code) || code, d = (LANGS[c] || {}).dict || {}, en = LANGS[DEFAULT].dict;
|
||||
return Object.keys(en).filter(function (k) { return !Object.prototype.hasOwnProperty.call(d, k); });
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
/* UI personalization (stored in localStorage, applied before the first render). */
|
||||
(function () {
|
||||
'use strict';
|
||||
var KEY = 'sdvb-prefs';
|
||||
var DEFAULTS = { chartWindow: 10, theme: 'dark', accent: 'orange', density: 'comfortable', scale: 100, sidebar: 'hover', start: 'overview',
|
||||
logos: true, progress: true, autoplay: false, epgZoom: 4, clock: false, motion: true };
|
||||
var ACCENTS = {
|
||||
orange: ['#f69b74', '#ffb090', '#221810', '#b85b38'],
|
||||
green: ['#bdd293', '#d3e6ad', '#1b2410', '#5f783f'],
|
||||
blue: ['#8ebcff', '#b0d0ff', '#0c1a2e', '#2d6bc4'],
|
||||
violet: ['#c5a3ff', '#d8c0ff', '#1d1230', '#7a4fc4'],
|
||||
red: ['#ff8a80', '#ffaaa2', '#2b0f0c', '#b64a43'],
|
||||
teal: ['#6fd6c6', '#94e6d9', '#0c2420', '#1f8a7a'],
|
||||
amber: ['#f2c14e', '#f7d47e', '#2a2008', '#9a6a00']
|
||||
};
|
||||
var prefs = {};
|
||||
function load() {
|
||||
var p = {};
|
||||
try { p = JSON.parse(localStorage.getItem(KEY) || '{}') || {}; } catch (e) {}
|
||||
prefs = {};
|
||||
Object.keys(DEFAULTS).forEach(function (k) { prefs[k] = p[k] === undefined ? DEFAULTS[k] : p[k]; });
|
||||
}
|
||||
var mq = window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null;
|
||||
function apply() {
|
||||
var html = document.documentElement;
|
||||
var theme = prefs.theme === 'system' ? (mq && mq.matches ? 'light' : 'dark') : prefs.theme;
|
||||
html.dataset.theme = theme;
|
||||
html.dataset.density = prefs.density;
|
||||
html.dataset.sidebar = prefs.sidebar;
|
||||
html.dataset.motion = prefs.motion ? 'on' : 'off';
|
||||
var a = ACCENTS[prefs.accent] || ACCENTS.orange;
|
||||
var light = theme === 'light';
|
||||
html.style.setProperty('--accent', light ? a[3] : a[0]);
|
||||
html.style.setProperty('--accent-hover', light ? a[3] : a[1]);
|
||||
html.style.setProperty('--accent-ink', light ? '#fff' : a[2]);
|
||||
html.style.zoom = prefs.scale && prefs.scale !== 100 ? (prefs.scale / 100) : '';
|
||||
}
|
||||
load();
|
||||
apply();
|
||||
if (mq && mq.addEventListener) mq.addEventListener('change', function () { if (prefs.theme === 'system') apply(); });
|
||||
window.SDVB_PREFS = {
|
||||
get: function (k) { return prefs[k]; },
|
||||
all: function () { return JSON.parse(JSON.stringify(prefs)); },
|
||||
set: function (k, v) {
|
||||
prefs[k] = v;
|
||||
try { localStorage.setItem(KEY, JSON.stringify(prefs)); } catch (e) {}
|
||||
apply();
|
||||
},
|
||||
reset: function () { try { localStorage.removeItem(KEY); } catch (e) {} load(); apply(); },
|
||||
accents: Object.keys(ACCENTS),
|
||||
accentColor: function (k) { return (ACCENTS[k] || ACCENTS.orange)[0]; }
|
||||
};
|
||||
})();
|
||||
Reference in new issue
Block a user