4
我使用PHP,并且想知道如何通过Internationalization扩展(ICAP库的Wrapper)获取区域设置的默认货币?如何从PHP Intl(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会有什么意义呢?
谢谢,它的工作原理。 – RoboTamer
真棒,救了我很多工作...谢谢 –
在不丹,有2种货币(BTN和INR),但你的示例代码只返回一个(BTN):( –