I am using the following code:
In the top navigation, I have a dropdown menu:
Code:
<ul aria-labelledby="dropdownSubMenu1" class="dropdown-menu border-0 shadow">
<li><a href="<?= base_url(); ?>/lang/set_language/en/<?= str_replace('/', '~', current_url()); ?>" class="dropdown-item"><?=lang('Language.English')?> </a></li>
<li><a href="<?= base_url(); ?>/lang/set_language/nl/<?= str_replace('/', '~', current_url()); ?>" class="dropdown-item"><?=lang('Language.Dutch')?> </a></li>
</ul>
I have a controller in a folder language with the following code to set the language:
PHP Code:
public function set_language()
{
// Get the language id
$uri = current_url(true);
$total = $uri->getTotalSegments();
// get the language id
$id = $uri->getSegment($total-1);
// Get the url to go to after this
$redirect = str_replace ("~", "/", $uri->getSegment($total));
$session = session();
$session->remove('language');
$session->set('language', $id);
return redirect()->to($redirect);
}
This changes the language without changing the url.