https://github.com/RubyMoney/money-rails和https://github.com/RubyMoney/google_currency是要走的路。这不是我问的或问题中的,但无论如何这是我现在最接近的答案。这些是我为使其正常工作所做的几个步骤:
在宝石文件中:
gem "json" #if you don't have it
gem "money"
gem "google_currency"
在里面创建一个文件money.rbconfig/initializers
require 'money'
require 'money/bank/google_currency'
require 'json'
MultiJson.engine = :json_gem
Money.default_bank = Money::Bank::GoogleCurrency.new
在product.rb中(或任何需要转换价格的模型)
composed_of :price,
:class_name => "Money",
:mapping => [%w(price price), %w(currency currency_as_string)],
:constructor => Proc.new { |price, currency| Money.new(price || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
在视图文件中:
<%= number_to_currency(product.price.exchange_to(:EUR)) %>
例如,我有一个 IT 语言环境(意大利语)——意大利货币现在是欧元:
您可以将价格转换为欧元。它对我来说非常有用,money gem 使用 Google_currency 将价格从美元转换为欧元,并且区域设置 yml 文件更改了此区域设置的货币,因此您的价格看起来像XXX,XX 欧元,而不是 $XXX,XX。
要为每个区域显示正确的货币,您需要添加:
it:
number:
currency:
format:
format: "%n %u"
unit: "EUR"
在it.yml
文件或其他语言中,您使用该国家/地区的货币。