Switching languages in the frontend: Can’t get example Switch C to work

Hi,

I’m having troubles to get Language Switch C from the guides to work with Kirby 3.3.4 nor the latest 3.3.5-rc.1.

In a two language setup, no matter whether a translation file exists or not, it always links to the error page of the given language.

As I can’t find any references to either $page->content($language->code())->language() or $site->language() in the Kirby 3 docs I am wondering if the example is still up to date?

Thanks for your help!
Malte

Sorry for the confusion, I think we overlooked to update this properly for Kirby 3.
$site->language() is indeed outdated code, should be $kirby->language()

$page->content($language->code())->language() should be replaced with $page->translation(string $languageCode), I think.

This is what the code should look like:

<nav class="languages">
  <ul>
  <?php foreach($kirby->languages() as $language): ?>
    <li<?php e($kirby->language() == $language, ' class="active"') ?>>
      <a href="<?php e($page->translation($language->code())->exists() === false, page('error')->url($language->code()), $page->url($language->code()))?>"><?php echo $language->code(); ?></a>
    </li>
  <?php endforeach ?>
  </ul>
</nav>

But I wonder if this really makes sense. I’d probably only show the link to the other translation for existing translations, or “disable” the link (i.e. only show text, not an anchor tag) for a non-existing translation. Or maybe send the user to the home page.

Related: https://getkirby.com/docs/cookbook/i18n/filter-by-language
(for example if you want to filter your blog post list by available post in a given language)

Thanks for your help! Now it works. :slightly_smiling_face:

hi, I tried to have my language switch in
select option tags instead of
ul li tags
but i can’t figure it out.
here is my code:


            <select name="language-picker-select" id="language-picker-select"  
   onchange="this.form.submit()">
            <?php foreach($kirby->languages() as $language): ?>
              <?php e($kirby->language() == $language, ' class="active"') ?>
              <option value="<?php echo $language->url() ?>" hreflang="<?php echo 
  $language->code() ?>"  selected>
  <?php echo html($language->name()) ?>
  </option>
  <?php endforeach ?>
            </select>

  </form>