|
15 | 15 | {% assign locales = site.data.locales %} |
16 | 16 |
|
17 | 17 | {% if locales and current_path %} |
| 18 | +{% assign path_parts = current_path | split: "/" %} |
| 19 | +{% assign first_segment = path_parts[0] %} |
| 20 | + |
| 21 | +{% comment %} Build the list of available translations first {% endcomment %} |
| 22 | +{% assign has_translations = false %} |
| 23 | + |
| 24 | +{% if locales[first_segment] %} |
| 25 | + {% comment %} We're on a translated page — English link is always available {% endcomment %} |
| 26 | + {% assign has_translations = true %} |
| 27 | + {% assign remaining_parts = path_parts | slice: 1, path_parts.size %} |
| 28 | + {% assign en_path = remaining_parts | join: "/" | replace: ".md", "" %} |
| 29 | +{% else %} |
| 30 | + {% comment %} Check if any translated version exists {% endcomment %} |
| 31 | + {% assign en_relative = current_path | replace: ".md", "" %} |
| 32 | + {% for locale in locales %} |
| 33 | + {% assign locale_code = locale[0] %} |
| 34 | + {% assign locale_file = locale_code | append: "/" | append: en_relative | append: ".md" %} |
| 35 | + {% for p in site.pages %} |
| 36 | + {% if p.path == locale_file %} |
| 37 | + {% assign has_translations = true %} |
| 38 | + {% break %} |
| 39 | + {% endif %} |
| 40 | + {% endfor %} |
| 41 | + {% if has_translations %}{% break %}{% endif %} |
| 42 | + {% endfor %} |
| 43 | +{% endif %} |
| 44 | + |
| 45 | +{% if has_translations %} |
18 | 46 | <details class="language-switcher" aria-label="Language options"> |
19 | 47 | <summary class="language-switcher-btn" title="View in another language"> |
20 | 48 | 🌐 <span class="lang-current">English</span> |
21 | 49 | </summary> |
22 | 50 | <ul class="language-switcher-list"> |
23 | | - {% comment %} Always show English link back to source {% endcomment %} |
24 | | - {% assign path_parts = current_path | split: "/" %} |
25 | | - {% assign first_segment = path_parts[0] %} |
26 | | - |
27 | | - {% comment %} Detect if we're currently IN a locale subdir {% endcomment %} |
28 | 51 | {% if locales[first_segment] %} |
29 | | - {% comment %} We're on a translated page — link back to English {% endcomment %} |
30 | | - {% assign remaining_parts = path_parts | slice: 1, path_parts.size %} |
31 | | - {% assign en_path = remaining_parts | join: "/" | replace: ".md", "" %} |
32 | 52 | <li><a href="{{ en_path | relative_url }}" lang="en">English</a></li> |
33 | 53 | {% endif %} |
34 | 54 |
|
35 | | - {% comment %} Show all available locale versions {% endcomment %} |
36 | 55 | {% for locale in locales %} |
37 | 56 | {% assign locale_code = locale[0] %} |
38 | 57 | {% assign locale_info = locale[1] %} |
39 | 58 |
|
40 | 59 | {% if locales[first_segment] %} |
41 | | - {% comment %} We're already on a translated page {% endcomment %} |
42 | 60 | {% if locale_code == first_segment %} |
43 | 61 | {% continue %} |
44 | 62 | {% endif %} |
45 | 63 | {% assign locale_path = locale_code | append: "/" | append: en_path %} |
46 | 64 | {% else %} |
47 | | - {% comment %} We're on an English page {% endcomment %} |
48 | | - {% assign en_relative = current_path | replace: ".md", "" %} |
49 | 65 | {% assign locale_path = locale_code | append: "/" | append: en_relative %} |
50 | 66 | {% endif %} |
51 | 67 |
|
52 | | - {% comment %} |
53 | | - Check if the translated file actually exists. |
54 | | - Jekyll doesn't have a file_exists filter, so we check site.pages. |
55 | | - {% endcomment %} |
56 | 68 | {% assign locale_file = locale_path | append: ".md" %} |
57 | 69 | {% assign page_exists = false %} |
58 | 70 | {% for p in site.pages %} |
|
69 | 81 | </ul> |
70 | 82 | </details> |
71 | 83 | {% endif %} |
| 84 | +{% endif %} |
0 commit comments