fix: Locale-aware currency formatting (#677)

* fix: Locale-aware currency formatting

Add locale-specific formatting for money display:
- French (fr): symbol after number with non-breaking space (1 000,12 €)
- German (de): symbol after number (1.000,12 €)
- Spanish (es): symbol after number (1.000,12 €)
- Italian (it): symbol after number (1.000,12 €)
- Portuguese-Brazil (pt-BR): symbol before with space (R$ 1.000,12)

This follows international conventions where most European languages
place the currency symbol after the number, unlike English.

* fix: Address CodeRabbit review comments

- Use non-breaking spaces (NBSP) for French locale formatting
- Add nil guard in locale_options to prevent NoMethodError
- Add test coverage for Portuguese (Brazil) locale

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
foXaCe
2026-01-16 23:55:51 +01:00
committed by GitHub
parent 9792ab838f
commit 917101853b
2 changed files with 52 additions and 1 deletions

View File

@@ -31,7 +31,35 @@ module Money::Formatting
end
def 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 ]

View File

@@ -90,6 +90,29 @@ class MoneyTest < ActiveSupport::TestCase
assert_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" do
ExchangeRate.expects(:find_or_fetch_rate).returns(OpenStruct.new(rate: 1.2))