i18n locale fallback for localized views #7368

merged 1 commit into from Mar 14, 2013
@@ -1,5 +1,10 @@
## unreleased ##
+* Include I18n locale fallbacks in view lookup.
+ Fixes GH#3512.
+ *Juan Barreneche*
* Fix incorrectly appended square brackets to a multiple select box
if an explicit name has been given and it already ends with "[]".
@@ -44,7 +44,13 @@ def initialize_details(details)
module Accessors #:nodoc:
- register_detail(:locale) { [I18n.locale, I18n.default_locale].uniq }
+ register_detail(:locale) do
+ locales = [I18n.locale]
+ locales.concat(I18n.fallbacks[I18n.locale]) if I18n.respond_to? :fallbacks
+ locales << I18n.default_locale
+ locales.uniq!
+ locales
+ end
register_detail(:formats) { Mime::SET.symbols }
register_detail(:handlers){ Template::Handlers.extensions }
@@ -19,4 +19,13 @@ def test_default_locale_template_is_used_when_locale_is_missing
get :hello_world
assert_equal "Hello World", @response.body
+ def test_use_fallback_locales
+ I18n.locale = :"de-AT"
+ I18n.backend.class.send(:include, I18n::Backend::Fallbacks)
+ I18n.fallbacks[:"de-AT"] = [:de]
+ get :hello_world
+ assert_equal "Gutten Tag", @response.body
+ end

carlosantoniodasilva Aug 17, 2012


How about changing the backend to a new class that includes the fallbacks, and then returning back to the default in an ensure block?


jbarreneche Aug 17, 2012


I liked the idea, but I bumped with a problem with how that module works.

I18n::Backend::Fallbacks modifies the I18n module (https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/fallbacks.rb#L10-23)
This means, that the fact that I load I18n::Backend::Fallbacks is enough to make the test pass (but, has the cons of changing the global state of the I18n module)

Any idea?


carlosantoniodasilva Aug 21, 2012


Sorry for the delay, I can't think of anything off the top of my head now (and I'm sleepy anyway), but I'll try to think about something and bring someone else to take a look, thanks!


jbarreneche Aug 30, 2012


Any news?
Anything else I can do to push it forward?