Welcome Guest, Not a member yet? Register   Sign In
Change language dynamically CI 4.4.6
#1

I'm trying to change the language of my app.

I've tried inside the BaseController in the initController method in several ways

PHP Code:
$lingua session('lingua');
config('App')->defaultLocale $lingua

or
PHP Code:
$lingua session('lingua');
(!isset(
$lingua)) ? $this->request->setLocale('it') : $this->request->setLocale(session('lingua')); 
or

PHP Code:
$lingua session('lingua');
config('App')->defaultLocale $lingua;
(!isset(
$lingua)) ? $this->request->setLocale('it') : $this->request->setLocale(session('lingua')); 


If I try to print these variables on the screen I receive:
echo session('language'); returns "en"
echo config('App')->defaultLocale; returns "en"
echo $this->request->getLocale(); return 'it'
If I manually change the value of public string $defaultLocale to 'en' or 'it' everything works and I see the app in the desired language

What should I do to be able to change the language dynamically?

I don't need the URL changed.
Reply
#2

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.
Reply
#3

(03-13-2024, 08:35 AM)JustJohnQ Wrote: 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.

I don't see where you set the value of the app language, I only see a setting of a session value that I already have, my problem is that I can't change the language or I can't change the defaultLocale value, if I I do it by hand works correctly but I can't modify it from code, i.e. based on the value of session('lang').

I don't use the URL like /home/en, I don't need it, I just need to be able to change the language of everything
Reply
#4

Sorry,
My answer was incomplete. The default language is set in the initController function in the BaseController:


PHP Code:
$session = \Config\Services::session();
$language = \Config\Services::language();
$locale $this->request->getLocale();
$currentLanguage $session->get('language');
if(!
$currentLanguage):
    $session->set('language'$locale);
endif;
$language->setLocale($session->language); 


Hope this helps
Reply
#5

(03-13-2024, 09:00 AM)JustJohnQ Wrote: Sorry,
My answer was incomplete. The default language is set in the initController function in the BaseController:


PHP Code:
$session = \Config\Services::session();
$language = \Config\Services::language();
$locale $this->request->getLocale();
$currentLanguage $session->get('language');
if(!
$currentLanguage):
    $session->set('language'$locale);
endif;
$language->setLocale($session->language); 


Hope this helps

Thanks, I think I solved it
Reply




Theme © iAndrew 2016 - Forum software by © MyBB