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

4 5 6 7 8 9
module ActiveSupport
  class Railtie < Rails::Railtie
    railtie_name :active_support

    # 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
    railtie_name :i18n
34 35

    # Initialize I18n load paths to an array
36
    config.i18n.railties_load_path = []
37 38
    config.i18n.load_path = []

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

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

      I18n.reload!
64 65
    end
  end
66
end