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]

This commit is contained in:
David Heinemeier Hansson 2008-11-18 14:23:13 +01:00
parent 75fb8dfb99
commit d9b92ee11b
7 changed files with 97 additions and 2 deletions

View File

@ -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]

View File

@ -44,6 +44,7 @@ BASE_DIRS = %w(
app
config/environments
config/initializers
config/locales
components
db
doc
@ -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"

View File

@ -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"

View File

@ -45,6 +45,11 @@ Rails::Initializer.run do |config|
# 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,

View File

@ -147,7 +147,10 @@ module Rails
initialize_dependency_mechanism
initialize_whiny_nils
initialize_temporary_session_directory
initialize_time_zone
initialize_i18n
initialize_framework_settings
initialize_framework_views
@ -504,6 +507,18 @@ Run `rake gems:install` to install the missing gems.
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.
@ -732,6 +747,9 @@ Run `rake gems:install` to install the missing gems.
# 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
@ -755,6 +773,7 @@ Run `rake gems:install` to install the missing gems.
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)
@ -967,6 +986,18 @@ Run `rake gems:install` to install the missing gems.
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

View File

@ -65,6 +65,9 @@ class AppGenerator < Rails::Generator::Base
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 }
@ -143,6 +146,7 @@ class AppGenerator < Rails::Generator::Base
app/views/layouts
config/environments
config/initializers
config/locales
db
doc
lib

View File

@ -18,7 +18,6 @@ class ConfigurationMock < Rails::Configuration
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
@ -260,5 +259,51 @@ uses_mocha "Initializer plugin loading tests" do
@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