CakePHP:如何在值后使用符号格式化非十进制货币?
我正在尝试在我的多语言应用程序中设置法语加拿大货币的格式。我正在使用 NumberHelper 的扩展版本,其中我使用 addFormat
添加法语加拿大人的格式,但问题是我找不到告诉 CakePHP 移动美元的方法符号出现在金额之后。
例如,对于法语加拿大人,$3.57 应显示为 3,57$。
如果我将 after
属性设置为“$”,那么当一个值仅为美分时,它看起来像美元,因此 0.57 美元变成 57$,看起来像 57 美元。
我尝试使用 PHP setlocale
和 money_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的 AppController 中有一个 _setLanguage() 方法,我在 beforeFilter() 回调中调用该方法来设置语言。我添加了此代码来配置法币加拿大货币
这应该适用于 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
This should work in CakePHP 2.3 or higher.
最后,我扩展了 NumberHelper 类,并在 __construct() 方法中简单地添加了我需要的货币。然后,我构建了另一个名为
money
的方法,如果我的语言设置是加拿大法语,则该方法会强制转换任何金额,以确保空格千位不间断,并添加空格美元金额后签名(完整代码如下)。我对此不太满意,但它完成了工作。如果有人可以建议如何在 PHP 中设置用户的区域设置而不破坏数据库插入,我会很感兴趣。
In the end, I extended the NumberHelper class and simply added my needed currencies in the
__construct()
method. Then I built another method calledmoney
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.