Hi,
Is it possible to check whether or not a certain path or URL will be generated by jekyll?
The reason I need this is for multilingual sites: here I have a language changer in the navigation area of the site. Some pages are not available in all languages. So if a certain page does NOT exist in the alternative language, I do not want to show the language buttons.
The default language pages are at, say, /foo/ or /bar/, while the alternative language pages are at /da/foo/ or /da/bar/.
So on /foo/, I do not want to show the language buttons if /da/foo/ is not to be generated.
Is something like this possible?
Here is the relevant code:
Language buttons:
<button id="language-btn" class="btn btn-light text-muted" type="button" >
{% if page.lang == "en" %}
<svg width="22" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 37 28"><path fill="#c60c30" d="M0,0H37V28H0Z"/><path fill="#fff" d="M0,12H12V0H16V12H37V16H16V28H12V16H0Z"/></svg>
<!--
Dansk
-->
{% else %}
<svg width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 30" >
<clipPath id="s">
<path d="M0,0 v30 h60 v-30 z"/>
</clipPath>
<clipPath id="t">
<path d="M30,15 h30 v15 z v15 h-30 z h-30 v-15 z v-15 h30 z"/>
</clipPath>
<g clip-path="url(#s)">
<path d="M0,0 v30 h60 v-30 z" fill="#012169"/>
<path d="M0,0 L60,30 M60,0 L0,30" stroke="#fff" stroke-width="6"/>
<path d="M0,0 L60,30 M60,0 L0,30" clip-path="url(#t)" stroke="#C8102E" stroke-width="4"/>
<path d="M30,0 v30 M0,15 h60" stroke="#fff" stroke-width="10"/>
<path d="M30,0 v30 M0,15 h60" stroke="#C8102E" stroke-width="6"/>
</g>
</svg>
<!--
English
-->
{% endif %}
</button>
And the javascript to make the buttons work:
// Locales
const path = window.location.pathname,
langUrl = path.indexOf('/da/') !== -1
// Language switch
document.getElementById('language-btn').onclick = () => {
if ( path == "/da/") {
// Redirect to /index.html instead of '/' in order not to trigger language detection on the server
window.location = "/index.html"
} else {
window.location = langUrl ? path.replace('/da', '') : '/da'.concat(path);
}
document.getElementById('language-img').src = '';
}