问题描述:

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 ?

网友答案:
$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.

网友答案:

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.

网友答案:

Have a look at the localeconv() function (http://www.php.net/manual/en/function.localeconv.php):

print_r(localeconv());

Outputs (depending on what you set with setlocale()):

Array
(
    [decimal_point] => .
    [thousands_sep] =>
    [int_curr_symbol] => EUR
    [currency_symbol] => €
    [mon_decimal_point] => ,
    [mon_thousands_sep] =>
    [positive_sign] =>
    [negative_sign] => -
    [int_frac_digits] => 2
    [frac_digits] => 2
    [p_cs_precedes] => 1
    [p_sep_by_space] => 1
    [n_cs_precedes] => 1
    [n_sep_by_space] => 1
    [p_sign_posn] => 1
    [n_sign_posn] => 2
    [grouping] => Array
        (
        )

    [mon_grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

)

The main thing you'll probably care about is the int_curr_symbol result.

$data = localeconv();
$symbol = $data['int_curr_symbol'];

switch($symbol){
    case 'EUR':
        // Euro
        break;

    case 'USD':
        // US Dollars
        break;

    // ...
}
相关阅读:
Top