CakePHP:如何在值后使用符号格式化非十进制货币?

发布于 2024-12-12 08:49:08 字数 489 浏览 0 评论 0原文

我正在尝试在我的多语言应用程序中设置法语加拿大货币的格式。我正在使用 NumberHelper 的扩展版本,其中我使用 addFormat 添加法语加拿大人的格式,但问题是我找不到告诉 CakePHP 移动美元的方法符号出现在金额之后

例如,对于法语加拿大人,$3.57 应显示为 3,57$。

如果我将 after 属性设置为“$”,那么当一个值仅为美分时,它看起来像美元,因此 0.57 美元变成 57$,看起来像 57 美元。

我尝试使用 PHP setlocalemoney_format 命令设置区域设置,但它搞砸了我的 MySQL 调用,因为我必须将所有 CRUD 的所有货币值转换回英语操作,我现在没有时间。

关于如何让它在 CakePHP 中以可维护的方式工作有什么想法吗?

预先感谢所有经验丰富、头脑聪明的人。

-乔什

I'm trying to format French Canadian currencies in my multi-lingual application. I'm using an extended version of the NumberHelper where I've used addFormat to add a format for French Canadian, but the problem is that I can't find a way to tell CakePHP to move the dollar sign to appear after the amount.

For example, $3.57 should display as 3,57$ for French Canadian.

If I set the after property to be "$" then when a value is only cents, it looks like dollars, so $0.57 becomes 57$ which looks like 57 dollars.

I tried setting the locale using the PHP setlocale and money_format commands, but it screws up my MySQL calls because I have to convert all the currency values back to English for all my CRUD operations, which I don't have time for at the moment.

Any ideas on how I could get this to work in a maintainable way in CakePHP?

Thanks in advance to all the experienced, big brains out there.

-Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-12-19 08:49:20

我的 AppController 中有一个 _setLanguage() 方法,我在 beforeFilter() 回调中调用该方法来设置语言。我添加了此代码来配置法币加拿大货币

if($this->Session->read('Config.language') == 'fra') {
    CakeNumber::addFormat('fr_CA', array(
        'wholeSymbol' => '

这应该适用于 CakePHP 2.3 或更高版本。

, 'wholePosition' => 'after', 'thousands' => ' ', 'decimals' => ',', 'negative' => '()',)); CakeNumber::defaultCurrency('fr_CA'); }

这应该适用于 CakePHP 2.3 或更高版本。

I have a _setLanguage() method in my AppController that I call in the beforeFilter() callback to setup the language. I added this code to configure a french canadian currency

if($this->Session->read('Config.language') == 'fra') {
    CakeNumber::addFormat('fr_CA', array(
        'wholeSymbol' => '

This should work in CakePHP 2.3 or higher.

, 'wholePosition' => 'after', 'thousands' => ' ', 'decimals' => ',', 'negative' => '()',)); CakeNumber::defaultCurrency('fr_CA'); }

This should work in CakePHP 2.3 or higher.

ゝ杯具 2024-12-19 08:49:16

最后,我扩展了 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;
}

}

In the end, I extended the NumberHelper class and simply added my needed currencies in the __construct() method. Then I built another method called money that just brute-forced the conversion of any amount if my language setting was French Canadian to both make sure that the spaces thousands was non-breaking, and to add a spaced dollar sign AFTER the amount (complete code below).

I'm not really happy with this, but it gets the job done. If anybody can suggest how to use set a user's locale in PHP without mangling the database inserts, I'd be interested.

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;
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文