railtie.rb 2.9 KB
Newer Older
1 2 3
require "active_support"
require "rails"

4 5
module ActiveSupport
  class Railtie < Rails::Railtie
6
    config.active_support = ActiveSupport::OrderedOptions.new
7 8 9

    # Loads support for "whiny nil" (noisy warnings when methods are invoked
    # on +nil+ values) if Configuration#whiny_nils is true.
10
    initializer "active_support.initialize_whiny_nils" do |app|
11 12 13 14 15
      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.
16
    initializer "active_support.initialize_time_zone" do |app|
17 18 19 20 21 22 23 24 25 26 27 28 29 30
      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

31 32
module I18n
  class Railtie < Rails::Railtie
33
    config.i18n = ActiveSupport::OrderedOptions.new
34
    config.i18n.railties_load_path = []
35
    config.i18n.load_path = []
36
    config.i18n.fallbacks = ActiveSupport::OrderedOptions.new
37

38
    initializer "i18n.initialize" do
39
      ActiveSupport.on_load(:i18n) do
40
        I18n.reload!
41 42 43 44

        ActionDispatch::Callbacks.to_prepare do
          I18n.reload!
        end
45 46 47 48 49 50 51
      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|
J
José Valim 已提交
52
        case setting
53
        when :railties_load_path
J
José Valim 已提交
54 55
          app.config.i18n.load_path.unshift(*value)
        when :load_path
56
          I18n.load_path += value
57 58
        when :fallbacks
          init_fallbacks(value) if value && validate_fallbacks(value)
59 60 61 62
        else
          I18n.send("#{setting}=", value)
        end
      end
J
José Valim 已提交
63 64

      I18n.reload!
65
    end
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    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
98
  end
99
end