File size: 3,131 Bytes
83ddd7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
document.addEventListener('DOMContentLoaded', function () {
  var path = window.location.pathname || '';
  var isZh = path.indexOf('/zh/') !== -1;
  var isEn = path.indexOf('/en/') !== -1;
  if (!isZh && !isEn) return;

  var currentLang = isZh ? 'zh' : 'en';

  function buildSwitcher() {
    var container = document.createElement('div');
    container.className = 'lang-switcher';

    var select = document.createElement('select');
    select.setAttribute('aria-label', 'Language');
    select.className = 'lang-switcher__select';

    var optionZh = document.createElement('option');
    optionZh.value = 'zh';
    optionZh.textContent = 'Simplified Chinese';
    optionZh.selected = isZh;

    var optionEn = document.createElement('option');
    optionEn.value = 'en';
    optionEn.textContent = 'English';
    optionEn.selected = isEn;

    select.appendChild(optionZh);
    select.appendChild(optionEn);

    select.addEventListener('change', function () {
      var nextLang = select.value;
      if (nextLang === currentLang) return;
      var targetUrl = path.replace('/' + currentLang + '/', '/' + nextLang + '/');
      window.location.href = targetUrl + window.location.search + window.location.hash;
    });

    container.appendChild(select);
    return container;
  }

  function hideOtherLanguageToc() {
    var captions = document.querySelectorAll('p.caption');
    for (var i = 0; i < captions.length; i++) {
      var caption = captions[i];
      var textEl = caption.querySelector('.caption-text');
      if (!textEl) continue;
      var label = (textEl.textContent || '').trim().toLowerCase();

      var isCaptionZh = label === '中文' || label === 'chinese' || label === 'zh';
      var isCaptionEn = label === 'english' || label === 'en';

      if (!isCaptionZh && !isCaptionEn) continue;

      var shouldHide = (currentLang === 'zh' && isCaptionEn) || (currentLang === 'en' && isCaptionZh);
      var shouldHideCaption = true;

      var next = caption.nextElementSibling;
      if (next && next.tagName && next.tagName.toLowerCase() === 'ul') {
        if (shouldHide) {
          caption.style.display = 'none';
          next.style.display = 'none';
        } else if (shouldHideCaption) {
          caption.style.display = 'none';
        }
      } else if (shouldHide) {
        caption.style.display = 'none';
      } else if (shouldHideCaption) {
        caption.style.display = 'none';
      }
    }
  }

  var side = document.querySelector('.wy-side-nav-search');
  if (side) {
    var sideSwitcher = buildSwitcher();
    sideSwitcher.style.marginTop = '8px';
    sideSwitcher.style.display = 'flex';
    sideSwitcher.style.justifyContent = 'center';
    side.appendChild(sideSwitcher);
  } else {
    var topRight = buildSwitcher();
    topRight.style.position = 'fixed';
    topRight.style.top = '12px';
    topRight.style.right = '12px';
    topRight.style.zIndex = '9999';
    document.body.appendChild(topRight);
  }

  hideOtherLanguageToc();
  window.addEventListener('load', hideOtherLanguageToc);
  setTimeout(hideOtherLanguageToc, 50);
  setTimeout(hideOtherLanguageToc, 300);
});