Skip to content

Back to Notes on Kirby CMS

Add a language switcher to the front-end

The markup below switches the current page between languages, preserving things like Kirby URL params.

<ul role=list>
<?php foreach($kirby->languages() as $language): ?>
	<li<?php e($kirby->language() == $language, ' class="active"') ?>>
		<a 
			href="<?= $page->urlForLanguage(
				$language->code(), 
				[ 'params' => params() ]
			) ?>" 
			hreflang="<?= $language->code() ?>"
		>
			<?= $language->code() ?>
		</a>
	</li>
<?php endforeach ?>
</ul>

Further reading