From 9f3b3997dbd7af3f1d3df7f893fb0daa1a723425 Mon Sep 17 00:00:00 2001 From: Antonio Tapiador del Dujo Date: Tue, 24 Mar 2009 14:49:47 +0100 Subject: [PATCH] I18n load_path support for Engines Rails will now automatically add locale files found in any engine's locale directory to the I18n.load_path (i.e. files that match the glob pattern "config/locales/**/*.{rb,yml}" relative to engine directories). --- railties/lib/rails/plugin.rb | 12 ++++++++++++ railties/lib/rails/plugin/loader.rb | 7 +++++++ .../plugins/engines/engine/config/locales/en.yml | 2 ++ railties/test/initializer_test.rb | 1 + railties/test/plugin_loader_test.rb | 8 ++++++++ 5 files changed, 30 insertions(+), 0 deletions(-) create mode 100644 railties/test/fixtures/plugins/engines/engine/config/locales/en.yml diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 49ec5c7..1c0af64 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -71,6 +71,10 @@ module Rails File.exist?(routing_file) end + # Returns true if there is any localization file in locale_path + def localized? + locale_files.any? + end def view_path File.join(directory, 'app', 'views') @@ -87,6 +91,14 @@ module Rails def routing_file File.join(directory, 'config', 'routes.rb') end + + def locale_path + File.join(directory, 'config', 'locales') + end + + def locale_files + Dir[ File.join(locale_path, '*.{rb,yml}') ] + end private diff --git a/railties/lib/rails/plugin/loader.rb b/railties/lib/rails/plugin/loader.rb index 7ea9c7c..9e2c3c7 100644 --- a/railties/lib/rails/plugin/loader.rb +++ b/railties/lib/rails/plugin/loader.rb @@ -73,6 +73,7 @@ module Rails def configure_engines if engines.any? add_engine_routing_configurations + add_engine_locales add_engine_controller_paths add_engine_view_paths end @@ -84,6 +85,12 @@ module Rails end end + def add_engine_locales + # reverse it such that the last engine can overwrite translations from the first, like with routes + locale_files = engines.select(&:localized?).collect(&:locale_files).reverse.flatten + I18n.load_path += locale_files - I18n.load_path + end + def add_engine_controller_paths ActionController::Routing.controller_paths += engines.collect {|engine| engine.controller_path } end diff --git a/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml b/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml new file mode 100644 index 0000000..641a7e0 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml @@ -0,0 +1,2 @@ +en: + hello: "Hello from Engine" diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 1fecd62..5bbd060 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -406,6 +406,7 @@ class InitializerSetupI18nTests < Test::Unit::TestCase File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"), File.expand_path(File.dirname(__FILE__) + "/../../activemodel/lib/active_model/locale/en.yml"), File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"), + File.expand_path(File.dirname(__FILE__) + "/../../railties/test/fixtures/plugins/engines/engine/config/locales/en.yml"), "my/test/locale.yml", "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /\.\./ ? File.expand_path(path) : path } end diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index 873e000..9930134 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -156,6 +156,14 @@ class TestPluginLoader < Test::Unit::TestCase plugin_load_paths.each { |path| assert $LOAD_PATH.include?(path) } end + def test_should_add_locale_files_to_I18n_load_path + only_load_the_following_plugins! [:engine] + + @loader.send :add_engine_locales + + assert I18n.load_path.include?(File.join(plugin_fixture_path('engines/engine'), 'config', 'locales', 'en.yml')) + end + private def reset_load_path! -- 1.6.0.5