Hello community!
I use a great theme from Minimal Mistakes.
I supplemented it with multilingualism and made a language switcher. It works, but the problem is that it ALWAYS outputs links to alternative translations, even if there is no translation.
Here is the code:
<nav id="lang" >
{% for lang in site.languages %}
{% if forloop.index0 == 0 %}
<a href="{{ page.url | replace: "index.html"}}" class="footer__link">{{ lang }}</a>
{% else %}
<a href="/{{ lang }}{{ page.url | replace: "index.html" }}" class="footer__link">{{ lang }}</a>
{% endif %}
{% endfor %}
</nav>
This code generates the following radio button in html:
<nav id="lang-sel">
<a href="/about/" class="footer__link">en</a>
<a href="/ru/about/" class="footer__link">ru</a>
<a href="/es/about/" class="footer__link">es</a>
<a href="/zh/about/" class="footer__link">zh</a>
</nav>
The problem is that if the page with the translation does not exist, the switch still creates a link.
Question: is it possible to make it so that if the translation does not exist, the link leads to the main page or is not displayed at all?
I have tried css and display none. It works, but search engines still see those 404 links. And that’s bad.
I also display rel alternate in the code:
{% for lang in site.languages %}
{% if forloop.index0 == 0 %}
<link rel="alternate" hreflang="{{ lang }}" href="{{ site.url }}{{ page.url }}" />
{% else %}
<link rel="alternate" hreflang="{{ lang }}" href="{{ site.url }}/{{ lang }}{{ page.url }}" />
{% endif %}
{% endfor %}