docs(nav): fix sidebar nesting, dynamic language switcher, and delete translations page (#5535)

This commit is contained in:
James Rich
2026-05-20 08:42:34 -07:00
committed by GitHub
parent 19d10a18cd
commit 89fe2b58d7
31 changed files with 223 additions and 204 deletions
+2 -1
View File
@@ -125,7 +125,8 @@
.language-switcher-list {
position: absolute;
top: 100%;
left: 0;
right: 0;
left: auto;
z-index: 100;
list-style: none;
padding: 8px 0;
+59 -44
View File
@@ -6,49 +6,49 @@
Usage: {% include language_switcher.html %}
Logic:
- Derives the current page's relative path within its section
- Checks if translated versions exist in locale subdirectories
- Shows a globe icon with available locale links
- Dynamically detects the current language and page name.
- Dynamically updates the switcher button's native language name.
- Correctly matches English pages to translated equivalents and vice versa.
- Pre-verifies existing files to only render valid, non-404 options.
{% endcomment %}
{% assign current_path = page.path %}
{% assign locales = site.data.locales %}
{% if locales and current_path %}
{% assign path_parts = current_path | split: "/" %}
{% assign first_segment = path_parts[0] %}
{% assign path_parts = current_path | split: "/" %}
{% assign first_segment = path_parts[0] %}
{% comment %} Build the list of available translations first {% endcomment %}
{% assign has_translations = false %}
{% comment %}
Identify the current page's language code and native language name.
If the first segment is defined in locales.yml, we are on a translation.
Otherwise, we are in default English.
{% endcomment %}
{% if locales[first_segment] %}
{% assign current_lang_code = first_segment %}
{% assign current_lang_name = locales[first_segment].name %}
{% else %}
{% assign current_lang_code = "en" %}
{% assign current_lang_name = "English" %}
{% endif %}
{% if locales[first_segment] %}
{% comment %} We're on a translated page — English link is always available {% endcomment %}
{% assign has_translations = true %}
{% comment %}
Extract the relative base path (independent of locale prefix and file extension).
E.g., "en/user/connections.md" and "be-rBY/user/connections.md" both resolve to "user/connections".
{% endcomment %}
{% assign remaining_parts = path_parts | slice: 1, path_parts.size %}
{% assign en_path = remaining_parts | join: "/" | replace: ".md", "" %}
{% else %}
{% comment %} Check if any translated version exists {% endcomment %}
{% assign en_relative = current_path | replace: ".md", "" %}
{% for locale in locales %}
{% assign locale_code = locale[0] %}
{% assign locale_file = locale_code | append: "/" | append: en_relative | append: ".md" %}
{% for p in site.pages %}
{% if p.path == locale_file %}
{% assign has_translations = true %}
{% break %}
{% endif %}
{% endfor %}
{% if has_translations %}{% break %}{% endif %}
{% endfor %}
{% endif %}
{% assign base_path = remaining_parts | join: "/" | replace: ".md", "" %}
{% if has_translations %}
<details class="language-switcher" aria-label="Language options">
<summary class="language-switcher-btn" title="View in another language">
🌐 <span class="lang-current">English</span>
</summary>
<ul class="language-switcher-list">
{% if locales[first_segment] %}
{% comment %}
Pre-render the dropdown list items.
{% endcomment %}
{% capture dropdown_items %}
{% if current_lang_code != "en" %}
{% if base_path == "index" %}
{% assign en_path = "en/" %}
{% else %}
{% assign en_path = "en/" | append: base_path %}
{% endif %}
<li><a href="{{ en_path | relative_url }}" lang="en">English</a></li>
{% endif %}
@@ -56,16 +56,12 @@
{% assign locale_code = locale[0] %}
{% assign locale_info = locale[1] %}
{% if locales[first_segment] %}
{% if locale_code == first_segment %}
{% continue %}
{% endif %}
{% assign locale_path = locale_code | append: "/" | append: en_path %}
{% else %}
{% assign locale_path = locale_code | append: "/" | append: en_relative %}
{% if locale_code == current_lang_code %}
{% continue %}
{% endif %}
{% assign locale_file = locale_path | append: ".md" %}
{% assign locale_file = locale_code | append: "/" | append: base_path | append: ".md" %}
{% assign page_exists = false %}
{% for p in site.pages %}
{% if p.path == locale_file %}
@@ -75,10 +71,29 @@
{% endfor %}
{% if page_exists %}
{% if base_path == "index" %}
{% assign locale_path = locale_code | append: "/" %}
{% else %}
{% assign locale_path = locale_code | append: "/" | append: base_path %}
{% endif %}
<li><a href="{{ locale_path | relative_url }}" lang="{{ locale_code }}" {% if locale_info.dir == "rtl" %}dir="rtl"{% endif %}>{{ locale_info.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
</details>
{% endif %}
{% endcapture %}
{% assign dropdown_items_stripped = dropdown_items | strip %}
{% comment %}
Only render the switcher if there is at least one other language option available.
{% endcomment %}
{% if dropdown_items_stripped != "" %}
<details class="language-switcher" aria-label="Language options">
<summary class="language-switcher-btn" title="View in another language">
🌐 <span class="lang-current">{{ current_lang_name }}</span>
</summary>
<ul class="language-switcher-list">
{{ dropdown_items }}
</ul>
</details>
{% endif %}
{% endif %}