@michael, @reboom If my issues were the same as yours, I might have a solution to point you in the right direction…
Grav has a function Page::translatedLanguages() (see docs on Page) which is said to do the following:
public translatedLanguages(bool $onlyPublished=false ): array the page translated languages
Return an array with the routes of other translated languages
Unfortunately this doesn’t work as I expected (issue #2163). After a “fix” it only returns nulls for each language…
I have therefor created my own LanguageSwitcher plugin with a rewritten translatedLanguages() method. The method returns an array with the proper slug for each supported (and translated) language.
Based on this array, the plugin can then:
- Create proper urls to each alternate language of the current page.
- Create correct absolute alternate links <link rel=“alternate”> for current language and alternate languages.
To test if this solves your issue you could try the following:
- Create fresh Grav installation
- Add the following to user/config/system.yaml:
languages:
supported: [en, de, fr]
include_default_lang: false # Don't show language in url for default language
- Create translated pages for user/pages/01.home and user/pages/02.typography with correct titles and slugs.
- To create an unstyled “language switcher” using a <ul> containing urls for each translation, add the following somewhere in the <body> of the template user/themes/quark/templates/partials/base.html.twig:
{% include 'partials/language-switcher-list.html.twig' %}
- To create <link rel=“alternate”> for each language, add the following to the <head> of user/themes/quark/templates/partials/base.html.twig:
{% include 'partials/language-switcher-hreflang.html.twig' %}
- Lastly, download a ZIP of the repo grav-plugin-language-switcher and drop folder “grav-plugin-language-switcher-master” from inside the zip into user/plugins/ and rename the folder to “language-switcher”.
The <link rel=“alternate”> in the <head> of the page “Typography” should look like:
<link rel="alternate" hreflang="en" href="http://localhost/grav/site-test/typography" />
<link rel="alternate" hreflang="de" href="http://localhost/grav/site-test/de/typografie" />
<link rel="alternate" hreflang="fr" href="http://localhost/grav/site-test/fr/typographie" />
The <ul> HTML code containing urls to alternate langauges for the english (default) page “Typography” should look like":
<ul>
<li>
<a href="/grav/site-test/de/typografie">
<img class="flag" src="/grav/site-test/user/plugins/language-switcher/images/de.png" alt="de">
<span class="language">Deutsch</span>
</a>
</li>
<li>
<a href="/grav/site-test/fr/typographie">
<img class="flag" src="/grav/site-test/user/plugins/language-switcher/images/fr.png" alt="fr">
<span class="language">Français</span>
</a>
</li>
</ul>
If you don’t want to show the flags you can alter a copy of /user/plugins/language-switcher/language-switcher.yaml in /user/config.
As Andy said in issue #2026, this method will pull all translated *.md files from disk during processing of the current page. Since I’m only using the plugin for a site with 2 languages, I noticed no performance degradation. If many languages are involved and performance might become an issue, the results for each page could be cached on first request and retrieved from cache in subsequent requests.
The plugin is not very well documented and might be hard to grasp… Sorry for that, it was only intended for personal use.
Nonetheless, I hope this helps a bit.