From dad80ad7862834543783974a22d316f074cfee66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 20 Jun 2010 14:44:38 +0200 Subject: I18n.reload! is only called if any of the locale files actually changed. --- activesupport/lib/active_support/i18n_railtie.rb | 80 ++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 activesupport/lib/active_support/i18n_railtie.rb (limited to 'activesupport/lib/active_support/i18n_railtie.rb') diff --git a/activesupport/lib/active_support/i18n_railtie.rb b/activesupport/lib/active_support/i18n_railtie.rb new file mode 100644 index 0000000000..d82e54f1d4 --- /dev/null +++ b/activesupport/lib/active_support/i18n_railtie.rb @@ -0,0 +1,80 @@ +require "active_support" +require "rails" +require "active_support/file_update_checker" + +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 + + def self.reloader + @reloader ||= ActiveSupport::FileUpdateChecker.new([]){ I18n.reload! } + end + + # Add I18n::Railtie.reloader to ActionDispatch callbacks. Since, at this + # point, no path was added to the reloader, I18n.reload! is not triggered + # on to_prepare callbacks. This will only happen on the config.after_initialize + # callback below. + initializer "i18n.callbacks" do + ActionDispatch::Callbacks.to_prepare do + I18n::Railtie.reloader.execute_if_updated + end + end + + # Set the i18n configuration only after initialization since a lot of + # configuration is still usually done in application initializers. + config.after_initialize do |app| + fallbacks = app.config.i18n.delete(:fallbacks) + + 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 + else + I18n.send("#{setting}=", value) + end + end + + init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks) + + reloader.paths.concat I18n.load_path + reloader.execute_if_updated + end + + protected + + def self.include_fallbacks_module + I18n.backend.class.send(:include, I18n::Backend::Fallbacks) + end + + def self.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 self.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 \ No newline at end of file -- cgit v1.2.3