Skip to content
Open
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
5 changes: 5 additions & 0 deletions lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def translate_formatting_rules(rules)
rescue I18n::MissingTranslationData
# Do nothing
end
begin
rules[:format] ||= I18n.t :format, scope: "number.currency.format", raise: true
rescue I18n::MissingTranslationData
# Do nothing
end
Comment on lines +71 to +75

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this code could be move to determine_format_from_formatting_rules since :symbol_position, :symbol_before_without_space and :symbol_after_without_space are deprecated options.

Something like:

def determine_format_from_formatting_rules(rules)
  begin
    I18n.t :format, scope: "number.currency.format", raise: true
  rescue I18n::MissingTranslationData
    return currency.format if currency.format && !rules.has_key?(:symbol_position)
    symbol_position = symbol_position_from(rules)
    if symbol_position == :before
      rules.fetch(:symbol_before_without_space, true) ? '%u%n' : '%u %n'
    else
      rules[:symbol_after_without_space] ? '%n%u' : '%n %u'
    end
  end
end

rules
end

Expand Down
28 changes: 28 additions & 0 deletions spec/money/formatting_rules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,32 @@
Money::FormattingRules.new(Money::Currency.new('USD'), symbol_position: :after)
end
end

context "when translate is false" do
it "ignores locale's format" do
I18n.backend.store_translations(:fr, number: {
currency: { format: { format: "%n %u" } }
})
# Have the currency's default symbol position be the opposite of the locale's format
allow_any_instance_of(Money::Currency).to receive(:symbol_first).and_return(true)

rules = I18n.with_locale(:fr) {Money::FormattingRules.new(Money::Currency.new('EUR'), translate: false)}

expect(rules[:format]).to eq("%u%n")
end
end

context "when translate is true" do
it "uses locale's format by default" do
I18n.backend.store_translations(:fr, number: {
currency: { format: { format: "%n %u" } }
})
# Have the currency's default symbol position be the opposite of the locale's format
allow_any_instance_of(Money::Currency).to receive(:symbol_first).and_return(true)

rules = I18n.with_locale(:fr) {Money::FormattingRules.new(Money::Currency.new('EUR'), translate: true)}

expect(rules[:format]).to eq("%n %u")
end
end
end