Multilanguage - Link to one specific page with dynamic slug?

I just started to build my first site with multilanguage. Many things that are a nightmare in other CMS:es just seems to work in Kirby. Awesome!

However I have one simple thing that I can’t figure out how to do right, link to a static page. Let’s say I need to link to a page that has these slugs:

contact
kontakt

How can I link to this page? The link is going to change dependent on what language I have.

I know I can use if statements but it feels messy. Is there a “magic” function here?

I’m not quite sure I understand the question? Do you want to link to a specific language version? And where? In a panel field or in your templates?

In the template. Or more correct in a Pattern but it’s quite the same thing.

Like this:

<a href="<?php echo magic_function('contact'); ?>"></a>

Then it should translate it to kontakt if the language changes to the correct language for that slug.

Result

<a href="contact"></a>

or

<a href="kontact"></a>

Depending on language.

If you use a link like this:

<a href="<?php echo page('contact')->url(); ?>"></a>

the correct link is generated automatically (or automagically)

1 Like

Or if you only want to get the slug:

<a href="<?php echo page('contact')->slug(); ?>"></a>

@texnixe Ahh! That was sooo simple! It worked!

For others that read this. If it does not work, make sure you add the slug of the default language. Else it does not work.

In my case echo page('kontakt')->url() worked but not echo page('contact')->url. It’s because I set the default language to Swedish.

Yes, you need to use the slug as it appears in your file system.

That’s really logical. Thanks for the help and clearing things out.