Fall back to english on MissingInterpolationError
This commit is contained in:
parent
e345f2dcbc
commit
b56838e5b1
3 changed files with 41 additions and 0 deletions
|
|
@ -14,3 +14,5 @@ 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)
|
||||
|
|
|
|||
24
lib/i18n_interpolation_fallbacks.rb
Normal file
24
lib/i18n_interpolation_fallbacks.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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[:fallback] = true
|
||||
I18n.fallbacks[locale].each do |fallback|
|
||||
begin
|
||||
result = super(fallback, key, options)
|
||||
return result unless result.nil?
|
||||
rescue I18n::MissingInterpolationArgument
|
||||
end
|
||||
end
|
||||
options.delete(:fallback)
|
||||
|
||||
return super(locale, nil, options.merge(:default => default)) if default
|
||||
raise(I18n::MissingInterpolationError.new(locale, key, options))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -62,4 +62,19 @@ describe 'making sure the spec runner works' do
|
|||
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
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue