Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
use I18n to lookup separator and delimiter signs
Browse files Browse the repository at this point in the history
  • Loading branch information
grobie committed Nov 3, 2010
1 parent 6ccb9a0 commit 427175a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'bigdecimal'
require 'i18n' rescue LoadError
require 'money/currency'
require 'money/money'
require 'money/defaults'
Expand Down
28 changes: 22 additions & 6 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,24 +724,40 @@ def symbol
currency.symbol || "¤"
end

# Uses +Currency#delimiter+. If +nil+ is returned, default to ",".
# If I18n is loaded, looks up key +:number.format.delimiter+.
# Otherwise and as fallback it uses +Currency#delimiter+.
# If +nil+ is returned, default to ",".
#
# @return [String]
#
# @example
# Money.new(100, "USD").delimiter #=> ","
def delimiter
currency.delimiter || ","
if Object.const_defined?("I18n")
def delimiter
I18n.t(:"number.format.delimiter", :default => currency.delimiter || ",")
end
else
def delimiter
currency.delimiter || ","
end
end

# Uses +Currency#separator+. If +nil+ is returned, default to ".".
# If I18n is loaded, looks up key +:number.format.seperator+.
# Otherwise and as fallback it uses +Currency#seperator+.
# If +nil+ is returned, default to ",".
#
# @return [String]
#
# @example
# Money.new(100, "USD").separator #=> "."
def separator
currency.separator || "."
if Object.const_defined?("I18n")
def separator
I18n.t(:"number.format.separator", :default => currency.separator || ".")
end
else
def separator
currency.separator || "."
end
end

# Creates a formatted price string according to several rules.
Expand Down
51 changes: 42 additions & 9 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,49 @@ def to_money
Money.empty(currency).symbol.should == "¤"
end

specify "#delimiter works as documented" do
Money.empty("USD").delimiter.should == ","
Money.empty("EUR").delimiter.should == ","
Money.empty("BRL").delimiter.should == "."
end
{
:delimiter => { :default => ",", :other => "." },
:separator => { :default => ".", :other => "," }
}.each do |method, options|
describe "##{method}" do
context "without I18n" do
it "works as documented" do
Money.empty("USD").send(method).should == options[:default]
Money.empty("EUR").send(method).should == options[:default]
Money.empty("BRL").send(method).should == options[:other]
end
end

specify "#separator works as documented" do
Money.empty("USD").separator.should == "."
Money.empty("EUR").separator.should == "."
Money.empty("BRL").separator.should == ","
if Object.const_defined?("I18n")
context "with I18n" do
before :all do
reset_i18n
store_number_formats(:en, method => options[:default])
store_number_formats(:de, method => options[:other])
end

it "looks up #{method} for current locale" do
I18n.locale = :en
Money.empty("USD").send(method).should == options[:default]
I18n.locale = :de
Money.empty("USD").send(method).should == options[:other]
end

it "fallbacks to default behaviour for missing translations" do
I18n.locale = :de
Money.empty("USD").send(method).should == options[:other]
I18n.locale = :fr
Money.empty("USD").send(method).should == options[:default]
end

after :all do
reset_i18n
end
end
else
puts "can't test ##{method} with I18n because it isn't loaded"
end
end
end

describe "#format" do
Expand Down
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@

RSpec.configure do |config|
end

def reset_i18n()
I18n.backend = I18n::Backend::Simple.new
end

def store_number_formats(locale, translations)
I18n.backend.store_translations(locale, {
:number => {
:format => translations
}
})
end

0 comments on commit 427175a

Please sign in to comment.