How to use PHP Locale functions

我们两清 提交于 2021-01-28 05:51:39

问题


I'm running PHP on windows vista. So, I'm trying to get to know how locale functions work. I started with

setlocale(LC_ALL, $locale) and localeconv()

At first it worked with CLDR locale IDs(I think.., Just started learning PHP locales) like "en_US", "en_UK", etc, resulting as demonstrated in the PHP documentation examples. But now setlocale() and localeconv() only works with $locale values like "English_United Kingdom.1252" and "English_United States.1252" which I believe is windows based locale IDs.

So when I do:

var_dump(setlocale(LC_ALL, "en_US"));
var_dump(localeconv());

I get these results:

boolean false

array (size=18)
  'decimal_point' => string '.' (length=1)
  'thousands_sep' => string '' (length=0)
  'int_curr_symbol' => string '' (length=0)
  'currency_symbol' => string '' (length=0)
  'mon_decimal_point' => string '' (length=0)
  'mon_thousands_sep' => string '' (length=0)
  'positive_sign' => string '' (length=0)
  'negative_sign' => string '' (length=0)
  'int_frac_digits' => int 127
  'frac_digits' => int 127
  'p_cs_precedes' => int 127
  'p_sep_by_space' => int 127
  'n_cs_precedes' => int 127
  'n_sep_by_space' => int 127
  'p_sign_posn' => int 127
  'n_sign_posn' => int 127
  'grouping' => 
    array (size=0)
      empty
  'mon_grouping' => 
    array (size=0)
      empty

How do I make my scripts respond to CLDR locale IDs?


回答1:


setlocale() returns false in your case. Manual:

Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform

Try to use one of: "usa", "america", "united states", "united-states", or "us"

http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.90%29.aspx




回答2:


The setlocale, localeconv, and related functions do not work with Unicode CLDR locale identifiers or data. Instead, they vary per operating system, with POSIX locale identifiers and data on *nix systems and Microsoft locale strings and data on Windows.

# works only on Linux after running `locale-gen de_DE.UTF-8`
# but Windows requires an entirely different locale identifier
setlocale(LC_ALL, 'de_DE.UTF-8');
$locale = localeconv();
echo number_format(
    1234.5,
    1,  # fraction digits
    $locale['decimal_point'],
    $locale['thousands_sep']
);  # '1.234,5'

If you’d actually like to use Unicode CLDR locale identifiers and data for a unified experience regardless of your operating system, use the International extension instead. It’s available with PHP 5.3 and is a wrapper around the ICU (International Components for Unicode) library, which provides standardized CLDR locale data.

$fmt = new NumberFormatter('de-DE', NumberFormatter::DECIMAL);  # or 'de_DE'
echo $fmt->format(1234.5);  # '1.234,5'


来源:https://stackoverflow.com/questions/21773588/how-to-use-php-locale-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!