Browse files

make backend methods signatures more consistent

  • Loading branch information...
1 parent 46aad28 commit ef68e892f6bbb73331f441e151d37e69393045be @svenfuchs committed Jul 6, 2008
Showing with 77 additions and 71 deletions.
  1. +11 −11 lib/i18n.rb
  2. +10 −10 lib/i18n/backend/simple.rb
  3. +13 −7 test/i18n_test.rb
  4. +43 −43 test/simple_backend_test.rb
View
22 lib/i18n.rb
@@ -67,21 +67,21 @@ def store_translations(locale, data)
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
#
# Translations can be looked up at any level of this hash using the key argument
- # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date, 'en-US'</tt>
+ # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
#
# Key can be either a single key or a dot-separated key (both Strings and Symbols
# work). <em>E.g.</em>, the short format can be looked up using both:
- # I18n.t 'date.formats.short', 'en-US'
- # I18n.t :'date.formats.short', 'en-US'
+ # I18n.t 'date.formats.short'
+ # I18n.t :'date.formats.short'
#
# Scope can be either a single key, a dot-separated key or an array of keys
# or dot-separated keys. Keys and scopes can be combined freely. So these
# examples will all look up the same short date format:
- # I18n.t 'date.formats.short', 'en-US'
- # I18n.t 'formats.short', 'en-US', :scope => 'date'
- # I18n.t 'short', 'en-US', :scope => 'date.formats'
- # I18n.t 'short', 'en-US', :scope => %w(date formats)
+ # I18n.t 'date.formats.short'
+ # I18n.t 'formats.short', :scope => 'date'
+ # I18n.t 'short', :scope => 'date.formats'
+ # I18n.t 'short', :scope => %w(date formats)
#
# *INTERPOLATION*
#
@@ -139,22 +139,22 @@ def store_translations(locale, data)
# I18n.t [:foo, :bar], :scope => :baz
def translate(key, options = {})
locale = options.delete(:locale) || I18n.locale
- backend.translate key, locale, options
+ backend.translate locale, key, options
rescue I18n::ArgumentError => e
- send @@exception_handler, e, key, locale, options
+ send @@exception_handler, e, locale, key, options
end
alias :t :translate
# Localizes certain objects, such as dates and numbers to local formatting.
def localize(object, options = {})
locale = options[:locale] || I18n.locale
format = options[:format] || :default
- backend.localize(object, locale, format)
+ backend.localize(locale, object, format)
end
alias :l :localize
protected
- def default_exception_handler(exception, key, locale, options)
+ def default_exception_handler(exception, locale, key, options)
if !options[:raise] and I18n::MissingTranslationData === exception
keys = normalize_translation_keys locale, key, options[:scope]
keys << 'no key' if keys.size < 2
View
20 lib/i18n/backend/simple.rb
@@ -21,9 +21,9 @@ def store_translations(locale, data)
merge_translations(locale, data)
end
- def translate(key, locale, options = {})
+ def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
- return key.map{|key| translate key, locale, options } if key.is_a? Array
+ return key.map{|key| translate locale, key, options } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
@@ -39,20 +39,20 @@ def translate(key, locale, options = {})
# Acts the same as +strftime+, but returns a localized version of the
# formatted date string. Takes a key from the date/time formats
# translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
- def localize(object, locale, format = :default)
+ def localize(locale, object, format = :default)
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
type = object.respond_to?(:sec) ? 'time' : 'date'
- formats = translate(:"#{type}.formats", locale)
+ formats = translate(locale, :"#{type}.formats")
format = formats[format.to_sym] if formats && formats[format.to_sym]
# TODO raise exception unless format found?
format = format.to_s.dup
- format.gsub!(/%a/, translate(:"date.abbr_day_names", locale)[object.wday])
- format.gsub!(/%A/, translate(:"date.day_names", locale)[object.wday])
- format.gsub!(/%b/, translate(:"date.abbr_month_names", locale)[object.mon])
- format.gsub!(/%B/, translate(:"date.month_names", locale)[object.mon])
- format.gsub!(/%p/, translate(:"time.#{object.hour < 12 ? :am : :pm}", locale)) if object.respond_to? :hour
+ format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
+ format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
+ format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
+ format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
+ format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
object.strftime(format)
end
@@ -79,7 +79,7 @@ def lookup(locale, key, scope = [])
def default(locale, default, options = {})
case default
when String then default
- when Symbol then translate default, locale, options
+ when Symbol then translate locale, default, options
when Array then default.each do |obj|
result = default(locale, obj, options.dup) and return result
end
View
20 test/i18n_test.rb
@@ -62,27 +62,33 @@ def test_uses_custom_exception_handler
end
def test_delegates_translate_to_backend
- I18n.backend.expects(:translate).with :foo, 'de-DE', {}
+ I18n.backend.expects(:translate).with 'de-DE', :foo, {}
I18n.translate :foo, :locale => 'de-DE'
end
def test_delegates_localize_to_backend
- I18n.backend.expects(:localize).with :whatever, 'de-DE', :default
+ I18n.backend.expects(:localize).with 'de-DE', :whatever, :default
I18n.localize :whatever, :locale => 'de-DE'
end
+ def test_delegates_store_translations_to_backend
+ I18n.backend.expects(:store_translations).with 'de-DE', {:foo => :bar}
+ I18n.store_translations 'de-DE', {:foo => :bar}
+ end
+
def test_delegates_populate_to_backend
I18n.backend.expects(:populate) # can't specify a block here as an expected argument
I18n.populate{ }
end
- def test_delegates_store_translations_to_backend
- I18n.backend.expects(:store_translations).with 'de-DE', {:foo => :bar}
- I18n.store_translations 'de-DE', {:foo => :bar}
+ def test_populate_yields_the_block
+ tmp = nil
+ I18n.populate do tmp = 'yielded' end
+ assert_equal 'yielded', tmp
end
def test_translate_given_no_locale_uses_i18n_locale
- I18n.backend.expects(:translate).with :foo, 'en-US', {}
+ I18n.backend.expects(:translate).with 'en-US', :foo, {}
I18n.translate :foo
end
@@ -111,7 +117,7 @@ def test_translate_with_dot_separated_key_array_and_scope_works
end
def test_translate_with_options_using_scope_works
- I18n.backend.expects(:translate).with(:precision, 'de-DE', :scope => :"currency.format")
+ I18n.backend.expects(:translate).with('de-DE', :precision, :scope => :"currency.format")
I18n.with_options :locale => 'de-DE', :scope => :'currency.format' do |locale|
locale.t :precision
end
View
86 test/simple_backend_test.rb
@@ -87,46 +87,46 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
def test_translate_calls_lookup_with_locale_given
@backend.expects(:lookup).with('de-DE', :bar, [:foo]).returns 'bar'
- @backend.translate :bar, 'de-DE', :scope => [:foo]
+ @backend.translate 'de-DE', :bar, :scope => [:foo]
end
def test_translate_given_a_symbol_as_a_default_translates_the_symbol
- assert_equal 'bar', @backend.translate(nil, 'en-US', :scope => [:foo], :default => :bar)
+ assert_equal 'bar', @backend.translate('en-US', nil, :scope => [:foo], :default => :bar)
end
def test_translate_given_an_array_as_default_uses_the_first_match
- assert_equal 'bar', @backend.translate(:does_not_exist, 'en-US', :scope => [:foo], :default => [:does_not_exist_2, :bar])
+ assert_equal 'bar', @backend.translate('en-US', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
end
def test_translate_an_array_of_keys_translates_all_of_them
- assert_equal %w(bar baz), @backend.translate([:bar, :baz], 'en-US', :scope => [:foo])
+ assert_equal %w(bar baz), @backend.translate('en-US', [:bar, :baz], :scope => [:foo])
end
def test_translate_calls_pluralize
@backend.expects(:pluralize).with 'bar', 1
- @backend.translate :bar, 'en-US', :scope => [:foo], :count => 1
+ @backend.translate 'en-US', :bar, :scope => [:foo], :count => 1
end
def test_translate_calls_interpolate
@backend.expects(:interpolate).with 'bar', {}
- @backend.translate :bar, 'en-US', :scope => [:foo]
+ @backend.translate 'en-US', :bar, :scope => [:foo]
end
def test_translate_calls_interpolate_including_count_as_a_value
@backend.expects(:interpolate).with 'bar', {:count => 1}
- @backend.translate :bar, 'en-US', :scope => [:foo], :count => 1
+ @backend.translate 'en-US', :bar, :scope => [:foo], :count => 1
end
def test_given_no_keys_it_returns_the_default
- assert_equal 'default', @backend.translate(nil, 'en-US', :default => 'default')
+ assert_equal 'default', @backend.translate('en-US', nil, :default => 'default')
end
def test_translate_given_nil_as_a_locale_raises_an_argument_error
- assert_raises(I18n::InvalidLocale){ @backend.translate :bar, nil }
+ assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar }
end
def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
- assert_raises(I18n::MissingTranslationData){ @backend.translate :bogus, 'de-DE' }
+ assert_raises(I18n::MissingTranslationData){ @backend.translate 'de-DE', :bogus }
end
end
@@ -209,47 +209,47 @@ def setup
end
def test_translate_given_the_short_format_it_uses_it
- assert_equal '01. Jan', @backend.localize(@date, 'de-DE', :short)
+ assert_equal '01. Jan', @backend.localize('de-DE', @date, :short)
end
def test_translate_given_the_long_format_it_uses_it
- assert_equal '01. Januar 2008', @backend.localize(@date, 'de-DE', :long)
+ assert_equal '01. Januar 2008', @backend.localize('de-DE', @date, :long)
end
def test_translate_given_the_default_format_it_uses_it
- assert_equal '01.01.2008', @backend.localize(@date, 'de-DE', :default)
+ assert_equal '01.01.2008', @backend.localize('de-DE', @date, :default)
end
def test_translate_given_a_day_name_format_it_returns_a_day_name
- assert_equal 'Dienstag', @backend.localize(@date, 'de-DE', '%A')
+ assert_equal 'Dienstag', @backend.localize('de-DE', @date, '%A')
end
def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
- assert_equal 'Di', @backend.localize(@date, 'de-DE', '%a')
+ assert_equal 'Di', @backend.localize('de-DE', @date, '%a')
end
def test_translate_given_a_month_name_format_it_returns_a_month_name
- assert_equal 'Januar', @backend.localize(@date, 'de-DE', '%B')
+ assert_equal 'Januar', @backend.localize('de-DE', @date, '%B')
end
def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
- assert_equal 'Jan', @backend.localize(@date, 'de-DE', '%b')
+ assert_equal 'Jan', @backend.localize('de-DE', @date, '%b')
end
def test_translate_given_no_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @date, 'de-DE' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @date }
end
def test_translate_given_an_unknown_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @date, 'de-DE', '%x' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @date, '%x' }
end
def test_localize_nil_raises_argument_error
- assert_raises(I18n::ArgumentError) { @backend.localize nil, 'de-DE' }
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de-DE', nil }
end
def test_localize_object_raises_argument_error
- assert_raises(I18n::ArgumentError) { @backend.localize Object.new, 'de-DE' }
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de-DE', Object.new }
end
end
@@ -264,44 +264,44 @@ def setup
end
def test_translate_given_the_short_format_it_uses_it
- assert_equal '01. Jan 06:00', @backend.localize(@morning, 'de-DE', :short)
+ assert_equal '01. Jan 06:00', @backend.localize('de-DE', @morning, :short)
end
def test_translate_given_the_long_format_it_uses_it
- assert_equal '01. Januar 2008 06:00', @backend.localize(@morning, 'de-DE', :long)
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de-DE', @morning, :long)
end
def test_translate_given_the_default_format_it_uses_it
- assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize(@morning, 'de-DE', :default)
+ assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de-DE', @morning, :default)
end
def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
- assert_equal 'Dienstag', @backend.localize(@morning, 'de-DE', '%A')
+ assert_equal 'Dienstag', @backend.localize('de-DE', @morning, '%A')
end
def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
- assert_equal 'Di', @backend.localize(@morning, 'de-DE', '%a')
+ assert_equal 'Di', @backend.localize('de-DE', @morning, '%a')
end
def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
- assert_equal 'Januar', @backend.localize(@morning, 'de-DE', '%B')
+ assert_equal 'Januar', @backend.localize('de-DE', @morning, '%B')
end
def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
- assert_equal 'Jan', @backend.localize(@morning, 'de-DE', '%b')
+ assert_equal 'Jan', @backend.localize('de-DE', @morning, '%b')
end
def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
- assert_equal 'am', @backend.localize(@morning, 'de-DE', '%p')
- assert_equal 'pm', @backend.localize(@evening, 'de-DE', '%p')
+ assert_equal 'am', @backend.localize('de-DE', @morning, '%p')
+ assert_equal 'pm', @backend.localize('de-DE', @evening, '%p')
end
def test_translate_given_no_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @morning, 'de-DE' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @morning }
end
def test_translate_given_an_unknown_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @morning, 'de-DE', '%x' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @morning, '%x' }
end
end
@@ -321,45 +321,45 @@ def teardown
end
def test_translate_given_the_short_format_it_uses_it
- assert_equal '01. Jan 06:00', @backend.localize(@morning, 'de-DE', :short)
+ assert_equal '01. Jan 06:00', @backend.localize('de-DE', @morning, :short)
end
def test_translate_given_the_long_format_it_uses_it
- assert_equal '01. Januar 2008 06:00', @backend.localize(@morning, 'de-DE', :long)
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de-DE', @morning, :long)
end
# TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
# def test_translate_given_the_default_format_it_uses_it
- # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize(@morning, 'de-DE', :default)
+ # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de-DE', @morning, :default)
# end
def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
- assert_equal 'Dienstag', @backend.localize(@morning, 'de-DE', '%A')
+ assert_equal 'Dienstag', @backend.localize('de-DE', @morning, '%A')
end
def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
- assert_equal 'Di', @backend.localize(@morning, 'de-DE', '%a')
+ assert_equal 'Di', @backend.localize('de-DE', @morning, '%a')
end
def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
- assert_equal 'Januar', @backend.localize(@morning, 'de-DE', '%B')
+ assert_equal 'Januar', @backend.localize('de-DE', @morning, '%B')
end
def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
- assert_equal 'Jan', @backend.localize(@morning, 'de-DE', '%b')
+ assert_equal 'Jan', @backend.localize('de-DE', @morning, '%b')
end
def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
- assert_equal 'am', @backend.localize(@morning, 'de-DE', '%p')
- assert_equal 'pm', @backend.localize(@evening, 'de-DE', '%p')
+ assert_equal 'am', @backend.localize('de-DE', @morning, '%p')
+ assert_equal 'pm', @backend.localize('de-DE', @evening, '%p')
end
def test_translate_given_no_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @morning, 'de-DE' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @morning }
end
def test_translate_given_an_unknown_format_it_does_not_fail
- assert_nothing_raised{ @backend.localize @morning, 'de-DE', '%x' }
+ assert_nothing_raised{ @backend.localize 'de-DE', @morning, '%x' }
end
end

0 comments on commit ef68e89

Please sign in to comment.