Search code examples
phplocalizationinternationalizationlocalecurrency

PHP change locale settings?


is there any way to edit/change the default locale setting in localeconv()?

I would like to use the the money_format function, and it works fine, but the locales for my language/region are not correct.

To be more precise, for Croatia, we use the currency symbol after the number, not before like set in local values?

Are there any ways I can edit this? Or at least manually check, change values, and send new values to setlocale()?

Working on shared hosting btw.


Solution

  • If anyone is interested, I made it work with my own replacement of the money_format() function. It is basically copy-paste from here with added parameters for forceRight and 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;
        }
    }