Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP- Get current locale

Tags:

I am setting the locale of the website using this function:

function set_locale($locale) { // ie. en, es      $language = $locale;     putenv("LANG=".$language);      setlocale(LC_ALL, $language);     $domain = "phd";     bindtextdomain($domain, "locale");      bind_textdomain_codeset($domain, 'UTF-8');      textdomain($domain);  } // end set_locale 

When someone visits the site, they have the ability to change their locale. What I am trying to do is somewhere else in the site retrieve what the current locale is.

How would I do this?

like image 434
MultiDev Avatar asked Apr 29 '15 01:04

MultiDev


People also ask

What is locale PHP?

Locale information is language, monetary, time and other information specific for a geographical area. Note: The setlocale() function changes the locale only for the current script. Tip: The locale information can be set to system default with setlocale(LC_ALL,NULL)

How can I get PHP language code?

It is basically an associative array in PHP which has keys like SERVER_NAME, SERVER_ADDR, REQUEST_METHOD , etc. We can use HTTP_ACCEPT_LANGUAGE key to get the language of the browser. echo substr ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ], 0, 2);

How do I get laravel locale?

To fetch current locale you should use App::getLocale() or App::isLocale('...') . Localization. You can also use app()->getLocale() which in Blade would be {{ app()->getLocale() }} .

How do I change my locale to UTF 8?

To enable UTF-8 mode, use ". UTF8" as the code page when using setlocale . For example, setlocale(LC_ALL, ". UTF8") will use the current default Windows ANSI code page (ACP) for the locale and UTF-8 for the code page.


2 Answers

You can call setlocale like so, and it'll return the current local.

$currentLocale = setlocale(LC_ALL, 0); echo $currentLocale; //outputs C/en_US.UTF-8/C/C/C/C on my machine 

Here is documentation from php.net as commented by @JROB

locale

If locale is "0", the locale setting is not affected, only the current setting is returned.

If locale is NULL or the empty string "", the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG".

If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.

like image 61
Jerry Saravia Avatar answered Sep 17 '22 13:09

Jerry Saravia


Another answer after two years!

You can simply user Locale::getDefault() or locale_get_default() to get the current locale.

http://php.net/manual/en/locale.getdefault.php

Intl Locale class is the modern alternative for old functions. Once you started using it, you need to update the local by php.net/manual/en/locale.setdefault.php function

like image 45
Handsome Nerd Avatar answered Sep 21 '22 13:09

Handsome Nerd