如何从 PHP Intl(ICU 库)获取默认货币
我使用 PHP,想知道如何通过国际化扩展(ICU 库的包装器)获取区域设置的默认货币?
下面的脚本解释了内容和原因。 我需要一些东西来替换 getCurrCode()
函数。
$accepted_currencies = array('USD','EUR');
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if( ! empty($locale)){
Locale::setDefault($locale);
$currency = getCurrCode();
if( ! in_array($currency, $accepted_currencies)){
$currency = 'USD';
}
}else{
Locale::setDefault('en_US');
}
$fmt = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
$price = $fmt->formatCurrency(1234567.891234567890000, $currency);
我知道,我可以使用 setlocale(LC_MONETARY, $locale); ,但这意味着我必须将所有语言环境安装到 Linux 上,并处理 Linux 发行版的变化。那么首先使用 Intl 的意义何在呢?
I use PHP, and like to know how I can get the default currency for a locale via the Internationalization extension (Wrapper for the ICU library)?
Below is a script that explains, what and why.
I need something to replace the getCurrCode()
function with.
$accepted_currencies = array('USD','EUR');
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if( ! empty($locale)){
Locale::setDefault($locale);
$currency = getCurrCode();
if( ! in_array($currency, $accepted_currencies)){
$currency = 'USD';
}
}else{
Locale::setDefault('en_US');
}
$fmt = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
$price = $fmt->formatCurrency(1234567.891234567890000, $currency);
I know, I could use setlocale(LC_MONETARY, $locale);
but that means I have to install all the locale's on to Linux, and deal with the variation of the Linux distros. What would then be the point of using Intl at the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将区域设置设置为 NumberFormatter 后,您可以获取货币代码,
上面将给出欧元、美元和日元。
Once you set the Locale to the NumberFormatter, you can fetch the Currency Code with
The above would give EUR, USD and JPY.