CodeIgniter Forums
Add a global function to return the default locale - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Add a global function to return the default locale (/showthread.php?tid=90217)



Add a global function to return the default locale - Elvis254 - 02-23-2024

I would really love it a miscellaneous global function to fetch the default locale could be added on the framework.
This will avoid writing such code statements:
Code:
$appConfig = new \Config\App();
$dateToday = Time::today(app_timezone(), $appConfig->defaultLocale);



RE: Add a global function to return the default locale - kenjis - 02-23-2024

You can write it as follows:
PHP Code:
$dateToday Time::today(); 

Because the default locale is used by default.

If you really want to get it, you can use PHP's Locale::getDefault().


RE: Add a global function to return the default locale - Elvis254 - 02-24-2024

(02-23-2024, 06:01 PM)kenjis Wrote: You can write it as follows:
PHP Code:
$dateToday Time::today(); 

Because the default locale is used by default.

If you really want to get it, you can use PHP's Locale::getDefault().

Here is the solution.
Code:
rtrim(slash_item('defaultLocale'), '/')

Routing currently includes locale because its negotiated. Anyway thanks.

(02-24-2024, 06:22 AM)Elvis254 Wrote:
(02-23-2024, 06:01 PM)kenjis Wrote: You can write it as follows:
PHP Code:
$dateToday Time::today(); 

Because the default locale is used by default.

If you really want to get it, you can use PHP's Locale::getDefault().

Here is the solution.
Code:
rtrim(slash_item('defaultLocale'), '/')

Routing currently includes locale because its negotiated. Anyway thanks.

Another solution of getting configurations is using the new settings library as shown below.
This one marks the best solution of the problem I was facing yesterday. Am done writing the system. Thanks a lot CodeIgniter Team.

Code:
setting('App.defaultLocale')



RE: Add a global function to return the default locale - kenjis - 02-24-2024

Ah, you want to get the default locale set in Config\App even if content negotiation is done.

PHP Code:
config('App')->defaultLocale;
$this->request->getDefaultLocale(); 



RE: Add a global function to return the default locale - Elvis254 - 02-25-2024

(02-24-2024, 04:55 PM)kenjis Wrote: Ah, you want to get the default locale set in Config\App even if content negotiation is done.

PHP Code:
config('App')->defaultLocale;
$this->request->getDefaultLocale(); 
Yes, this is what I wanted let me update the code I will use the second statement i.e. get it from the request.