The personal finance app for everyone (by everyone)
Ruby
fix: Locale-aware currency formatting (#677)
foXaCe917101853b4c
diff --git a/lib/money/formatting.rb b/lib/money/formatting.rbindex cd160beffc..9c4df8f58f 100644--- a/lib/money/formatting.rb+++ b/lib/money/formatting.rb@@ -31,7 +31,35 @@ module Money::Formattingenddef locale_options(locale)- case [ currency.iso_code, locale.to_sym ]+ locale_sym = (locale || I18n.locale || :en).to_sym++ # French locale: symbol after number with non-breaking space, comma as decimal separator+ if locale_sym == :fr+ return { delimiter: "\u00A0", separator: ",", format: "%n\u00A0%u" }+ end++ # German locale: symbol after number with space, comma as decimal separator+ if locale_sym == :de+ return { delimiter: ".", separator: ",", format: "%n %u" }+ end++ # Spanish locale: symbol after number with space, comma as decimal separator+ if locale_sym == :es+ return { delimiter: ".", separator: ",", format: "%n %u" }+ end++ # Italian locale: symbol after number with space, comma as decimal separator+ if locale_sym == :it+ return { delimiter: ".", separator: ",", format: "%n %u" }+ end++ # Portuguese (Brazil) locale: symbol before, comma as decimal separator+ if locale_sym == :"pt-BR"+ return { delimiter: ".", separator: ",", format: "%u %n" }+ end++ # Currency-specific overrides for remaining locales+ case [ currency.iso_code, locale_sym ]when [ "EUR", :nl ], [ "EUR", :pt ]{ delimiter: ".", separator: ",", format: "%u %n" }when [ "EUR", :en ], [ "EUR", :en_IE ]diff --git a/test/lib/money_test.rb b/test/lib/money_test.rbindex 699a1ab38a..7acf814c92 100644--- a/test/lib/money_test.rb+++ b/test/lib/money_test.rb@@ -90,6 +90,29 @@ class MoneyTest < ActiveSupport::TestCaseassert_equal "€ 1.000,12", Money.new(1000.12, :eur).format(locale: :nl)end+ test "formats correctly for French locale" do+ # French uses non-breaking spaces (NBSP = \u00A0) between thousands and before currency symbol+ assert_equal "1\u00A0000,12\u00A0€", Money.new(1000.12, :eur).format(locale: :fr)+ assert_equal "1\u00A0000,12\u00A0$", Money.new(1000.12, :usd).format(locale: :fr)+ end++ test "formats correctly for German locale" do+ assert_equal "1.000,12 €", Money.new(1000.12, :eur).format(locale: :de)+ assert_equal "1.000,12 $", Money.new(1000.12, :usd).format(locale: :de)+ end++ test "formats correctly for Spanish locale" do+ assert_equal "1.000,12 €", Money.new(1000.12, :eur).format(locale: :es)+ end++ test "formats correctly for Italian locale" do+ assert_equal "1.000,12 €", Money.new(1000.12, :eur).format(locale: :it)+ end++ test "formats correctly for Portuguese (Brazil) locale" do+ assert_equal "R$ 1.000,12", Money.new(1000.12, :brl).format(locale: :"pt-BR")+ end+test "converts currency when rate available" doExchangeRate.expects(:find_or_fetch_rate).returns(OpenStruct.new(rate: 1.2))