Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Dave Kroondyk
Diego Aguir Selzlein
Doug Droper
Douglas Miller
Drew Bragg
Ed Saunders
Edwin Vlieg
Eloy
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)
- **Potential breaking change**: Remove special handling for Japanese language only
- **Potential breaking change**: Adjust formatting rules to use i18n translations for `:format`
- Updated Armenian Dram sign and HTML entity
- Updated the Turkmen Manat symbol and HTML entity and added disambiguation symbol for TMM
- Expose Money::VERSION
Expand Down
3 changes: 2 additions & 1 deletion lib/money/locale_backend/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class I18n < Base
KEY_MAP = {
thousands_separator: :delimiter,
decimal_mark: :separator,
symbol: :unit
symbol: :unit,
format: :format,
}.freeze

def initialize
Expand Down
8 changes: 6 additions & 2 deletions lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(currency, *raw_rules)

@rules = default_formatting_rules.merge(@rules) unless @rules[:ignore_defaults]
@rules = translate_formatting_rules(@rules) if @rules[:translate]
@rules[:format] ||= determine_format_from_formatting_rules(@rules)
@rules[:format] ||= determine_format
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)
end

Expand Down Expand Up @@ -68,7 +68,11 @@ def translate_formatting_rules(rules)
rules
end

def determine_format_from_formatting_rules(rules)
def determine_format
Money.locale_backend&.lookup(:format, @currency) || default_format
end

def default_format
if currency.format
currency.format
else
Expand Down
4 changes: 4 additions & 0 deletions spec/locale_backend/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
it 'returns decimal_mark based as defined in currency' do
expect(subject.lookup(:decimal_mark, currency)).to eq(',')
end

it 'returns format based as defined in currency' do
expect(subject.lookup(:format, currency)).to eq(nil)
end
end
end
10 changes: 9 additions & 1 deletion spec/locale_backend/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
before do
I18n.locale = :de
I18n.backend.store_translations(:de, number: {
currency: { format: { delimiter: '.', separator: ',', unit: '$' } }
currency: { format: { delimiter: '.', separator: ',', unit: '$', format: '%u%n' } }
})
end

Expand All @@ -36,6 +36,10 @@
it 'returns symbol based on the current locale' do
expect(subject.lookup(:symbol, nil)).to eq('$')
end

it 'returns format based on the current locale' do
expect(subject.lookup(:format, nil)).to eq('%u%n')
end
end

context 'with number.format defined' do
Expand Down Expand Up @@ -65,6 +69,10 @@
it 'returns symbol based on the current locale' do
expect(subject.lookup(:symbol, nil)).to eq(nil)
end

it 'returns format based on the current locale' do
expect(subject.lookup(:format, nil)).to eq(nil)
end
end
end
end
43 changes: 43 additions & 0 deletions spec/money/formatting_rules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,47 @@
expect(rules).to eq(separator: '.')
expect(rules).not_to eq(new_rules)
end

describe 'format' do
context 'when there is a locale backend' do
it 'returns the format from the passed rules' do
currency = Money::Currency.new('EUR')
rules = { format: '%n%u', separator: '.', delimiter: ',' }

expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u')
end

it 'returns the translated format for the locale' do
I18n.backend.store_translations(:fr, number: {
currency: { format: { format: "%n %u" } }
})
currency = Money::Currency.new('EUR')
rules = { separator: '.', delimiter: ',' }

expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%n %u')
end
end

context 'when there is no locale backend' do
it 'returns the format from the passed rules' do
allow(Money).to receive(:locale_backend).and_return(nil)
currency = Money::Currency.new('EUR')
rules = { format: '%n%u', separator: '.', delimiter: ',' }

expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u')
end

it 'returns the default format for the locale' do
allow(Money).to receive(:locale_backend).and_return(nil)
I18n.backend.store_translations(:fr, number: {
currency: { format: { format: "%n %u" } }
})
currency = Money::Currency.new('EUR')
rules = { separator: '.', delimiter: ',' }
allow(currency).to receive(:format).and_return("%u%n")

expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%u%n')
end
end
end
end