Skip to content
This repository
Browse code

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...
commit d9b92ee11b33fed5c7a94a91415fa846705f7dd3 1 parent 75fb8df
David Heinemeier Hansson authored November 18, 2008
2  railties/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.3.0/3.0*
2 2
 
  3
+* 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]
  4
+
3 5
 * 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]
4 6
 
5 7
 
3  railties/Rakefile
@@ -44,6 +44,7 @@ BASE_DIRS = %w(
44 44
   app
45 45
   config/environments
46 46
   config/initializers
  47
+  config/locales
47 48
   components
48 49
   db
49 50
   doc
@@ -199,6 +200,8 @@ task :copy_configs do
199 200
   cp "configs/initializers/inflections.rb", "#{PKG_DESTINATION}/config/initializers/inflections.rb"
200 201
   cp "configs/initializers/mime_types.rb",  "#{PKG_DESTINATION}/config/initializers/mime_types.rb"
201 202
 
  203
+  cp "configs/locales/en.yml", "#{PKG_DESTINATION}/config/locales/en.yml"
  204
+
202 205
   cp "environments/boot.rb",        "#{PKG_DESTINATION}/config/boot.rb"
203 206
   cp "environments/environment.rb", "#{PKG_DESTINATION}/config/environment.rb"
204 207
   cp "environments/production.rb",  "#{PKG_DESTINATION}/config/environments/production.rb"
5  railties/configs/locales/en.yml
... ...
@@ -0,0 +1,5 @@
  1
+# Sample localization file for English. Add more files in this directory for other locales.
  2
+# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
  3
+
  4
+en:
  5
+  hello: "Hello world"
5  railties/environments/environment.rb
@@ -45,6 +45,11 @@
45 45
   # Run "rake -D time" for a list of tasks for finding time zone names. Comment line to use default local time.
46 46
   config.time_zone = 'UTC'
47 47
 
  48
+  # The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
  49
+  # All files from config/locales/*.rb,yml are added automatically.
  50
+  # config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
  51
+  # config.i18n.default_locale = :de
  52
+
48 53
   # Your secret key for verifying cookie session data integrity.
49 54
   # If you change this key, all old sessions will become invalid!
50 55
   # Make sure the secret is at least 30 characters and all random, 
31  railties/lib/initializer.rb
@@ -147,7 +147,10 @@ def process
147 147
       initialize_dependency_mechanism
148 148
       initialize_whiny_nils
149 149
       initialize_temporary_session_directory
  150
+
150 151
       initialize_time_zone
  152
+      initialize_i18n
  153
+
151 154
       initialize_framework_settings
152 155
       initialize_framework_views
153 156
 
@@ -504,6 +507,18 @@ def initialize_time_zone
504 507
       end
505 508
     end
506 509
 
  510
+    # Set the i18n configuration from config.i18n but special-case for the load_path which should be 
  511
+    # appended to what's already set instead of overwritten.
  512
+    def initialize_i18n
  513
+      configuration.i18n.each do |setting, value|
  514
+        if setting == :load_path
  515
+          I18n.load_path += value
  516
+        else
  517
+          I18n.send("#{setting}=", value)
  518
+        end
  519
+      end
  520
+    end
  521
+
507 522
     # Initializes framework-specific settings for each of the loaded frameworks
508 523
     # (Configuration#frameworks). The available settings map to the accessors
509 524
     # on each of the corresponding Base classes.
@@ -732,6 +747,9 @@ def breakpoint_server(_ = nil)
732 747
     # timezone to <tt>:utc</tt>.
733 748
     attr_accessor :time_zone
734 749
 
  750
+    # Accessor for i18n settings.
  751
+    attr_accessor :i18n
  752
+
735 753
     # Create a new Configuration instance, initialized with the default
736 754
     # values.
737 755
     def initialize
@@ -755,6 +773,7 @@ def initialize
755 773
       self.database_configuration_file  = default_database_configuration_file
756 774
       self.routes_configuration_file    = default_routes_configuration_file
757 775
       self.gems                         = default_gems
  776
+      self.i18n                         = default_i18n
758 777
 
759 778
       for framework in default_frameworks
760 779
         self.send("#{framework}=", Rails::OrderedOptions.new)
@@ -967,6 +986,18 @@ def default_cache_store
967 986
       def default_gems
968 987
         []
969 988
       end
  989
+
  990
+      def default_i18n
  991
+        i18n = Rails::OrderedOptions.new
  992
+        i18n.load_path = []
  993
+
  994
+        if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
  995
+          i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
  996
+          i18n.load_path.flatten!
  997
+        end
  998
+
  999
+        i18n
  1000
+      end
970 1001
   end
971 1002
 end
972 1003
 
4  railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -65,6 +65,9 @@ def manifest
65 65
       m.template "configs/initializers/mime_types.rb", "config/initializers/mime_types.rb"
66 66
       m.template "configs/initializers/new_rails_defaults.rb", "config/initializers/new_rails_defaults.rb"
67 67
 
  68
+      # Locale
  69
+      m.template "configs/locales/en.yml", "config/locales/en.yml"
  70
+
68 71
       # Environments
69 72
       m.file "environments/boot.rb",    "config/boot.rb"
70 73
       m.template "environments/environment.rb", "config/environment.rb", :assigns => { :freeze => options[:freeze], :app_name => @app_name, :app_secret => secret }
@@ -143,6 +146,7 @@ def mysql_socket_location
143 146
     app/views/layouts
144 147
     config/environments
145 148
     config/initializers
  149
+    config/locales
146 150
     db
147 151
     doc
148 152
     lib
49  railties/test/initializer_test.rb
@@ -18,7 +18,6 @@ def initialize(envpath)
18 18
 end
19 19
 
20 20
 class Initializer_load_environment_Test < Test::Unit::TestCase
21  
-
22 21
   def test_load_environment_with_constant
23 22
     config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
24 23
     assert_nil $initialize_test_set_from_env
@@ -260,5 +259,51 @@ def load_plugins!
260 259
         @initializer.load_plugins
261 260
       end
262 261
   end
263  
-
264 262
 end
  263
+
  264
+uses_mocha 'i18n settings' do
  265
+  class InitializerSetupI18nTests < Test::Unit::TestCase
  266
+    def test_no_config_locales_dir_present_should_return_empty_load_path
  267
+      File.stubs(:exist?).returns(false)
  268
+      assert_equal [], Rails::Configuration.new.i18n.load_path
  269
+    end
  270
+
  271
+    def test_config_locales_dir_present_should_be_added_to_load_path
  272
+      File.stubs(:exist?).returns(true)
  273
+      Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
  274
+      assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
  275
+    end
  276
+    
  277
+    def test_config_defaults_should_be_added_with_config_settings
  278
+      File.stubs(:exist?).returns(true)
  279
+      Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
  280
+
  281
+      config = Rails::Configuration.new
  282
+      config.i18n.load_path << "my/other/locale.yml"
  283
+
  284
+      assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
  285
+    end
  286
+    
  287
+    def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
  288
+      File.stubs(:exist?).returns(true)
  289
+      Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
  290
+
  291
+      config = Rails::Configuration.new
  292
+      config.i18n.load_path << "my/other/locale.yml"
  293
+
  294
+      Rails::Initializer.run(:initialize_i18n, config)
  295
+      assert_equal [ 
  296
+       "./test/../../activesupport/lib/active_support/locale/en-US.yml",
  297
+       "./test/../../actionpack/lib/action_view/locale/en-US.yml",
  298
+       "my/test/locale.yml",
  299
+       "my/other/locale.yml" ], I18n.load_path
  300
+    end
  301
+    
  302
+    def test_setting_another_default_locale
  303
+      config = Rails::Configuration.new
  304
+      config.i18n.default_locale = :de
  305
+      Rails::Initializer.run(:initialize_i18n, config)
  306
+      assert_equal :de, I18n.default_locale
  307
+    end
  308
+  end
  309
+end

0 notes on commit d9b92ee

Please sign in to comment.
Something went wrong with that request. Please try again.