Skip to content

Commit

Permalink
rename I18n.normalize_translation_keys to normalize_keys and finally …
Browse files Browse the repository at this point in the history
…make it public.
  • Loading branch information
Sven Fuchs committed Jan 21, 2010
1 parent 8f02aa2 commit 20b05fe
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
20 changes: 10 additions & 10 deletions lib/i18n.rb
Expand Up @@ -225,6 +225,16 @@ def localize(object, options = {})
end
alias :l :localize

# Merges the given locale, key and scope into a single array of keys.
# Splits keys that contain dots into multiple keys. Makes sure all
# keys are Symbols.
def normalize_keys(locale, key, scope, separator = nil)
keys = [locale] + Array(scope) + Array(key)
keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
keys = keys.flatten - ['']
keys.map { |k| k.to_sym }
end

# making these private until Ruby 1.9.2 can send to protected methods again
# see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
private
Expand Down Expand Up @@ -263,15 +273,5 @@ def handle_exception(exception, locale, key, options)
@@exception_handler.call(exception, locale, key, options)
end
end

# Merges the given locale, key and scope into a single array of keys.
# Splits keys that contain dots into multiple keys. Makes sure all
# keys are Symbols.
def normalize_translation_keys(locale, key, scope, separator = nil)
keys = [locale] + Array(scope) + Array(key)
keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
keys = keys.flatten - ['']
keys.map { |k| k.to_sym }
end
end
end
2 changes: 1 addition & 1 deletion lib/i18n/backend/active_record/missing.rb
Expand Up @@ -37,7 +37,7 @@ def store_default_translations(locale, key, options = {})
count, scope, default, separator = options.values_at(:count, *Base::RESERVED_KEYS)
separator ||= I18n.default_separator

keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)[1..-1]
keys = I18n.normalize_keys(locale, key, scope, separator)[1..-1]
key = keys.join(separator || I18n.default_separator)

unless ActiveRecord::Translation.locale(locale).lookup(key, separator).exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/base.rb
Expand Up @@ -110,7 +110,7 @@ def translations
def lookup(locale, key, scope = [], options = {})
return unless key
init_translations unless initialized?
keys = I18n.send(:normalize_translation_keys, locale, key, scope, options[:separator])
keys = I18n.normalize_keys(locale, key, scope, options[:separator])
keys.inject(translations) do |result, key|
key = key.to_sym
return nil unless result.is_a?(Hash) && result.has_key?(key)
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/cascade.rb
Expand Up @@ -37,7 +37,7 @@ def lookup(locale, key, scope = [], options = {})
return unless key
return super unless options[:cascade]

locale, *scope = I18n.send(:normalize_translation_keys, locale, key, scope, options[:separator])
locale, *scope = I18n.normalize_keys(locale, key, scope, options[:separator])
key = scope.pop

begin
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/exceptions.rb
Expand Up @@ -21,7 +21,7 @@ class MissingTranslationData < ArgumentError
attr_reader :locale, :key, :options
def initialize(locale, key, opts = nil)
@key, @locale, @options = key, locale, opts || {}
keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
keys = I18n.normalize_keys(locale, key, options[:scope])
keys << 'no key' if keys.size < 2
super "translation missing: #{keys.join(', ')}"
end
Expand Down
16 changes: 8 additions & 8 deletions test/cases/i18n_test.rb
Expand Up @@ -55,20 +55,20 @@ def test_can_set_default_separator
end

def test_normalize_keys
assert_equal [:en, :foo, :bar], I18n.send(:normalize_translation_keys, :en, :bar, :foo)
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, 'baz.buz', 'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, %w(baz buz), %w(foo bar))
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, [:baz, :buz], [:foo, :bar])
assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
end

def test_normalize_keys_should_not_attempt_to_sym_on_empty_string
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo..bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo......bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo..bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo......bar')
end

def test_uses_passed_separator_to_normalize_keys
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz|buz', :'foo|bar', '|')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
end

def test_can_set_exception_handler
Expand Down

7 comments on commit 20b05fe

@iain
Copy link

@iain iain commented on 20b05fe Feb 25, 2010

Choose a reason for hiding this comment

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

You might have added a deprecation warning for this one, because it breaks other gems like formtastic.

@svenfuchs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good call, Iain. I should probably re-release.

@iain
Copy link

@iain iain commented on 20b05fe Feb 25, 2010

Choose a reason for hiding this comment

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

Oh, it wasn't formtastic btw: It was Rails 2.3! Calling t(".foo") in views, when the translation wasn't present. An edgecase, maybe, but there nonetheless.

http://github.com/rails/rails/blob/2-3-stable/actionpack/lib/action_view/helpers/translation_helper.rb#L18

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, this broke Rails build as well. We should not change APIs in tiny releases!
Sven, could you please re-release with a deprecation warning? :D

@iain
Copy link

@iain iain commented on 20b05fe Feb 25, 2010

Choose a reason for hiding this comment

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

@josevalim it's not really API change, since the normalize_translation_keys was private ;)

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

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

@iain hahaha! That's true! :D

@svenfuchs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for all the prompt feedback, guys!

I've re-added the method and delegated to normalize_keys. Pushed to rubygems, too. http://rubygems.org/gems/i18n/versions/0.3.5

Please sign in to comment.