PHP更改区域设置?

时间:2011-04-26 13:33:51

标签: php localization internationalization locale currency

有没有办法在localeconv()中编辑/更改默认的区域设置?

我想使用money_format函数,它工作正常,但我的语言/区域的语言环境不正确。

更准确地说,对于克罗地亚,我们在数字后使用货币符号,而不是在本地值中设置之前?

我有什么办法可以编辑吗?或者至少手动检查,更改值并将新值发送到setlocale()

使用共享主机btw。

2 个答案:

答案 0 :(得分:1)

number_format()与货币符号无关,您可能需要money_format(),但是......嗯 - 只需使用number_format()并将所需的任何货币符号附加到返回值。

答案 1 :(得分:1)

如果有人有兴趣,我可以自行更换money_format()功能。 它基本上是here的复制粘贴,添加了 forceRight noSpace

的参数
class Helper_Locales
{
    public static function formatNumber($number, $isMoney=false, $forceRight=false, $noSpace=false) {
        $lg = isset($lg) ? $lg : setlocale(LC_MONETARY, '0');
        $ret = setLocale(LC_ALL, $lg);
        setLocale(LC_TIME, 'Europe/Paris');
        if ($ret===FALSE) {
            echo "Language '$lg' is not supported by this system.\n";
            return;
        }
        $LocaleConfig = localeConv();
        forEach($LocaleConfig as $key => $val) $$key = $val;

        // Sign specifications:
        if ($number>=0) {
            $sign = $positive_sign;
            $sign_posn = $p_sign_posn;
            $sep_by_space = $p_sep_by_space;
            $cs_precedes = $p_cs_precedes;
        } else {
            $sign = $negative_sign;
            $sign_posn = $n_sign_posn;
            $sep_by_space = $n_sep_by_space;
            $cs_precedes = $n_cs_precedes;
        }

        // Number format:
        $n = number_format(abs($number), $frac_digits,
            $decimal_point, $thousands_sep);
        $n = str_replace(' ', ' ', $n);
        switch($sign_posn) {
            case 0: $n = "($n)"; break;
            case 1: $n = "$sign$n"; break;
            case 2: $n = "$n$sign"; break;
            case 3: $n = "$sign$n"; break;
            case 4: $n = "$n$sign"; break;
            default: $n = "$n [error sign_posn=$sign_posn !]";
        }

        // Currency format:
        $currency_symbol = strtolower($currency_symbol);
        $m = number_format(abs($number), $frac_digits,
            $mon_decimal_point, $mon_thousands_sep);
        if ($sep_by_space && !$noSpace) $space = ' '; else $space = '';
        if ($cs_precedes && !$forceRight) $m = "$currency_symbol$space$m";
        else $m = "$m$space$currency_symbol";
        $m = str_replace(' ', ' ', $m);
        switch($sign_posn) {
            case 0: $m = "($m)"; break;
            case 1: $m = "$sign$m"; break;
            case 2: $m = "$m$sign"; break;
            case 3: $m = "$sign$m"; break;
            case 4: $m = "$m$sign"; break;
            default: $m = "$m [error sign_posn=$sign_posn !]";
        }
        if ($isMoney) return $m; else return $n;
    }
}