Simple language switcher v3

Hi! I was using a snippet in v2 with this code, on various pages, a simple language switcher:

<?php if($site->language()->code() == 'en'): ?>
    <a href="<?= $page->url('pt') ?>" title="<?= $page->lang() ?>">pt</a>
<?php else: ?>
    <a href="<?= $page->url('en') ?>" title="<?= $page->lang() ?>">en</a>
<?php endif ?>

But now in v3 it doesn’t work… but i can’t understand why. Can anybody help? Thanks!

Edit: I must add that the default language URL is “/” but even if i remove this from config, it doesn’t work.

Try: https://getkirby.com/docs/reference/objects/page/url-for-language

But what exactly doesn’t work? As far as I can see from the source code, using the url() method with the language parameter should still work, as it call the urlForLanguage() method internally.

The link was always stuck to EN, using the code snippet above. It wasn’t detecting the current page language.

I managed to make it work, but i’m not sure if it’s correct… I mean, it is working! I went to the references to look for $site, and when i looked for $kirby i found language() there, so i used it. See:

<?php if($kirby->language()->code() == 'en'): ?>
    <a href="<?= $page->url('pt') ?>" title="<?= $page->lang() ?>">pt</a>
<?php else: ?>
    <a href="<?= $page->url('en') ?>" title="<?= $page->lang() ?>">en</a>
<?php endif ?>
1 Like

Ah, I focused on the URL part, totally missing you had use $site, sorry…:blush:

You’ve been a huge help!

Well, now we have a new ref for Kirby 3’s language switcher! :stuck_out_tongue: