<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
{# Language switcher: links to the same page in the other language #}
<span class="theme-switch-button btn btn-sm btn-outline-primary navbar-btn rounded-circle" data-toggle="tooltip"
  {% if language == 'zh' %}title="Switch to English"{% else %}title="切换到中文"{% endif %}>
  <a id="lang-switch" href="#" style="display:flex">
    <i class="fa-solid fa-language"></i>
  </a>
</span>
<script>
(function() {
  var link = document.getElementById('lang-switch');
  var path = window.location.pathname;
  {% if language == 'zh' %}
  // Chinese -> English: remove the /zh/ path segment
  link.href = path.replace(/\/zh\//, '/');
  {% else %}
  // English -> Chinese: insert /zh/ before the page-relative path
  var pagePath = '{{ pagename ~ file_suffix }}';
  var idx = path.lastIndexOf(pagePath);
  if (idx > 0) {
    link.href = path.substring(0, idx) + 'zh/' + path.substring(idx);
  } else {
    // Fallback for root with trailing slash
    link.href = path.replace(/\/?$/, '/') + 'zh/';
  }
  {% endif %}
})();
</script>
