2014-05-27 13:39:24 -04:00
|
|
|
require "active_support"
|
2010-06-20 08:44:38 -04:00
|
|
|
require "active_support/file_update_checker"
|
2010-07-01 04:26:45 -04:00
|
|
|
require "active_support/core_ext/array/wrap"
|
2010-06-20 08:44:38 -04:00
|
|
|
|
2017-01-31 15:33:50 -05:00
|
|
|
# :enddoc:
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
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
|
|
|
|
|
2011-02-01 11:12:51 -05:00
|
|
|
# Set the i18n configuration after initialization since a lot of
|
|
|
|
# configuration is still usually done in application initializers.
|
|
|
|
config.after_initialize do |app|
|
|
|
|
I18n::Railtie.initialize_i18n(app)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Trigger i18n config before any eager loading has happened
|
2012-09-17 01:22:18 -04:00
|
|
|
# so it's ready if any classes require it when eager loaded.
|
2011-02-01 11:12:51 -05:00
|
|
|
config.before_eager_load do |app|
|
|
|
|
I18n::Railtie.initialize_i18n(app)
|
|
|
|
end
|
|
|
|
|
2011-08-24 16:25:11 -04:00
|
|
|
@i18n_inited = false
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Setup i18n configuration.
|
2011-02-01 11:12:51 -05:00
|
|
|
def self.initialize_i18n(app)
|
|
|
|
return if @i18n_inited
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
fallbacks = app.config.i18n.delete(:fallbacks)
|
|
|
|
|
2013-12-12 05:28:30 -05:00
|
|
|
# Avoid issues with setting the default_locale by disabling available locales
|
|
|
|
# check while configuring.
|
|
|
|
enforce_available_locales = app.config.i18n.delete(:enforce_available_locales)
|
2014-07-23 16:40:44 -04:00
|
|
|
enforce_available_locales = I18n.enforce_available_locales if enforce_available_locales.nil?
|
2013-12-12 05:28:30 -05:00
|
|
|
I18n.enforce_available_locales = false
|
2013-12-05 06:46:22 -05:00
|
|
|
|
2015-08-04 11:53:15 -04:00
|
|
|
reloadable_paths = []
|
2010-06-20 08:44:38 -04:00
|
|
|
app.config.i18n.each do |setting, value|
|
|
|
|
case setting
|
|
|
|
when :railties_load_path
|
2015-08-04 11:53:15 -04:00
|
|
|
reloadable_paths = value
|
|
|
|
app.config.i18n.load_path.unshift(*value.map(&:existent).flatten)
|
2010-06-20 08:44:38 -04:00
|
|
|
when :load_path
|
|
|
|
I18n.load_path += value
|
|
|
|
else
|
|
|
|
I18n.send("#{setting}=", value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks)
|
|
|
|
|
2013-12-21 12:03:32 -05:00
|
|
|
# Restore available locales check so it will take place from now on.
|
2013-12-12 05:28:30 -05:00
|
|
|
I18n.enforce_available_locales = enforce_available_locales
|
|
|
|
|
2015-08-04 11:53:15 -04:00
|
|
|
directories = watched_dirs_with_extensions(reloadable_paths)
|
2015-11-11 17:08:44 -05:00
|
|
|
reloader = app.config.file_watcher.new(I18n.load_path.dup, directories) do
|
2015-08-04 11:53:15 -04:00
|
|
|
I18n.load_path.keep_if { |p| File.exist?(p) }
|
|
|
|
I18n.load_path |= reloadable_paths.map(&:existent).flatten
|
|
|
|
|
|
|
|
I18n.reload!
|
|
|
|
end
|
|
|
|
|
2012-06-12 04:36:13 -04:00
|
|
|
app.reloaders << reloader
|
2016-02-21 20:25:52 -05:00
|
|
|
app.reloader.to_run do
|
|
|
|
reloader.execute_if_updated { require_unload_lock! }
|
2014-12-15 01:46:23 -05:00
|
|
|
# TODO: remove the following line as soon as the return value of
|
|
|
|
# callbacks is ignored, that is, returning `false` does not
|
|
|
|
# display a deprecation warning or halts the callback chain.
|
|
|
|
true
|
|
|
|
end
|
2011-12-13 05:23:21 -05:00
|
|
|
reloader.execute
|
2010-06-20 08:44:38 -04:00
|
|
|
|
2011-02-01 11:12:51 -05:00
|
|
|
@i18n_inited = true
|
|
|
|
end
|
2010-06-20 08:44:38 -04:00
|
|
|
|
|
|
|
def self.include_fallbacks_module
|
2015-01-31 23:12:37 -05:00
|
|
|
I18n.backend.class.include(I18n::Backend::Fallbacks)
|
2010-06-20 08:44:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.init_fallbacks(fallbacks)
|
|
|
|
include_fallbacks_module
|
|
|
|
|
2016-09-06 10:34:09 -04:00
|
|
|
args = \
|
|
|
|
case fallbacks
|
|
|
|
when ActiveSupport::OrderedOptions
|
|
|
|
[*(fallbacks[:defaults] || []) << fallbacks[:map]].compact
|
|
|
|
when Hash, Array
|
|
|
|
Array.wrap(fallbacks)
|
|
|
|
else # TrueClass
|
|
|
|
[]
|
|
|
|
end
|
2010-06-20 08:44:38 -04:00
|
|
|
|
|
|
|
I18n.fallbacks = I18n::Locale::Fallbacks.new(*args)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
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
|
2015-08-04 11:53:15 -04:00
|
|
|
|
|
|
|
def self.watched_dirs_with_extensions(paths)
|
|
|
|
paths.each_with_object({}) do |path, result|
|
|
|
|
result[path.absolute_current] = path.extensions
|
|
|
|
end
|
|
|
|
end
|
2010-06-20 08:44:38 -04:00
|
|
|
end
|
2011-01-28 07:53:07 -05:00
|
|
|
end
|