Get locale

why am having a hard time with getting locale in PHP?

all these examples (which I found when searching) throw errors (either I’m calling an undefined function or class “Locale” is missing…)

$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;

$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;

$coll    = collator_create( 'en_US_California' );
$res_val = collator_get_locale( $coll, Locale::VALID_LOCALE );
$res_act = collator_get_locale( $coll, Locale::ACTUAL_LOCALE );
printf( "Valid locale name: %s\nActual locale name: %s\n", $res_val, $res_act );

why is this so complicated in PHP? in java you just do


it’s like getting Host or current URL or any other env variable…

(if I have to import this “Locale” class, how do I do this? have never seen any PHP with imported classes like you do in java…)

thank you…

because there is no guarantee that you can determine a locale at all. (e.g CLI environments often do not set a locale).

if the class is written in PHP you use include or require. otherwise the classes have to be compiled into PHP (see configure options) or provided as module (similar like apache imports its modules, have a look at Homebrew)

if that value is even defined by the server it is unreliable at best. Rethink your strategy.

The Locale class was introduced in PHP 5.3, so most likely you are running an older version of PHP that doesn’t have the class.

I’m on PHP 5.3.26…

(but yes, it looks like that class (& others?) is missing from my PHP (which it shouldn’t it seems to me, if I’m on PHP 5.3…)

thank you…

for older php just used system locale, so exec system(‘locale’) to get shell locale.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.