Skip to content

Commit bb1d006

Browse files
authored
Merge pull request #1157 from DRBragg/i18n-formatting
Adjust formatting rules to use i18n translations for `:format`
2 parents 3bfb4a2 + 9f6505c commit bb1d006

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Dave Kroondyk
3030
Diego Aguir Selzlein
3131
Doug Droper
3232
Douglas Miller
33+
Drew Bragg
3334
Ed Saunders
3435
Edwin Vlieg
3536
Eloy

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
2121
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)
2222
- **Potential breaking change**: Remove special handling for Japanese language only
23+
- **Potential breaking change**: Adjust formatting rules to use i18n translations for `:format`
2324
- Updated Armenian Dram sign and HTML entity
2425
- Updated the Turkmen Manat symbol and HTML entity and added disambiguation symbol for TMM
2526
- Expose Money::VERSION

lib/money/locale_backend/i18n.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class I18n < Base
88
KEY_MAP = {
99
thousands_separator: :delimiter,
1010
decimal_mark: :separator,
11-
symbol: :unit
11+
symbol: :unit,
12+
format: :format,
1213
}.freeze
1314

1415
def initialize

lib/money/money/formatting_rules.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(currency, *raw_rules)
1010

1111
@rules = default_formatting_rules.merge(@rules) unless @rules[:ignore_defaults]
1212
@rules = translate_formatting_rules(@rules) if @rules[:translate]
13-
@rules[:format] ||= determine_format_from_formatting_rules(@rules)
13+
@rules[:format] ||= determine_format
1414
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)
1515
end
1616

@@ -68,7 +68,11 @@ def translate_formatting_rules(rules)
6868
rules
6969
end
7070

71-
def determine_format_from_formatting_rules(rules)
71+
def determine_format
72+
Money.locale_backend&.lookup(:format, @currency) || default_format
73+
end
74+
75+
def default_format
7276
if currency.format
7377
currency.format
7478
else

spec/locale_backend/currency_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
it 'returns decimal_mark based as defined in currency' do
1212
expect(subject.lookup(:decimal_mark, currency)).to eq(',')
1313
end
14+
15+
it 'returns format based as defined in currency' do
16+
expect(subject.lookup(:format, currency)).to eq(nil)
17+
end
1418
end
1519
end

spec/locale_backend/i18n_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
before do
2222
I18n.locale = :de
2323
I18n.backend.store_translations(:de, number: {
24-
currency: { format: { delimiter: '.', separator: ',', unit: '$' } }
24+
currency: { format: { delimiter: '.', separator: ',', unit: '$', format: '%u%n' } }
2525
})
2626
end
2727

@@ -36,6 +36,10 @@
3636
it 'returns symbol based on the current locale' do
3737
expect(subject.lookup(:symbol, nil)).to eq('$')
3838
end
39+
40+
it 'returns format based on the current locale' do
41+
expect(subject.lookup(:format, nil)).to eq('%u%n')
42+
end
3943
end
4044

4145
context 'with number.format defined' do
@@ -65,6 +69,10 @@
6569
it 'returns symbol based on the current locale' do
6670
expect(subject.lookup(:symbol, nil)).to eq(nil)
6771
end
72+
73+
it 'returns format based on the current locale' do
74+
expect(subject.lookup(:format, nil)).to eq(nil)
75+
end
6876
end
6977
end
7078
end

spec/money/formatting_rules_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,47 @@
1414
expect(rules).to eq(separator: '.')
1515
expect(rules).not_to eq(new_rules)
1616
end
17+
18+
describe 'format' do
19+
context 'when there is a locale backend' do
20+
it 'returns the format from the passed rules' do
21+
currency = Money::Currency.new('EUR')
22+
rules = { format: '%n%u', separator: '.', delimiter: ',' }
23+
24+
expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u')
25+
end
26+
27+
it 'returns the translated format for the locale' do
28+
I18n.backend.store_translations(:fr, number: {
29+
currency: { format: { format: "%n %u" } }
30+
})
31+
currency = Money::Currency.new('EUR')
32+
rules = { separator: '.', delimiter: ',' }
33+
34+
expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%n %u')
35+
end
36+
end
37+
38+
context 'when there is no locale backend' do
39+
it 'returns the format from the passed rules' do
40+
allow(Money).to receive(:locale_backend).and_return(nil)
41+
currency = Money::Currency.new('EUR')
42+
rules = { format: '%n%u', separator: '.', delimiter: ',' }
43+
44+
expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u')
45+
end
46+
47+
it 'returns the default format for the locale' do
48+
allow(Money).to receive(:locale_backend).and_return(nil)
49+
I18n.backend.store_translations(:fr, number: {
50+
currency: { format: { format: "%n %u" } }
51+
})
52+
currency = Money::Currency.new('EUR')
53+
rules = { separator: '.', delimiter: ',' }
54+
allow(currency).to receive(:format).and_return("%u%n")
55+
56+
expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%u%n')
57+
end
58+
end
59+
end
1760
end

0 commit comments

Comments
 (0)