Get page translation

Say my site has only two languages, German and English. Is there a simple way for me to get the page URL for “the other language”.

Currently, this seems to be quite complicated to me:

  • get all languages
  • find the language that is not the current language in a loop
  • get the other language’s code and pass it to $page->url()

Additionally, if I’d like to have this url available across templates, I’d have to create a plugin with a general page method, too?

Am I missing the obvious?

Have you read this page? Switch B sounds like what you are looking for.

Yes, that example is exactly doing what I describe above. Only that it generates markup whereas I want to store the url in a variable.

I think what I’m after is something like this:


$page->translations(false) would exclude the current language form the translation collection. And this is the missing piece, $page->translations() always returns all languages.

I updated my comment:

Still, there is no easy way to get the translated URL as far as I can see.

This should be what you want. It gets the current language, then iterates through all languages and excludes the current language from the output.

<?php foreach($kirby->languages()->not($kirby->language()) as $language): ?>
  <?= $page->url($language->code()) ?>
<?php endforeach ?>
$languages = $kirby->languages();
echo $page->url($languages->not($kirby->language())->first()->code()) ?>

Maybe wrap it in a custom page method?