$kirby->languages() method we can fetch all information about available languages. Here are two possible ways to implement such a language switch.
The first option is to redirect users to the home page when they select another language. This can be done by using
$language->url() for the link.
The second option is to redirect users to the same page in the selected language. This can be done by using
$page->url($language->code()) for the link.
You can use this third option if you want to redirect the user to the error page or any other page you define, e.g. home or search, in case there is no content available in the target language: