{% comment %} Language switcher for translated docs pages. Renders a dropdown-style link list showing available translations of the current page. Include this in any page or layout that should offer locale switching. Usage: {% include language_switcher.html %} Logic: - 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] %} {% 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 %} {% 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 base_path = remaining_parts | join: "/" | replace: ".md", "" %} {% 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 %}