Automatically reload rails yml files in config/locales
There's attempted support for this in rails 3.2:
https://github.com/rails/rails/blob/v3.2.16/activesupport/lib/active_support/i18n_railtie.rb
However, it comes with this disclaimer:
# Add <tt>I18n::Railtie.reloader</tt> 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.
There's some better looking code in rails 4, so this problem might be fixed there (I don't use rails 4 yet).
I added the following initializer, which checks for changed files is config/locales and reloads I18n:
# config/initializers/reload_locale.rb
if Rails.env == 'development'
locale_reloader = ActiveSupport::FileUpdateChecker.new(Dir["config/locales/*yml"]) do
I18n.backend.reload!
end
ActionDispatch::Callbacks.to_prepare do
locale_reloader.execute_if_updated
end
end
I18n detects changes made to existing files in its load path, but if you want to detect new files under locales
and add them to the load path at runtime, try this.
# config/initializers/i18n_load_path.rb
if Rails.env.development? || Rails.env.test?
locales_path = Rails.root.join("config/locales").to_s
i18n_reloader = ActiveSupport::FileUpdateChecker.new([], locales_path => "yml") do
Dir["#{locales_path}/*.yml"].each do |locale_path|
I18n.load_path << locale_path unless I18n.load_path.include? path
end
end
ActiveSupport::Reloader.to_prepare do
i18n_reloader.execute_if_updated
end
end
That will monitor the locales directory (or any other directory you want to store locales) and add missing ones to the load path when they are added. I18n picks up on these added files so no need to call reload!
.
I think Rails misses new translation files, but adding translations to an existing file should work.
Try force reload it with I18n.backend.reload!
I hope this helps ;)