From f40810a9f32befb5238189111d2be3f6f6e088bd Mon Sep 17 00:00:00 2001 From: calavera Date: Tue, 12 May 2009 16:54:31 +0200 Subject: [PATCH] Added an option to allow to look up for translations with the default locale when a translation is missed with another specific locale --- .../active_support/vendor/i18n-0.1.3/lib/i18n.rb | 19 +++++++++++++++++++ .../vendor/i18n-0.1.3/lib/i18n/backend/simple.rb | 3 +++ .../vendor/i18n-0.1.3/test/simple_backend_test.rb | 11 +++++++++++ 3 files changed, 33 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb index 1b49deb..7a6dbe1 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb @@ -18,6 +18,7 @@ module I18n @@load_path = nil @@default_locale = :'en' @@exception_handler = :default_exception_handler + @@use_default_locale_on_missing_translation = false class << self # Returns the current backend. Defaults to +Backend::Simple+. @@ -60,6 +61,16 @@ module I18n @@exception_handler = exception_handler end + # Tells the backend to look up the same object to translate with the default locale before to send an error. + def use_default_locale_on_missing_translation + @@use_default_locale_on_missing_translation + end + + # Enable the option to look up a translation with the default locale before to send an error. Defaults to false. + def use_default_locale_on_missing_translation=(use_default_locale_on_missing_translation) + @@use_default_locale_on_missing_translation = use_default_locale_on_missing_translation + end + # Allow clients to register paths providing translation data sources. The # backend defines acceptable sources. # @@ -155,6 +166,14 @@ module I18n # or default if no translations for :foo and :bar were found. # I18n.t :foo, :default => [:bar, 'default'] # + # There is also an option that allows to look up the translation for an object with the default locale + # when the translation is missed with another specific locale. + # E.g. with the option enabled + # I18n.use_default_locale_on_missing_translation = true, + # if we don't have a translation for :foo with the locale :es but we have it + # with the locale :en the method I18n.t :foo, :locale => :es returns the translation + # with the locale :en instead of send an error. + # # BULK LOOKUP # # This returns an array with the translations for :foo and :bar. diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb index c32cc76..0fab1a7 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb @@ -31,6 +31,9 @@ module I18n values = options.reject { |name, value| reserved.include?(name) } entry = lookup(locale, key, scope) + if entry.nil? && I18n.use_default_locale_on_missing_translation + entry = lookup(I18n.default_locale, key, scope) + end if entry.nil? entry = default(locale, default, options) if entry.nil? diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb index 65f3ac1..750fe13 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb @@ -186,6 +186,17 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus } end + + def test_translate_with_use_default_locale_option_enabled + I18n.use_default_locale_on_missing_translation = true + + @backend.expects(:lookup).with('de', :bogus, nil).returns(nil) + @backend.expects(:lookup).with(:en, :bogus, nil).returns('bogus bogus') + + assert_equal('bogus bogus', @backend.translate('de', :bogus)) + + I18n.use_default_locale_on_missing_translation = false + end end class I18nSimpleBackendLookupTest < Test::Unit::TestCase -- 1.6.0.4