Language switcher template code

Hello - I’ve implemented a language switch dropdown menu, with this code in my sidebar partial:

<ul id="dropdown1" class="dropdown-content">
 {{ $perma := .Permalink }}
 {{ with .Params.alternatelocales }}
 {{ range . }}
 {{ $thelocale := . }}
 {{ $currenturl := ( index $.Site.Data.translations $.Site.Params.locale ).languageurl }}
 {{ $targeturl := ( index $.Site.Data.translations $thelocale ).languageurl }}
 {{ $langurl := replace $perma $currenturl $targeturl }}
  <li><a href="{{ $langurl }}">{{ ( index $.Site.Data.translations $thelocale ).languagename }}</a></li>
 {{ end }}
 {{ end }}

This works, to find in the .Permalink, the current url and replace it with for example. It assumes the url path is the same, but in my case, that’s true.

With this, I just put the array of alternative languages available in my file.

Later I’ll add a check whether the alternatelocale is in the frontmatter, and if not, don’t show the switcher.

I’m just wondering if there’s a more efficient or “better” way of doing this, in general.

Any comments appreciated.