1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/railtie.rb
Sven Fuchs 9a3a4d6aef 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>
2010-04-17 19:26:46 +01:00

99 lines
No EOL
2.9 KiB
Ruby

require "active_support"
require "rails"
module ActiveSupport
class Railtie < Rails::Railtie
config.active_support = ActiveSupport::OrderedOptions.new
# Loads support for "whiny nil" (noisy warnings when methods are invoked
# on +nil+ values) if Configuration#whiny_nils is true.
initializer "active_support.initialize_whiny_nils" do |app|
require 'active_support/whiny_nil' if app.config.whiny_nils
end
# Sets the default value for Time.zone
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
initializer "active_support.initialize_time_zone" do |app|
require 'active_support/core_ext/time/zones'
zone_default = Time.__send__(:get_zone, app.config.time_zone)
unless zone_default
raise \
'Value assigned to config.time_zone not recognized.' +
'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
end
Time.zone_default = zone_default
end
end
end
module I18n
class Railtie < Rails::Railtie
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
I18n.reload!
ActionDispatch::Callbacks.to_prepare do
I18n.reload!
end
end
end
# Set the i18n configuration from config.i18n but special-case for
# the load_path which should be appended to what's already set instead of overwritten.
config.after_initialize do |app|
app.config.i18n.each do |setting, value|
case setting
when :railties_load_path
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
end
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