最后,我延长NumberHelper类和简单地加入我的需要货币在__construct()
方法。然后,我建立了另一种名为money
的方法,如果我的语言设置是法语加拿大语言,就会强制转换任何金额,以确保数千个空格不间断,并在数量后添加间隔美元符号(完整代码下面)。
我对此并不满意,但它完成了工作。如果任何人都可以建议如何在PHP中设置用户的语言环境而不改变数据库插入,我会很感兴趣。
class CurrencyHelper extends NumberHelper {
var $helpers = array('Session');
function __construct() {
parent::__construct();
$this->addFormat('en_ca', array(
'before' => '$',
'after' => false,
'zero' => 0,
'places' => 2,
'thousands' => ',',
'decimals' => '.',
'negative' => '()',));
$this->addFormat('fr_ca', array(
'before' => false,
'after' => false,
'zero' => 0,
'places' => 2,
'thousands' => ' ',
'decimals' => ',',
'negative' => '()',));
}
function money($amount = 0, $useLocale = true) {
$locale = 'en_ca';
if ($useLocale) {
$s = new CakeSession();
$locale = $s->read('Config.language');
unset($s);
}
$result = str_replace(' ', ' ', $this->currency($amount, $locale)) . ($locale == 'fr_ca' ? ' $' : '');
return $result;
}
}