30

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?

MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • save it in a session? –  Apr 29 '15 at 01:32
  • dunno why u are using putenv, but to retrieve it simply use getenv() – sunshinejr Apr 29 '15 at 01:34
  • What is the scope of this configuration? Are you allowing them to set it for the duration of the connection? Are you looking for a permanent set? Or are you trying to dynamically determine their locale? – jhansen Apr 29 '15 at 01:34
  • They click a link with a GET variable like `?do=locale&lang=en` and each page checks for those GET variables and if set, will run through this function. – MultiDev Apr 29 '15 at 01:36
  • 1
    Try taking a look at http://stackoverflow.com/questions/2316476/how-to-get-the-language-value-from-serverhttp-accept-language-using-php – SameOldNick Apr 29 '15 at 02:00

2 Answers2

58

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.

Community
  • 1
  • 1
Jerry Saravia
  • 3,737
  • 3
  • 24
  • 39
  • 2
    To clarify, I've posted this from php.net: `setlocale (int $category, string $locale)`. If locale is "0", the locale setting is not affected, only the current setting is returned. – userlond Sep 07 '16 at 04:35
  • See also [localeconv](http://php.net/manual/en/function.localeconv.php) to get numeric formats. – Peter Krauss Oct 25 '16 at 13:26
  • 2
    Update for PHP 8.0+. I use `setlocale(LC_ALL, 'en_US');` but `setlocale(LC_ALL, 0);` return just `C`. – vee Dec 08 '21 at 11:09
  • Sorry, I found the problem now. ( https://stackoverflow.com/a/70312801/128761 ) – vee Dec 11 '21 at 05:25
10

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

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
  • 2
    No it doesn't. It gets the *default* locale, not the *current* locale. The default locale is not by definition the same as the current locale. When you're switching between locales, setlocale(LC_WHATEVER, 0) is still the way to retrieve the current locale. – Friek May 31 '18 at 12:30
  • You are again referring to the *default* locale which is, as I already said, not by definition the same as the *current* locale. – Friek Jun 07 '18 at 12:20
  • Code generating documents with different locales, using for example strftime. It’s not possible to pass a locale to strftime, so the locale needs to be changed temporarily. I know the intl module could be used for date/time formatting, but still there are lots of functions in php relying on the current locale. Even the conversion from a float to a string uses the current locale. As such, the default locale is not always helpful when you’re doing i18n stuff and need to switch between different locales. – Friek Jun 07 '18 at 16:15
  • This also not working on all machine. I got "Uncaught Error: Call to undefined function locale_get_default" error. – vee Oct 28 '19 at 12:58
  • @vee, you need to install intl extension first – Handsome Nerd Oct 29 '19 at 03:15
  • Why are you still notifying ME about this? – Friek Oct 29 '19 at 21:37
  • 1
    Despite the discussion on whether it returns default or current locale, `locale_get_default()` did return whatever I had set with `setlocale()` previously, so I guess this answer is the best one. – igorsantos07 Mar 26 '21 at 02:09