1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge commit 'sven/i18n'

Conflicts:
	activesupport/lib/active_support.rb
This commit is contained in:
Jeremy Kemper 2008-08-27 12:31:07 -07:00
commit 657898c821
6 changed files with 14 additions and 41 deletions

View file

@ -43,9 +43,7 @@ require 'action_view/base'
require 'action_view/partials' require 'action_view/partials'
require 'action_view/template_error' require 'action_view/template_error'
I18n.backend.populate do
I18n.load_translations "#{File.dirname(__FILE__)}/action_view/locale/en-US.yml" I18n.load_translations "#{File.dirname(__FILE__)}/action_view/locale/en-US.yml"
end
require 'action_view/helpers' require 'action_view/helpers'

View file

@ -77,7 +77,5 @@ require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/schema_dumper' require 'active_record/schema_dumper'
I18n.backend.populate do
I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.yml' I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.yml'
end

View file

@ -5,42 +5,37 @@ require 'models/reply'
class ActiveRecordI18nTests < Test::Unit::TestCase class ActiveRecordI18nTests < Test::Unit::TestCase
def setup def setup
reset_translations I18n.backend = I18n::Backend::Simple.new
end end
def test_translated_model_attributes def test_translated_model_attributes
I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } }
assert_equal 'topic title attribute', Topic.human_attribute_name('title') assert_equal 'topic title attribute', Topic.human_attribute_name('title')
end end
def test_translated_model_attributes_with_sti def test_translated_model_attributes_with_sti
I18n.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } }
assert_equal 'reply title attribute', Reply.human_attribute_name('title') assert_equal 'reply title attribute', Reply.human_attribute_name('title')
end end
def test_translated_model_attributes_with_sti_fallback def test_translated_model_attributes_with_sti_fallback
I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } }
assert_equal 'topic title attribute', Reply.human_attribute_name('title') assert_equal 'topic title attribute', Reply.human_attribute_name('title')
end end
def test_translated_model_names def test_translated_model_names
I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} }
assert_equal 'topic model', Topic.human_name assert_equal 'topic model', Topic.human_name
end end
def test_translated_model_names_with_sti def test_translated_model_names_with_sti
I18n.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} } I18n.backend.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} }
assert_equal 'reply model', Reply.human_name assert_equal 'reply model', Reply.human_name
end end
def test_translated_model_names_with_sti_fallback def test_translated_model_names_with_sti_fallback
I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} }
assert_equal 'topic model', Reply.human_name assert_equal 'topic model', Reply.human_name
end end
private
def reset_translations
I18n.backend = I18n::Backend::Simple.new
end
end end

View file

@ -56,9 +56,7 @@ require 'active_support/time_with_zone'
require 'active_support/secure_random' require 'active_support/secure_random'
I18n.populate do
I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.yml' I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.yml'
end
Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector') Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector')
Dependencies = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Dependencies', 'ActiveSupport::Dependencies') Dependencies = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Dependencies', 'ActiveSupport::Dependencies')

View file

@ -49,13 +49,6 @@ module I18n
@@exception_handler = exception_handler @@exception_handler = exception_handler
end end
# Allow client libraries to pass a block that populates the translation
# storage. Decoupled for backends like a db backend that persist their
# translations, so the backend can decide whether/when to yield or not.
def populate(&block)
backend.populate(&block)
end
# Allows client libraries to pass arguments that specify a source for # Allows client libraries to pass arguments that specify a source for
# translation data to be loaded by the backend. The backend defines # translation data to be loaded by the backend. The backend defines
# acceptable sources. # acceptable sources.
@ -66,11 +59,6 @@ module I18n
backend.load_translations(*args) backend.load_translations(*args)
end end
# Stores translations for the given locale in the backend.
def store_translations(locale, data)
backend.store_translations locale, data
end
# Translates, pluralizes and interpolates a given key using a given locale, # Translates, pluralizes and interpolates a given key using a given locale,
# scope, and default, as well as interpolation values. # scope, and default, as well as interpolation values.
# #

View file

@ -3,13 +3,6 @@ require 'strscan'
module I18n module I18n
module Backend module Backend
class Simple class Simple
# Allow client libraries to pass a block that populates the translation
# storage. Decoupled for backends like a db backend that persist their
# translations, so the backend can decide whether/when to yield or not.
def populate(&block)
yield
end
# Accepts a list of paths to translation files. Loads translations from # Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details. # for details.
@ -47,11 +40,14 @@ module I18n
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
type = object.respond_to?(:sec) ? 'time' : 'date' type = object.respond_to?(:sec) ? 'time' : 'date'
# TODO only translate these if format is a String?
formats = translate(locale, :"#{type}.formats") formats = translate(locale, :"#{type}.formats")
format = formats[format.to_sym] if formats && formats[format.to_sym] format = formats[format.to_sym] if formats && formats[format.to_sym]
# TODO raise exception unless format found? # TODO raise exception unless format found?
format = format.to_s.dup format = format.to_s.dup
# TODO only translate these if the format string is actually present
# TODO check which format strings are present, then bulk translate then, then replace them
format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])