Skip to content

Commit

Permalink
add ability to disable i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 committed Jun 11, 2011
1 parent 91c4a69 commit 82f5fef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
8 changes: 8 additions & 0 deletions lib/money/money.rb
Expand Up @@ -42,6 +42,11 @@ class << self
#
# @return [Money::Currency]
attr_accessor :default_currency

# Use this to disable i18n even if it's used by other objects in your app.
#
# @return [true,false]
attr_accessor :use_i18n
end

# Set the default bank for creating new +Money+ objects.
Expand All @@ -50,6 +55,9 @@ class << self
# Set the default currency for creating new +Money+ object.
self.default_currency = Currency.new("USD")

# Default to using i18n
self.use_i18n = true

# Create a new money object with value 0.
#
# @param [Currency, String, Symbol] currency The currency to use.
Expand Down
38 changes: 24 additions & 14 deletions lib/money/money/formatting.rb
@@ -1,14 +1,19 @@
class Money
module Formatting

if Object.const_defined?("I18n")
def thousands_separator
I18n.t(
:"number.currency.format.delimiter",
:default => I18n.t(
:"number.format.delimiter",
:default => (currency.thousands_separator || ",")
)
)
if self.class.use_i18n
I18n.t(
:"number.currency.format.delimiter",
:default => I18n.t(
:"number.format.delimiter",
:default => (currency.thousands_separator || ",")
)
)
else
currency.thousands_separator || ","
end
end
else
def thousands_separator
Expand All @@ -17,15 +22,20 @@ def thousands_separator
end
alias :delimiter :thousands_separator


if Object.const_defined?("I18n")
def decimal_mark
I18n.t(
:"number.currency.format.separator",
:default => I18n.t(
:"number.format.separator",
:default => (currency.decimal_mark || ".")
)
)
if self.class.use_i18n
I18n.t(
:"number.currency.format.separator",
:default => I18n.t(
:"number.format.separator",
:default => (currency.decimal_mark || ".")
)
)
else
currency.decimal_mark || "."
end
end
else
def decimal_mark
Expand Down
23 changes: 23 additions & 0 deletions spec/money/formatting_spec.rb
Expand Up @@ -67,6 +67,29 @@
its(:decimal_mark){should == "."}
end

context "with i18n but use_i18n = false" do
before :each do
reset_i18n
I18n.locale = :de
I18n.backend.store_translations(
:de, :number => {:currency => {:format => {:delimiter => ".",
:separator => ","}}}
)

Money.use_i18n = false
end

after :each do
reset_i18n
I18n.locale = :en
Money.use_i18n = true
end

subject{ Money.new(1000_00, :usd) }

its(:format){should == "$1,000.00"}
end

context "with i18n" do
after :each do
reset_i18n
Expand Down

0 comments on commit 82f5fef

Please sign in to comment.