Skip to content

← Back to Notes on Kirby CMS

Order languages in the front end

By default, kirby()->languages() returns the list of front-end languages ordered alphabetically and doesn’t provide a way to define a different order. Anything on the language objects beyond the documented properties is ignored.

One solution is to keep the order separately in config.php, or we can be slightly mischievous and piggyback on the translations array:

/* languages/ro.php */
<?php

return [
    'code' => 'ro',
    'default' => true,
    'direction' => 'ltr',
    'locale' => 'ro_RO',
    'name' => 'Română',
    'url' => '/',
    'translations' => [
        '__KIRBY_MENU_ORDER__' => 0
    ]
];

Then, in a template or controller, sort by this value:

$languages_ordered = kirby()
	->languages()
	->sortBy(
		fn($lang) => $lang->translations()['__KIRBY_MENU_ORDER__'] ?? INF
	);

The __KIRBY_MENU_ORDER__ is not really meant to be changed by editors, but one side-effect of keeping the order in the translations array is that they could, just as they would any other translation string.

See medienbaecker/kirby-language-access for also managing the visibility of front-end languages.