Class: I18n::Railtie

Inherits:
Rails::Railtie show all
Defined in:
activesupport/lib/active_support/i18n_railtie.rb

Constant Summary

Constants inherited from Rails::Railtie

Rails::Railtie::ABSTRACT_RAILTIES

Class Method Summary collapse

Methods inherited from Rails::Railtie

<=>, abstract_railtie?, #config, configure, #configure, console, generators, inherited, #initialize, #inspect, instance, railtie_name, #railtie_namespace, rake_tasks, runner, server, subclasses

Methods included from ActiveSupport::DescendantsTracker

clear, #descendants, descendants, disable_clear!, reject!, store_inherited, subclasses, #subclasses

Methods included from ActiveSupport::DescendantsTracker::ReloadedClassesFiltering

#descendants, #subclasses

Methods included from Rails::Initializable

included, #initializers, #run_initializers

Constructor Details

This class inherits a constructor from Rails::Railtie

Class Method Details

.forward_raise_on_missing_translations_config(app) ⇒ Object

[View source]

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 80

def self.forward_raise_on_missing_translations_config(app)
  ActiveSupport.on_load(:action_view) do
    ActionView::Helpers::TranslationHelper.raise_on_missing_translations = app.config.i18n.raise_on_missing_translations
  end

  ActiveSupport.on_load(:action_controller) do
    AbstractController::Translation.raise_on_missing_translations = app.config.i18n.raise_on_missing_translations
  end

  if app.config.i18n.raise_on_missing_translations &&
      I18n.exception_handler.is_a?(I18n::ExceptionHandler) # Only override the i18n gem's default exception handler.

    I18n.exception_handler = ->(exception, *) {
      exception = exception.to_exception if exception.is_a?(I18n::MissingTranslation)
      raise exception
    }
  end
end

.include_fallbacks_moduleObject

[View source]

99
100
101
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 99

def self.include_fallbacks_module
  I18n.backend.class.include(I18n::Backend::Fallbacks)
end

.init_fallbacks(fallbacks) ⇒ Object

[View source]

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 103

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
      [I18n.default_locale]
    end

  I18n.fallbacks = I18n::Locale::Fallbacks.new(*args)
end

.initialize_i18n(app) ⇒ Object

Setup i18n configuration.

[View source]

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 32

def self.initialize_i18n(app)
  return if @i18n_inited

  fallbacks = app.config.i18n.delete(:fallbacks)

  # Avoid issues with setting the default_locale by disabling available locales
  # check while configuring.
  enforce_available_locales = app.config.i18n.delete(:enforce_available_locales)
  enforce_available_locales = I18n.enforce_available_locales if enforce_available_locales.nil?
  I18n.enforce_available_locales = false

  reloadable_paths = []
  app.config.i18n.each do |setting, value|
    case setting
    when :railties_load_path
      reloadable_paths = value
      app.config.i18n.load_path.unshift(*value.flat_map(&:existent))
    when :load_path
      I18n.load_path += value
    when :raise_on_missing_translations
      forward_raise_on_missing_translations_config(app)
    else
      I18n.public_send("#{setting}=", value)
    end
  end

  init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks)

  # Restore available locales check so it will take place from now on.
  I18n.enforce_available_locales = enforce_available_locales

  if app.config.reloading_enabled?
    directories = watched_dirs_with_extensions(reloadable_paths)
    reloader = app.config.file_watcher.new(I18n.load_path.dup, directories) do
      I18n.load_path.keep_if { |p| File.exist?(p) }
      I18n.load_path |= reloadable_paths.flat_map(&:existent)
    end

    app.reloaders << reloader
    app.reloader.to_run do
      reloader.execute_if_updated { require_unload_lock! }
    end
    reloader.execute
  end

  @i18n_inited = true
end

.validate_fallbacks(fallbacks) ⇒ Object

[View source]

119
120
121
122
123
124
125
126
127
128
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 119

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

.watched_dirs_with_extensions(paths) ⇒ Object

[View source]

130
131
132
133
134
# File 'activesupport/lib/active_support/i18n_railtie.rb', line 130

def self.watched_dirs_with_extensions(paths)
  paths.each_with_object({}) do |path, result|
    result[path.absolute_current] = path.extensions
  end
end