CodeIgniter Forums
setlocale() seems to do nothing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: setlocale() seems to do nothing (/showthread.php?tid=87524)



setlocale() seems to do nothing - sjender - 04-28-2023

Hello,
I want to give my users the option to change their locale (only for their account).
I read the locale can be set by using `$this->request->setLocale('nl');`.
But this still prints 'en'.
PHP Code:
        $this->request->setLocale('nl');
        echo $this->request->getLocale();
       
//output: en 

And still the english language files are used.
Is there a way I can dynamicly change the locale (without using the locale in the url) ?


RE: setlocale() seems to do nothing - kenjis - 04-28-2023

You need to set $supportedLocales:
https://github.com/codeigniter4/CodeIgniter4/blob/483674d718ab873e2c482618a0b5beb543620a59/app/Config/App.php#L99


RE: setlocale() seems to do nothing - kenjis - 04-28-2023

Updated the docs.
See https://codeigniter4.github.io/CodeIgniter4/outgoing/localization.html#setting-the-current-locale


RE: setlocale() seems to do nothing - sjender - 04-29-2023

Thanks, that partially did the trick.
Now the getLocale() prints 'nl' instead of 'en'.
Ik have placed '$this->request->setLocale('nl)' on top of my controller method.
But still the english language files are loaded...

If I change the $defaultLocale in Config/App.php it does switch to the NL language files.


RE: setlocale() seems to do nothing - kenjis - 04-29-2023

You need to run $this->request->setLocale('nl') before calling lang().

Or set locale on Language:
PHP Code:
$language Services::language();
$language->setLocale('nl');