1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reload I18n locales in development

This commit is contained in:
Kir Shatrov 2015-08-04 17:53:15 +02:00
parent 7a3ca69959
commit f50e90a1ae
5 changed files with 114 additions and 3 deletions

View file

@ -37,10 +37,12 @@ module I18n
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
app.config.i18n.load_path.unshift(*value)
reloadable_paths = value
app.config.i18n.load_path.unshift(*value.map(&:existent).flatten)
when :load_path
I18n.load_path += value
else
@ -53,7 +55,14 @@ module I18n
# Restore available locales check so it will take place from now on.
I18n.enforce_available_locales = enforce_available_locales
reloader = ActiveSupport::FileUpdateChecker.new(I18n.load_path.dup){ I18n.reload! }
directories = watched_dirs_with_extensions(reloadable_paths)
reloader = ActiveSupport::FileUpdateChecker.new(I18n.load_path.dup, directories) do
I18n.load_path.keep_if { |p| File.exist?(p) }
I18n.load_path |= reloadable_paths.map(&:existent).flatten
I18n.reload!
end
app.reloaders << reloader
ActionDispatch::Reloader.to_prepare do
reloader.execute_if_updated
@ -96,5 +105,11 @@ module I18n
raise "Unexpected fallback type #{fallbacks.inspect}"
end
end
def self.watched_dirs_with_extensions(paths)
paths.each_with_object({}) do |path, result|
result[path.absolute_current] = path.extensions
end
end
end
end

View file

@ -589,7 +589,7 @@ module Rails
# I18n load paths are a special case since the ones added
# later have higher priority.
initializer :add_locales do
config.i18n.railties_load_path.concat(paths["config/locales"].existent)
config.i18n.railties_load_path << paths["config/locales"]
end
initializer :add_view_paths do

View file

@ -123,6 +123,11 @@ module Rails
options[:load_path] ? load_path! : skip_load_path!
end
# :nodoc:
def absolute_current
File.expand_path(@current, @root.path)
end
def children
keys = @root.keys.find_all { |k|
k.start_with?(@current) && k != @current
@ -175,6 +180,11 @@ module Rails
@paths
end
# :nodoc:
def extensions
$1.split(',') if @glob =~ /\{([\S]+)\}/
end
# Expands all paths against the root and return all unique values.
def expanded
raise "You need to set a path root" unless @root.path

View file

@ -132,6 +132,79 @@ en:
assert_equal "2", last_response.body
end
test "new locale files are loaded" do
add_to_config <<-RUBY
config.cache_classes = false
RUBY
app_file "config/locales/en.yml", <<-YAML
en:
foo: "1"
YAML
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get '/i18n', :to => lambda { |env| [200, {}, [I18n.t(:foo)]] }
end
RUBY
require 'rack/test'
extend Rack::Test::Methods
load_app
get "/i18n"
assert_equal "1", last_response.body
# Wait a full second so we have time for changes to propagate
sleep(1)
remove_file "config/locales/en.yml"
app_file "config/locales/custom.en.yml", <<-YAML
en:
foo: "2"
YAML
get "/i18n"
assert_equal "2", last_response.body
end
test "I18n.load_path is reloaded" do
add_to_config <<-RUBY
config.cache_classes = false
RUBY
app_file "config/locales/en.yml", <<-YAML
en:
foo: "1"
YAML
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get '/i18n', :to => lambda { |env| [200, {}, [I18n.load_path.inspect]] }
end
RUBY
require 'rack/test'
extend Rack::Test::Methods
load_app
get "/i18n"
assert_match "en.yml", last_response.body
# Wait a full second so we have time for changes to propagate
sleep(1)
app_file "config/locales/fr.yml", <<-YAML
fr:
foo: "2"
YAML
get "/i18n"
assert_match "fr.yml", last_response.body
assert_match "en.yml", last_response.body
end
# Fallbacks
test "not using config.i18n.fallbacks does not initialize I18n.fallbacks" do
I18n.backend = Class.new(I18n::Backend::Simple).new

View file

@ -62,6 +62,13 @@ class PathsTest < ActiveSupport::TestCase
assert_equal ["/foo/bar/baz"], @root["app/models"].to_a
end
test "absolute current path" do
@root.add "config"
@root.add "config/locales"
assert_equal "/foo/bar/config/locales", @root["config/locales"].absolute_current
end
test "adding multiple physical paths as an array" do
@root.add "app", with: ["/app", "/app2"]
assert_equal ["/app", "/app2"], @root["app"].to_a
@ -215,6 +222,12 @@ class PathsTest < ActiveSupport::TestCase
assert_equal "*.rb", @root["app"].glob
end
test "it should be possible to get extensions by glob" do
@root["app"] = "/app"
@root["app"].glob = "*.{rb,yml}"
assert_equal ["rb", "yml"], @root["app"].extensions
end
test "it should be possible to override a path's default glob without assignment" do
@root.add "app", with: "/app", glob: "*.rb"
assert_equal "*.rb", @root["app"].glob