Linz/Urfahr
Hauptstraße 58, 4040 Linz
+43 50 353-300
Linz/Urfahr
Hauptstraße 73, 4040 Linz
+43 50 353-300
Neufelden
Marktplatz 1, 4120 Neufelden
+43 50 353-300
Gleisdorf
Franz Bloder Gasse 4, 8200 Gleisdorf
+43 50 353-300
Salzburg
Linzer Gasse 3, 5020 Salzburg
+43 50 353-300

Craft CMS — Language switcher macro

A language switcher or, as they say nowadays, a site switcher. A switcher is always needed when there are multiple versions of a website. For example a translation of a website, though other applications are also conceivable. The language switcher is the typical case however. What makes it so special?

Requirements for a flexible language switcher in Craft CMS

We have already carried out numerous customer projects using a language switcher (Talentor, Kusser). We didn’t want any ordinary language switcher, however, where a parameter is simply “inanely” replaced in the URL. No, what we wanted was to be flexible and to dictate what the language switcher displays. What emerged is a Twig macro – yes indeed, macros are brilliant for this purpose – which fulfils most requirements. All of this then always acts as the underlying basis, which is built on and adapted in the specific project.

Code snippet

Lines 7-11 are interesting for showing how to integrate it all. If nothing is entered in the macro, “all” is set by default. All languages available on the website are then simply displayed. Though it is also possible to include an array, for example, in the macro. The site handles can be used for this purpose and thus control which languages the switcher will display with relative accuracy. Fabulously flexible.

We’d be happy to discuss it all. Simply contact us on Twitter ;)