Skip to content

Commit

Permalink
Added config.i18n settings gatherer to config/environment, auto-loadi…
Browse files Browse the repository at this point in the history
…ng of all locales in config/locales/*.rb,yml, and config/locales/en.yml as a sample locale [DHH]
  • Loading branch information
dhh committed Nov 18, 2008
1 parent 75fb8df commit d9b92ee
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.0/3.0*

* Added config.i18n settings gatherer to config/environment, auto-loading of all locales in config/locales/*.rb,yml, and config/locales/en.yml as a sample locale [DHH]

* BACKWARDS INCOMPATIBLE: Renamed application.rb to application_controller.rb and removed all the special casing that was in place to support the former. You must do this rename in your own application when you upgrade to this version [DHH]


Expand Down
3 changes: 3 additions & 0 deletions railties/Rakefile
Expand Up @@ -44,6 +44,7 @@ BASE_DIRS = %w(
app
config/environments
config/initializers
config/locales
components
db
doc
Expand Down Expand Up @@ -199,6 +200,8 @@ task :copy_configs do
cp "configs/initializers/inflections.rb", "#{PKG_DESTINATION}/config/initializers/inflections.rb"
cp "configs/initializers/mime_types.rb", "#{PKG_DESTINATION}/config/initializers/mime_types.rb"

cp "configs/locales/en.yml", "#{PKG_DESTINATION}/config/locales/en.yml"

cp "environments/boot.rb", "#{PKG_DESTINATION}/config/boot.rb"
cp "environments/environment.rb", "#{PKG_DESTINATION}/config/environment.rb"
cp "environments/production.rb", "#{PKG_DESTINATION}/config/environments/production.rb"
Expand Down
5 changes: 5 additions & 0 deletions railties/configs/locales/en.yml
@@ -0,0 +1,5 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
hello: "Hello world"
5 changes: 5 additions & 0 deletions railties/environments/environment.rb
Expand Up @@ -45,6 +45,11 @@
# Run "rake -D time" for a list of tasks for finding time zone names. Comment line to use default local time.
config.time_zone = 'UTC'

# The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.
# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de

# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
Expand Down
31 changes: 31 additions & 0 deletions railties/lib/initializer.rb
Expand Up @@ -147,7 +147,10 @@ def process
initialize_dependency_mechanism
initialize_whiny_nils
initialize_temporary_session_directory

initialize_time_zone
initialize_i18n

initialize_framework_settings
initialize_framework_views

Expand Down Expand Up @@ -504,6 +507,18 @@ def initialize_time_zone
end
end

# Set the i18n configuration from config.i18n but special-case for the load_path which should be
# appended to what's already set instead of overwritten.
def initialize_i18n
configuration.i18n.each do |setting, value|
if setting == :load_path
I18n.load_path += value
else
I18n.send("#{setting}=", value)
end
end
end

# Initializes framework-specific settings for each of the loaded frameworks
# (Configuration#frameworks). The available settings map to the accessors
# on each of the corresponding Base classes.
Expand Down Expand Up @@ -732,6 +747,9 @@ def breakpoint_server(_ = nil)
# timezone to <tt>:utc</tt>.
attr_accessor :time_zone

# Accessor for i18n settings.
attr_accessor :i18n

# Create a new Configuration instance, initialized with the default
# values.
def initialize
Expand All @@ -755,6 +773,7 @@ def initialize
self.database_configuration_file = default_database_configuration_file
self.routes_configuration_file = default_routes_configuration_file
self.gems = default_gems
self.i18n = default_i18n

for framework in default_frameworks
self.send("#{framework}=", Rails::OrderedOptions.new)
Expand Down Expand Up @@ -967,6 +986,18 @@ def default_cache_store
def default_gems
[]
end

def default_i18n
i18n = Rails::OrderedOptions.new
i18n.load_path = []

if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
i18n.load_path.flatten!
end

i18n
end
end
end

Expand Down
Expand Up @@ -65,6 +65,9 @@ def manifest
m.template "configs/initializers/mime_types.rb", "config/initializers/mime_types.rb"
m.template "configs/initializers/new_rails_defaults.rb", "config/initializers/new_rails_defaults.rb"

# Locale
m.template "configs/locales/en.yml", "config/locales/en.yml"

# Environments
m.file "environments/boot.rb", "config/boot.rb"
m.template "environments/environment.rb", "config/environment.rb", :assigns => { :freeze => options[:freeze], :app_name => @app_name, :app_secret => secret }
Expand Down Expand Up @@ -143,6 +146,7 @@ def mysql_socket_location
app/views/layouts
config/environments
config/initializers
config/locales
db
doc
lib
Expand Down
49 changes: 47 additions & 2 deletions railties/test/initializer_test.rb
Expand Up @@ -18,7 +18,6 @@ def initialize(envpath)
end

class Initializer_load_environment_Test < Test::Unit::TestCase

def test_load_environment_with_constant
config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
assert_nil $initialize_test_set_from_env
Expand Down Expand Up @@ -260,5 +259,51 @@ def load_plugins!
@initializer.load_plugins
end
end

end

uses_mocha 'i18n settings' do
class InitializerSetupI18nTests < Test::Unit::TestCase
def test_no_config_locales_dir_present_should_return_empty_load_path
File.stubs(:exist?).returns(false)
assert_equal [], Rails::Configuration.new.i18n.load_path
end

def test_config_locales_dir_present_should_be_added_to_load_path
File.stubs(:exist?).returns(true)
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
end

def test_config_defaults_should_be_added_with_config_settings
File.stubs(:exist?).returns(true)
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])

config = Rails::Configuration.new
config.i18n.load_path << "my/other/locale.yml"

assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
end

def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
File.stubs(:exist?).returns(true)
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])

config = Rails::Configuration.new
config.i18n.load_path << "my/other/locale.yml"

Rails::Initializer.run(:initialize_i18n, config)
assert_equal [
"./test/../../activesupport/lib/active_support/locale/en-US.yml",
"./test/../../actionpack/lib/action_view/locale/en-US.yml",
"my/test/locale.yml",
"my/other/locale.yml" ], I18n.load_path
end

def test_setting_another_default_locale
config = Rails::Configuration.new
config.i18n.default_locale = :de
Rails::Initializer.run(:initialize_i18n, config)
assert_equal :de, I18n.default_locale
end
end
end

0 comments on commit d9b92ee

Please sign in to comment.