Repository navigation
Expand file tree
/
Copy pathi18n.js
More file actions
67 lines (57 loc) · 1.87 KB
/
Copy pathi18n.js
File metadata and controls
67 lines (57 loc) · 1.87 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ====== i18n Toggle System ======
// Shared across intro & expert guides.
// Usage: add data-i18n="key" to any element.
// Text nodes get replaced; attributes can be targeted with data-i18n-attr="placeholder".
(function () {
const STORAGE_KEY = 'rag-guide-lang';
function getLang() {
return localStorage.getItem(STORAGE_KEY) || 'ko';
}
function setLang(lang) {
localStorage.setItem(STORAGE_KEY, lang);
applyLang(lang);
updateToggleUI(lang);
}
function applyLang(lang) {
document.documentElement.lang = lang === 'en' ? 'en' : 'ko';
document.querySelectorAll('[data-ko]').forEach(el => {
const text = el.getAttribute(lang === 'en' ? 'data-en' : 'data-ko');
if (text !== null) {
// If the element has child elements we only want to replace, use innerHTML carefully
if (el.children.length === 0) {
el.textContent = text;
} else {
el.innerHTML = text;
}
}
});
}
function updateToggleUI(lang) {
document.querySelectorAll('.lang-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.lang === lang);
});
}
// Create toggle button & inject into nav
function createToggle() {
const nav = document.querySelector('.nav-inner');
if (!nav) return;
const wrap = document.createElement('div');
wrap.className = 'lang-toggle';
wrap.innerHTML = `
<button class="lang-btn" data-lang="ko">KO</button>
<button class="lang-btn" data-lang="en">EN</button>
`;
nav.appendChild(wrap);
wrap.querySelectorAll('.lang-btn').forEach(btn => {
btn.addEventListener('click', () => setLang(btn.dataset.lang));
});
updateToggleUI(getLang());
}
// Init
document.addEventListener('DOMContentLoaded', () => {
createToggle();
const lang = getLang();
if (lang !== 'ko') applyLang(lang);
updateToggleUI(lang);
});
})();