|
| 1 | +/* One language in the sidebar; language switch link in the menu bar. */ |
| 2 | +(function () { |
| 3 | + var path = window.location.pathname || ''; |
| 4 | + var isCn = path.indexOf('/cn/') !== -1; |
| 5 | + |
| 6 | + var sidebar = document.querySelector('#sidebar .sidebar-scrollbox ol.chapter'); |
| 7 | + if (sidebar) { |
| 8 | + var titles = sidebar.querySelectorAll(':scope > li.part-title'); |
| 9 | + if (titles.length >= 2) { |
| 10 | + var cnPartTitle = titles[1]; |
| 11 | + var el = sidebar.firstElementChild; |
| 12 | + var next; |
| 13 | + if (isCn) { |
| 14 | + while (el && el !== cnPartTitle) { |
| 15 | + next = el.nextElementSibling; |
| 16 | + el.style.display = 'none'; |
| 17 | + el = next; |
| 18 | + } |
| 19 | + } else { |
| 20 | + el = cnPartTitle; |
| 21 | + while (el) { |
| 22 | + next = el.nextElementSibling; |
| 23 | + el.style.display = 'none'; |
| 24 | + el = next; |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + function otherLangPath(p) { |
| 31 | + if (p.indexOf('/cn/') !== -1) return p.replace('/cn/', '/en/'); |
| 32 | + if (p.indexOf('/en/') !== -1) return p.replace('/en/', '/cn/'); |
| 33 | + return new URL('cn/2026/index.html', window.location.href).pathname; |
| 34 | + } |
| 35 | + |
| 36 | + var target = otherLangPath(path); |
| 37 | + if (target === path) return; |
| 38 | + if (path.indexOf('print.html') !== -1) return; |
| 39 | + |
| 40 | + var right = document.querySelector('#menu-bar .right-buttons'); |
| 41 | + if (!right) return; |
| 42 | + |
| 43 | + var a = document.createElement('a'); |
| 44 | + a.className = 'pgw-menu-lang'; |
| 45 | + a.href = target; |
| 46 | + var icon = document.createElement('i'); |
| 47 | + icon.className = 'fa fa-language'; |
| 48 | + icon.setAttribute('aria-hidden', 'true'); |
| 49 | + var label = document.createElement('span'); |
| 50 | + label.className = 'pgw-menu-lang-label'; |
| 51 | + if (target.indexOf('/cn/') !== -1) { |
| 52 | + label.textContent = '中文'; |
| 53 | + a.title = '切换到中文版'; |
| 54 | + a.setAttribute('aria-label', '切换到中文版'); |
| 55 | + } else { |
| 56 | + label.textContent = 'English'; |
| 57 | + a.title = 'English version'; |
| 58 | + a.setAttribute('aria-label', 'English version'); |
| 59 | + } |
| 60 | + a.appendChild(icon); |
| 61 | + a.appendChild(label); |
| 62 | + right.insertBefore(a, right.firstChild); |
| 63 | +})(); |
0 commit comments