Skip to content

Commit

Permalink
I18n support for plugins
Browse files Browse the repository at this point in the history
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).

[#2325 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
atd authored and jeremy committed Aug 30, 2009
1 parent 0efedf2 commit cf4846c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* I18n support for plugins. #2325 [Antonio Tapiador, Sven Fuchs]

* Ruby 1.9: use UTF-8 for default internal and external encodings. [Jeremy Kemper]

* Added db/seeds.rb as a default file for storing seed data for the database. Can be loaded with rake db:seed (or created alongside the db with db:setup). (This is also known as the "Stop Putting Gawd Damn Seed Data In Your Migrations" feature) [DHH]
Expand Down
12 changes: 12 additions & 0 deletions railties/lib/rails/plugin.rb
Expand Up @@ -71,6 +71,10 @@ def routed?
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')
Expand All @@ -87,6 +91,14 @@ def metal_path
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}') ]

This comment has been minimized.

Copy link
@karmi

karmi Sep 7, 2009

Contributor

Hi, this means only locale files directly in /config are loaded, and does not allow for nested locales like /my_plugin/app/config/locales/models/en.yml, /my_plugin/app/config/locales/defaults/en.yml, etc. Something like:

Dir[ File.join(locale_path, '**', '*.{rb,yml}') ]

would be better?

end


private
Expand Down
7 changes: 7 additions & 0 deletions railties/lib/rails/plugin/loader.rb
Expand Up @@ -73,6 +73,7 @@ def engine_metal_paths
def configure_engines
if engines.any?
add_engine_routing_configurations
add_engine_locales
add_engine_controller_paths
add_engine_view_paths
end
Expand All @@ -84,6 +85,12 @@ def add_engine_routing_configurations
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
Expand Down
@@ -0,0 +1,2 @@
en:
hello: "Hello from Engine"
1 change: 1 addition & 0 deletions railties/test/initializer_test.rb
Expand Up @@ -406,6 +406,7 @@ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
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
Expand Down
8 changes: 8 additions & 0 deletions railties/test/plugin_loader_test.rb
Expand Up @@ -156,6 +156,14 @@ def test_should_add_all_load_paths_from_a_plugin_to_LOAD_PATH_array
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!
Expand Down

1 comment on commit cf4846c

@KieranP
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Now start on migrations/seed data.

Please sign in to comment.