Skip to content

Commit

Permalink
I18n: Introduce I18n.load_path in favor of I18n.load_translations and…
Browse files Browse the repository at this point in the history
… change Simple backend to load translations lazily. [#1048 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Sven Fuchs authored and lifo committed Sep 20, 2008
1 parent 8cb7d46 commit a3b7fa7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view.rb
Expand Up @@ -43,7 +43,7 @@
require 'action_view/partials'
require 'action_view/template_error'

I18n.load_translations "#{File.dirname(__FILE__)}/action_view/locale/en-US.yml"
I18n.load_path << "#{File.dirname(__FILE__)}/action_view/locale/en-US.yml"

require 'action_view/helpers'

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record.rb
Expand Up @@ -78,4 +78,4 @@
require 'active_record/schema_dumper'

require 'active_record/i18n_interpolation_deprecation'
I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.yml'
I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en-US.yml'
6 changes: 5 additions & 1 deletion activerecord/test/cases/validations_i18n_test.rb
Expand Up @@ -6,12 +6,16 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
def setup
reset_callbacks Topic
@topic = Topic.new
@old_load_path, @old_backend = I18n.load_path, I18n.backend

This comment has been minimized.

Copy link
@Roman2K

Roman2K Sep 21, 2008

The backup of I18n.load_path should be a copy of it (using #dup) since #clear is a destructive operation.

I18n.load_path.clear
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations('en-US', :activerecord => {:errors => {:messages => {:custom => nil}}})
end

def teardown
reset_callbacks Topic
I18n.load_translations File.dirname(__FILE__) + '/../../lib/active_record/locale/en-US.yml'
I18n.load_path.replace @old_load_path
I18n.backend = @old_backend
end

def unique_topic
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support.rb
Expand Up @@ -56,7 +56,7 @@

require 'active_support/secure_random'

I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.yml'
I18n.load_path << File.dirname(__FILE__) + '/active_support/locale/en-US.yml'

Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector')
Dependencies = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Dependencies', 'ActiveSupport::Dependencies')
Expand Down
27 changes: 17 additions & 10 deletions activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb
Expand Up @@ -10,7 +10,8 @@

module I18n
@@backend = nil
@@default_locale = 'en-US'
@@load_path = nil
@@default_locale = :'en-US'
@@exception_handler = :default_exception_handler

class << self
Expand Down Expand Up @@ -49,14 +50,22 @@ def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end

# Allows client libraries to pass arguments that specify a source for
# translation data to be loaded by the backend. The backend defines
# acceptable sources.
# Allow clients to register paths providing translation data sources. The
# backend defines acceptable sources.
#
# E.g. the provided SimpleBackend accepts a list of paths to translation
# files which are either named *.rb and contain plain Ruby Hashes or are
# named *.yml and contain YAML data.)
def load_translations(*args)
backend.load_translations(*args)
# named *.yml and contain YAML data. So for the SimpleBackend clients may
# register translation files like this:
# I18n.load_path << 'path/to/locale/en-US.yml'
def load_path
@@load_path ||= []
end

# Sets the load path instance. Custom implementations are expected to
# behave like a Ruby Array.
def load_path=(load_path)
@@load_path = load_path
end

# Translates, pluralizes and interpolates a given key using a given locale,
Expand Down Expand Up @@ -175,6 +184,4 @@ def normalize_translation_keys(locale, key, scope)
keys.flatten.map{|k| k.to_sym}
end
end
end


end
@@ -1,4 +1,4 @@
require 'strscan'
require 'yaml'

module I18n
module Backend
Expand Down Expand Up @@ -59,7 +59,16 @@ def localize(locale, object, format = :default)
object.strftime(format)
end

def initialized?
@initialized ||= false
end

protected

def init_translations
load_translations(*I18n.load_path)
@initialized = true
end

def translations
@translations ||= {}
Expand All @@ -72,6 +81,7 @@ def translations
# <tt>%w(currency format)</tt>.
def lookup(locale, key, scope = [])
return unless key
init_translations unless initialized?
keys = I18n.send :normalize_translation_keys, locale, key, scope
keys.inject(translations){|result, k| result[k.to_sym] or return nil }
end
Expand All @@ -94,7 +104,7 @@ def default(locale, default, options = {})
rescue MissingTranslationData
nil
end

# Picks a translation from an array according to English pluralization
# rules. It will pick the first translation if count is not equal to 1
# and the second translation if it is equal to 1. Other backends can
Expand Down

0 comments on commit a3b7fa7

Please sign in to comment.