Creating a shortcode with translations

Hi everyone :wave:

Thank you for reading my post. I’m relatively new to Hugo and eager to learn more about using it.

I’m currently making a shortcode that I called “button” that you can use like this:
{{< button id="food/apple" some="properties" some2="properties2" >}}

It will create a <a href="link">button text</a> with some styling based on optional key/value pairs. So far, so good!

I’m struggling making the localized permalink in the <a href=...>. The shortcode needs to check if a localised page is available based on the ID. If there’s no localised page available, then it needs to use the English default page.

This code seems to work OK:
{{ $scratch := newScratch }}

{{ with .Site.GetPage (.Get "id") }}
{{ $.Scratch.Set "url" .Permalink }}
{{ end }}

<a href="{{ $.Scratch.Get "url" }}" target="_blank">{{ $.Scratch.Get "text" }}</a>

At this time it is only looking in the current language and not looking in the default language. How do I fallback to the default English “en” language, when there isn’t a localised page available?

Provided that “en” is the first defined language, try using .Sites.First