diff options
author | Sven Fuchs <svenfuchs@artweb-design.de> | 2010-04-16 22:16:21 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-04-17 19:26:46 +0100 |
commit | 9a3a4d6aefa7e2ca94340754eb5541bea1783de0 (patch) | |
tree | da4eba73b9666077ea711a197a1fe91d45eea3ab /activesupport/lib/active_support | |
parent | b7693dc028cea513724681a70dfd4f3e294bbc54 (diff) | |
download | rails-9a3a4d6aefa7e2ca94340754eb5541bea1783de0.tar.gz rails-9a3a4d6aefa7e2ca94340754eb5541bea1783de0.tar.bz2 rails-9a3a4d6aefa7e2ca94340754eb5541bea1783de0.zip |
Make i18n fallbacks configurable and fallback to the default locale by default in production [#4428 state:resolved]
Allows to configure locale fallbacks through config.i18n.fallbacks. The default setting
config.i18n.fallbacks = true in production.rb will make I18n.t lookup fallback to the
I18n.default_locale if a translation could not be found for the current or given locale.
config.fallbacks = true
config.fallbacks.map = { :ca => :es }
config.fallbacks.defaults = [:'es-ES', :es]
config.fallbacks = [:'es-ES', :es]
config.fallbacks = { :ca => :es }
config.fallbacks = [:'es-ES', :es, { :ca => :es }]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/railtie.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index b8d54ff839..0243157e35 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -33,6 +33,7 @@ module I18n config.i18n = ActiveSupport::OrderedOptions.new config.i18n.railties_load_path = [] config.i18n.load_path = [] + config.i18n.fallbacks = ActiveSupport::OrderedOptions.new initializer "i18n.initialize" do ActiveSupport.on_load(:i18n) do @@ -53,6 +54,8 @@ module I18n app.config.i18n.load_path.unshift(*value) when :load_path I18n.load_path += value + when :fallbacks + init_fallbacks(value) if value && validate_fallbacks(value) else I18n.send("#{setting}=", value) end @@ -60,5 +63,37 @@ module I18n I18n.reload! end + + class << self + protected + + def init_fallbacks(fallbacks) + include_fallbacks_module + args = case fallbacks + when ActiveSupport::OrderedOptions + [*(fallbacks[:defaults] || []) << fallbacks[:map]].compact + when Hash, Array + Array.wrap(fallbacks) + else # TrueClass + [] + end + I18n.fallbacks = I18n::Locale::Fallbacks.new(*args) + end + + def include_fallbacks_module + I18n.backend.class.send(:include, I18n::Backend::Fallbacks) + end + + def validate_fallbacks(fallbacks) + case fallbacks + when ActiveSupport::OrderedOptions + !fallbacks.empty? + when TrueClass, Array, Hash + true + else + raise "Unexpected fallback type #{fallbacks.inspect}" + end + end + end end end
\ No newline at end of file |