File size: 1,698 Bytes
4a9bab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// RetinaPlus — theme + language (i18n) engine, loaded on every page
(function () {
  const root = document.documentElement;

  // ---- theme ----
  const theme = localStorage.getItem('rp-theme') || 'light';
  root.setAttribute('data-theme', theme);
  window.toggleTheme = function () {
    const t = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    root.setAttribute('data-theme', t);
    localStorage.setItem('rp-theme', t);
    paintTools();
  };

  // ---- language ----
  let lang = localStorage.getItem('rp-lang') || 'th';   // default Thai
  function applyLang() {
    document.documentElement.lang = lang;
    document.querySelectorAll('[data-th]').forEach(el => {
      const v = el.getAttribute('data-' + lang);
      if (v != null) {
        if (el.hasAttribute('data-attr')) el.setAttribute(el.getAttribute('data-attr'), v);
        else el.textContent = v;
      }
    });
    document.title = (document.querySelector('[data-title-' + lang + ']')?.getAttribute('data-title-' + lang)) || document.title;
  }
  window.toggleLang = function () {
    lang = lang === 'th' ? 'en' : 'th';
    localStorage.setItem('rp-lang', lang);
    applyLang(); paintTools();
  };
  window.getLang = () => lang;

  // ---- tool buttons label ----
  function paintTools() {
    const t = root.getAttribute('data-theme');
    document.querySelectorAll('[data-tool="theme"] i').forEach(i =>
      i.className = 'ti ' + (t === 'dark' ? 'ti-sun' : 'ti-moon'));
    document.querySelectorAll('[data-tool="lang"]').forEach(b =>
      b.textContent = lang === 'th' ? 'EN' : 'ไทย');
  }

  document.addEventListener('DOMContentLoaded', function () { applyLang(); paintTools(); });
})();