Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php get current locale for LC_MONETARY

Tags:

php

locale

I'm using this code to display an amount in the current locale.

setlocale(LC_MONETARY, 'it_IT');
echo money_format('%i', $number);

My question is, how can I see the current value for LC_MONETARY ? If I do a simple echo the value seems the same and I can't find any getlocale function.

echo LC_MONETARY;
setlocale(LC_MONETARY, 'it_IT');
echo LC_MONETARY;

Update : LC_MONETARY is the category of function affected so it makes sense the value is the same. But how can I see the current locale info then ?

like image 779
johnlemon Avatar asked Jun 13 '26 09:06

johnlemon


2 Answers

$oldLocale = setlocale(LC_MONETARY, 'it_IT');
// setlocale() will return the old value if the locale could 
// be set (return value greatly depends on the system's underlying 
// setlocale() implementation)

$oldLocale = setlocale(LC_MONETARY, '0');
// using '0' as the locale-argument will result in the current setting 
//being returned without affecting the locale setting itself

See the note for the $locale parameter in the setlocale() documentation.

like image 184
Stefan Gehrig Avatar answered Jun 15 '26 00:06

Stefan Gehrig


The value of the constant LC_MONETARY will never change. When setting a locale with setlocale(LC_MONETARY, ...), you're not changing the LC_MONETARY constant, you're setting the locale for the "monetary" category. This locale setting happens in the background and is not visible outwardly. The LC_MONETARY constant is just an identifier for the category.

Usually you don't need to know what's currently set. You should simply set your desired locale when needed.

like image 36
deceze Avatar answered Jun 14 '26 22:06

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!