Permalink
Browse files

Added config.i18n settings gatherer to config/environment, auto-loadi…

…ng of all locales in config/locales/*.rb,yml, and config/locales/en.yml as a sample locale [DHH]
  • Loading branch information...
1 parent f7a8e39 commit 42c9d372734607c62e5d4ac9db22598c43359827 @dhh dhh committed Nov 18, 2008
View
@@ -1,3 +1,8 @@
+*2.2.2 [Final]*
+
+* 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]
+
+
*2.2.1 [RC2] (November 14th, 2008)*
* Fixed plugin generator so that generated unit tests would subclass ActiveSupport::TestCase, also introduced a helper script to reduce the needed require statements #1137 [Mathias Meyer]
View
@@ -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"
@@ -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"
@@ -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,
@@ -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
@@ -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.
@@ -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
@@ -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)
@@ -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
@@ -64,6 +64,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 }
@@ -142,6 +145,7 @@ def mysql_socket_location
app/views/layouts
config/environments
config/initializers
+ config/locales
db
doc
lib
@@ -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
@@ -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 42c9d37

Please sign in to comment.