Permalink
Browse files

Additionally trigger i18n configuration setup before any eager loading [

#6353 state:resolved]

This handles the case where config.cache_classes is true and classes
are loaded before the I18n load path has had a chance to be populated.
  • Loading branch information...
1 parent 806e6f8 commit 5670062fb4c74731fda38566a4ecb69ebf0af9f1 @saimonmoore committed Jan 28, 2011
@@ -24,9 +24,8 @@ def self.reloader
end
end
- # Set the i18n configuration only after initialization since a lot of
- # configuration is still usually done in application initializers.
- config.after_initialize do |app|
+ # Proc to set up i18n configuration
+ init_load_path = Proc.new do |app|
fallbacks = app.config.i18n.delete(:fallbacks)
app.config.i18n.each do |setting, value|
@@ -46,6 +45,14 @@ def self.reloader
reloader.execute_if_updated
end
+ # Set the i18n configuration only after initialization since a lot of
+ # configuration is still usually done in application initializers.
+ config.after_initialize(&init_load_path)
+
+ # Trigger i18n config before any eager loading has happened
+ # so it's ready if any classes require it when eager loaded
+ config.before_eager_load(&init_load_path)
+
protected
def self.include_fallbacks_module
@@ -78,4 +85,4 @@ def self.validate_fallbacks(fallbacks)
end
end
end
-end
+end
@@ -63,6 +63,36 @@ def assert_no_fallbacks
assert I18n.load_path.include?("#{app_path}/config/another_locale.yml")
end
+ test "load_path is populated before eager loaded models" do
+ add_to_config <<-RUBY
+ config.cache_classes = true
+ RUBY
+
+ app_file "config/locales/en.yml", <<-YAML
+en:
+ foo: "1"
+ YAML
+
+ app_file 'app/models/foo.rb', <<-RUBY
+ class Foo < ActiveRecord::Base
+ @foo = I18n.t(:foo)
+ end
+ RUBY
+
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do
+ match '/i18n', :to => lambda { |env| [200, {}, [Foo.instance_variable_get('@foo')]] }
+ end
+ RUBY
+
+ require 'rack/test'
+ extend Rack::Test::Methods
+ load_app
+
+ get "/i18n"
+ assert_equal "1", last_response.body
+ end
+
test "locales are reloaded if they change between requests" do
add_to_config <<-RUBY
config.cache_classes = false

0 comments on commit 5670062

Please sign in to comment.