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

Fix #158: never modify the given options hash #174

Merged
merged 1 commit into from
Dec 5, 2012
Merged
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
7 changes: 4 additions & 3 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def reload!
# always return the same translations/values per unique combination of argument
# values.
def translate(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
options = args.last.is_a?(Hash) ? args.pop.dup : {}
key = args.shift
backend = config.backend
locale = options.delete(:locale) || config.locale
Expand Down Expand Up @@ -219,7 +219,7 @@ def translate!(key, options={})
# I18n.transliterate("Jürgen", :locale => :en) # => "Jurgen"
# I18n.transliterate("Jürgen", :locale => :de) # => "Juergen"
def transliterate(*args)
options = args.pop if args.last.is_a?(Hash)
options = args.pop.dup if args.last.is_a?(Hash)
key = args.shift
locale = options && options.delete(:locale) || config.locale
handling = options && (options.delete(:throw) && :throw || options.delete(:raise) && :raise)
Expand All @@ -230,7 +230,8 @@ def transliterate(*args)
end

# Localizes certain objects, such as dates and numbers to local formatting.
def localize(object, options = {})
def localize(object, options = nil)
options = options ? options.dup : {}
locale = options.delete(:locale) || config.locale
format = options.delete(:format) || :default
config.backend.localize(locale, object, format, options)
Expand Down
7 changes: 7 additions & 0 deletions lib/i18n/tests/localization/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def setup
assert_nothing_raised { I18n.l(@date, :format => '%x') }
end

test "localize Date: does not modify the options hash" do
options = { :format => '%b', :locale => :de }
assert_equal 'Mar', I18n.l(@date, options)
assert_equal({ :format => '%b', :locale => :de }, options)
assert_nothing_raised { I18n.l(@date, options.freeze) }
end

test "localize Date: given nil it raises I18n::ArgumentError" do
assert_raise(I18n::ArgumentError) { I18n.l(nil) }
end
Expand Down
7 changes: 7 additions & 0 deletions lib/i18n/tests/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def setup
assert_nothing_raised { I18n.t(:foo, :locale => :xx) }
end

test "lookup: does not modify the options hash" do
options = {}
assert_equal "a", I18n.t(:string, options)
assert_equal({}, options)
assert_nothing_raised { I18n.t(:string, options.freeze) }
end

test "lookup: given an array of keys it translates all of them" do
assert_equal %w(bar baz), I18n.t([:bar, :baz], :scope => [:foo])
end
Expand Down