diff --git a/config/initializers/locale.rb b/config/initializers/locale.rb index 8ff5ec7f2..6428eb8cc 100644 --- a/config/initializers/locale.rb +++ b/config/initializers/locale.rb @@ -2,9 +2,14 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +require 'i18n_interpolation_fallbacks' + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] I18n.default_locale = DEFAULT_LANGUAGE + +I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationFallbacks) + I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) AVAILABLE_LANGUAGE_CODES.each do |c| if LANGUAGE_CODES_MAP.key?(c) @@ -14,5 +19,4 @@ AVAILABLE_LANGUAGE_CODES.each do |c| I18n.fallbacks[c.to_sym] = [c.to_sym, DEFAULT_LANGUAGE.to_sym, :en] end end -require 'i18n_interpolation_fallbacks' -I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationFallbacks) + diff --git a/features/support/env.rb b/features/support/env.rb index d793c64fc..c2134aa42 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -45,7 +45,6 @@ require File.join(File.dirname(__FILE__), "integration_sessions_controller") require File.join(File.dirname(__FILE__), "..", "..", "spec", "support", "fake_redis") require File.join(File.dirname(__FILE__), "..", "..", "spec", "helper_methods") require File.join(File.dirname(__FILE__), "..", "..", "spec", "support","no_id_on_object") -require File.join(File.dirname(__FILE__), "..", "..", "spec", "support","i18n_raise_errors") require File.join(File.dirname(__FILE__), "..", "..", "spec", "support","user_methods") include HelperMethods Before do diff --git a/lib/i18n_interpolation_fallbacks.rb b/lib/i18n_interpolation_fallbacks.rb index cdf248019..d83ae6c36 100644 --- a/lib/i18n_interpolation_fallbacks.rb +++ b/lib/i18n_interpolation_fallbacks.rb @@ -1,13 +1,10 @@ module I18n module Backend module InterpolationFallbacks - def translate(locale, key, options = {}) - return super if options[:fallback] default = extract_string_or_lambda_default!(options) if options[:default] options.merge!(:default => default) if default - options[:fallback] = true I18n.fallbacks[locale].each do |fallback| begin result = super(fallback, key, options) @@ -15,11 +12,10 @@ module I18n rescue I18n::MissingInterpolationArgument end end - options.delete(:fallback) return super(locale, nil, options) if default raise(I18n::MissingInterpolationArgument.new(options, "key: #{key} in locale: #{locale}")) end end end -end +end \ No newline at end of file diff --git a/spec/lib/i18n_interpolation_fallbacks_spec.rb b/spec/lib/i18n_interpolation_fallbacks_spec.rb new file mode 100644 index 000000000..de71e2411 --- /dev/null +++ b/spec/lib/i18n_interpolation_fallbacks_spec.rb @@ -0,0 +1,64 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe "i18n interpolation fallbacks" do + describe "when string does not require interpolation arguments" do + it "works normally" do + I18n.t('user.invalid', + :resource_name => "user", + :scope => "devise.failure", + :default => [:invalid, "invalid"]).should == "Invalid email or password." + end + end + describe "when string requires interpolation arguments" do + context "current locale has no fallbacks" do + # ago: "%{time} ago" (in en.yml) + it "returns the translation when all arguments are provided" do + I18n.t('ago', :time => "2 months").should == "2 months ago" + end + it "returns the translation without substitution when all arguments are omitted" do + I18n.t('ago').should == "%{time} ago" + end + it "raises a MissingInterpolationArgument when arguments are wrong" do + expect { I18n.t('ago', :not_time => "2 months") }.should raise_exception(I18n::MissingInterpolationArgument) + end + end + context "current locale falls back to English" do + before do + @old_locale = I18n.locale + I18n.locale = 'it' + I18n.backend.store_translations('it', {"nonexistant_key" => "%{random_key} here is some Italian"}) + end + after do + I18n.locale = @old_locale + end + describe "when all arguments are provided" do + it "returns the locale's translation" do + I18n.t('nonexistant_key', :random_key => "Hi Alex,").should == "Hi Alex, here is some Italian" + end + end + describe "when no arguments are provided" do + it "returns the locale's translation without substitution" do + I18n.t('nonexistant_key').should == "%{random_key} here is some Italian" + end + end + describe "when arguments are wrong" do + describe "when the English translation works" do + it "falls back to English" do + I18n.backend.store_translations('en', {"nonexistant_key" => "Working English translation"}) + I18n.t('nonexistant_key', :hey => "what").should == "Working English translation" + end + end + describe "when the English translation does not work" do + it "raises a MissingInterpolationArgument" do + I18n.backend.store_translations('en', {"nonexistant_key" => "%{random_key} also required, so this will fail"}) + expect { I18n.t('nonexistant_key', :hey => "what") }.should raise_exception(I18n::MissingInterpolationArgument) + end + end + end + end + end +end diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index 83af6674c..194c5cd6b 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -61,28 +61,4 @@ describe 'making sure the spec runner works' do alice.comment "yo", :post => person_status end end - - describe I18n do - include ActionView::Helpers::TranslationHelper - it 'MissingInterpolationError in a non-english locale is not fatal' do - I18n.locale = 'it' - I18n.backend.should_receive(:lookup).ordered. - with(:it, 'bob', nil, :hey => "what", :fallback => true). - and_return("%{not_present} failure") - I18n.backend.should_receive(:lookup).ordered. - with(:en, 'bob', nil, :hey => "what", :fallback => true). - and_return("English translation") - translation = translate('bob', :hey => "what") - translation.should == "English translation" - end - it 'MissingInterpolationError with no fallback is fatal' do - I18n.backend.stub!(:lookup). - with(:en, 'bob', nil, :hey => "what", :fallback => true). - and_return("English translation %{that_will_fail}") - lambda { - translate('bob', :hey => "what") - }.should raise_error I18n::MissingInterpolationArgument - end - end - end diff --git a/spec/support/i18n_raise_errors.rb b/spec/support/i18n_raise_errors.rb deleted file mode 100644 index 915ddf919..000000000 --- a/spec/support/i18n_raise_errors.rb +++ /dev/null @@ -1,22 +0,0 @@ -module I18n - def self.just_raise_that_exception(*args) - raise "Translation not found: #{args.first.inspect}" - end -end - -I18n.exception_handler = :just_raise_that_exception -module ActionView - module Helpers - module TranslationHelper - def translate(key, options = {}) - translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true)) - if html_safe_translation_key?(key) && translation.respond_to?(:html_safe) - translation.html_safe - else - translation - end - end - alias :t :translate - end - end -end