Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying format via I18n #1115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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