提交 8f6da948 编写于 作者: Y Yehuda Katz

Merge remote branch 'jose/am'

...@@ -6,6 +6,9 @@ gem "rails", "3.0.pre", :path => "railties" ...@@ -6,6 +6,9 @@ gem "rails", "3.0.pre", :path => "railties"
gem lib, '3.0.pre', :path => lib gem lib, '3.0.pre', :path => lib
end end
# AS
gem "i18n", ">= 0.3.0"
# AR # AR
gem "arel", "0.2.pre", :git => "git://github.com/rails/arel.git" gem "arel", "0.2.pre", :git => "git://github.com/rails/arel.git"
gem "sqlite3-ruby", ">= 1.2.5" gem "sqlite3-ruby", ">= 1.2.5"
...@@ -32,4 +35,4 @@ if ENV['CI'] ...@@ -32,4 +35,4 @@ if ENV['CI']
end end
end end
disable_system_gems disable_system_gems
\ No newline at end of file
module ActionMailer module ActionMailer
module AdvAttrAccessor #:nodoc: module AdvAttrAccessor #:nodoc:
def self.included(base) def adv_attr_accessor(*names)
base.extend(ClassMethods) names.each do |name|
end ivar = "@#{name}"
module ClassMethods #:nodoc:
def adv_attr_accessor(*names)
names.each do |name|
ivar = "@#{name}"
define_method("#{name}=") do |value| class_eval <<-ACCESSORS, __FILE__, __LINE__ + 1
instance_variable_set(ivar, value) def #{name}=(value)
#{ivar} = value
end end
define_method(name) do |*parameters| def #{name}(*args)
raise ArgumentError, "expected 0 or 1 parameters" unless parameters.length <= 1 raise ArgumentError, "expected 0 or 1 parameters" unless args.length <= 1
if parameters.empty? if args.empty?
if instance_variable_names.include?(ivar) #{ivar} if instance_variable_names.include?(#{ivar.inspect})
instance_variable_get(ivar)
end
else else
instance_variable_set(ivar, parameters.first) #{ivar} = args.first
end end
end end
end ACCESSORS
self.protected_instance_variables << ivar if self.respond_to?(:protected_instance_variables)
end end
end end
end end
......
...@@ -25,7 +25,8 @@ module ActionMailer #:nodoc: ...@@ -25,7 +25,8 @@ module ActionMailer #:nodoc:
# bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"] # bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"]
# from "system@example.com" # from "system@example.com"
# subject "New account information" # subject "New account information"
# body :account => recipient #
# @account = recipient
# end # end
# end # end
# #
...@@ -45,13 +46,6 @@ module ActionMailer #:nodoc: ...@@ -45,13 +46,6 @@ module ActionMailer #:nodoc:
# address. Setting this is useful when you want delivery notifications sent to a different address than # address. Setting this is useful when you want delivery notifications sent to a different address than
# the one in <tt>from</tt>. # the one in <tt>from</tt>.
# #
# The <tt>body</tt> method has special behavior. It takes a hash which generates an instance variable
# named after each key in the hash containing the value that that key points to.
#
# So, for example, <tt>body :account => recipient</tt> would result
# in an instance variable <tt>@account</tt> with the value of <tt>recipient</tt> being accessible in the
# view.
#
# #
# = Mailer views # = Mailer views
# #
...@@ -71,7 +65,12 @@ module ActionMailer #:nodoc: ...@@ -71,7 +65,12 @@ module ActionMailer #:nodoc:
# You can even use Action Pack helpers in these views. For example: # You can even use Action Pack helpers in these views. For example:
# #
# You got a new note! # You got a new note!
# <%= truncate(note.body, 25) %> # <%= truncate(@note.body, 25) %>
#
# If you need to access the subject, from or the recipients in the view, you can do that through mailer object:
#
# You got a new note from <%= mailer.from %>!
# <%= truncate(@note.body, 25) %>
# #
# #
# = Generating URLs # = Generating URLs
...@@ -254,14 +253,15 @@ module ActionMailer #:nodoc: ...@@ -254,14 +253,15 @@ module ActionMailer #:nodoc:
# and appear last in the mime encoded message. You can also pick a different order from inside a method with # and appear last in the mime encoded message. You can also pick a different order from inside a method with
# +implicit_parts_order+. # +implicit_parts_order+.
class Base < AbstractController::Base class Base < AbstractController::Base
include AdvAttrAccessor, PartContainer, Quoting, Utils include PartContainer, Quoting
extend AdvAttrAccessor
include AbstractController::Rendering include AbstractController::Rendering
include AbstractController::LocalizedCache include AbstractController::LocalizedCache
include AbstractController::Layouts include AbstractController::Layouts
include AbstractController::Helpers include AbstractController::Helpers
helper ActionMailer::MailHelper helper ActionMailer::MailHelper
include ActionController::UrlWriter include ActionController::UrlWriter
include ActionMailer::DeprecatedBody include ActionMailer::DeprecatedBody
...@@ -289,7 +289,7 @@ class Base < AbstractController::Base ...@@ -289,7 +289,7 @@ class Base < AbstractController::Base
@@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ] @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
cattr_accessor :default_implicit_parts_order cattr_accessor :default_implicit_parts_order
@@protected_instance_variables = [] @@protected_instance_variables = %w(@parts @mail)
cattr_reader :protected_instance_variables cattr_reader :protected_instance_variables
# Specify the BCC addresses for the message # Specify the BCC addresses for the message
...@@ -454,7 +454,7 @@ def process(method_name, *args) #:nodoc: ...@@ -454,7 +454,7 @@ def process(method_name, *args) #:nodoc:
:default => method_name.humanize) :default => method_name.humanize)
# Build the mail object itself # Build the mail object itself
@mail = create_mail create_mail
end end
# Delivers a TMail::Mail object. By default, it delivers the cached mail # Delivers a TMail::Mail object. By default, it delivers the cached mail
...@@ -582,15 +582,7 @@ def create_mail ...@@ -582,15 +582,7 @@ def create_mail
m.set_content_type(real_content_type, nil, ctype_attrs) m.set_content_type(real_content_type, nil, ctype_attrs)
m.body = normalize_new_lines(@parts.first.body) m.body = normalize_new_lines(@parts.first.body)
else else
@parts.each do |p| setup_multiple_parts(m, real_content_type, ctype_attrs)
part = (TMail::Mail === p ? p : p.to_mail(self))
m.parts << part
end
if real_content_type =~ /multipart/
ctype_attrs.delete "charset"
m.set_content_type(real_content_type, nil, ctype_attrs)
end
end end
@mail = m @mail = m
......
...@@ -15,5 +15,10 @@ def block_format(text) ...@@ -15,5 +15,10 @@ def block_format(text)
formatted formatted
end end
# Access the mailer instance.
def mailer #:nodoc:
@controller
end
end end
end end
\ No newline at end of file
...@@ -4,7 +4,8 @@ module ActionMailer ...@@ -4,7 +4,8 @@ module ActionMailer
# and add them to the +parts+ list of the mailer, it is easier # and add them to the +parts+ list of the mailer, it is easier
# to use the helper methods in ActionMailer::PartContainer. # to use the helper methods in ActionMailer::PartContainer.
class Part class Part
include AdvAttrAccessor, PartContainer, Utils include PartContainer
extend AdvAttrAccessor
# Represents the body of the part, as a string. This should not be a # Represents the body of the part, as a string. This should not be a
# Hash (like ActionMailer::Base), but if you want a template to be rendered # Hash (like ActionMailer::Base), but if you want a template to be rendered
...@@ -82,16 +83,8 @@ def to_mail(defaults) ...@@ -82,16 +83,8 @@ def to_mail(defaults)
@parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain') @parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain')
@body = nil @body = nil
end end
@parts.each do |p| setup_multiple_parts(part, real_content_type, ctype_attrs)
prt = (TMail::Mail === p ? p : p.to_mail(defaults))
part.parts << prt
end
if real_content_type =~ /multipart/
ctype_attrs.delete 'charset'
part.set_content_type(real_content_type, nil, ctype_attrs)
end
end end
headers.each { |k,v| part[k] = v } headers.each { |k,v| part[k] = v }
......
...@@ -39,8 +39,24 @@ def attachment(params, &block) ...@@ -39,8 +39,24 @@ def attachment(params, &block)
end end
private private
def parse_content_type(defaults=nil) def normalize_new_lines(text) #:nodoc:
text.to_s.gsub(/\r\n?/, "\n")
end
def setup_multiple_parts(mailer, real_content_type, ctype_attrs) #:nodoc:
@parts.each do |p|
part = (TMail::Mail === p ? p : p.to_mail(self))
mailer.parts << part
end
if real_content_type =~ /multipart/
ctype_attrs.delete "charset"
mailer.set_content_type(real_content_type, nil, ctype_attrs)
end
end
def parse_content_type(defaults=nil) #:nodoc:
if content_type.blank? if content_type.blank?
return defaults ? return defaults ?
[ defaults.content_type, { 'charset' => defaults.charset } ] : [ defaults.content_type, { 'charset' => defaults.charset } ] :
......
module ActionMailer
module Utils #:nodoc:
def normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
end
end
require 'abstract_unit' require 'abstract_unit'
require 'action_mailer/adv_attr_accessor' require 'action_mailer/adv_attr_accessor'
class AdvAttrTest < Test::Unit::TestCase class AdvAttrTest < ActiveSupport::TestCase
class Person class Person
include ActionMailer::AdvAttrAccessor cattr_reader :protected_instance_variables
@@protected_instance_variables = []
extend ActionMailer::AdvAttrAccessor
adv_attr_accessor :name adv_attr_accessor :name
end end
def setup
@person = Person.new
end
def test_adv_attr def test_adv_attr
bob = Person.new assert_nil @person.name
assert_nil bob.name @person.name 'Bob'
bob.name 'Bob' assert_equal 'Bob', @person.name
assert_equal 'Bob', bob.name end
def test_adv_attr_writer
assert_nil @person.name
@person.name = 'Bob'
assert_equal 'Bob', @person.name
end
def test_raise_an_error_with_multiple_args
assert_raise(ArgumentError) { @person.name('x', 'y') }
end
assert_raise(ArgumentError) {bob.name 'x', 'y'} def test_ivar_is_added_to_protected_instnace_variables
assert Person.protected_instance_variables.include?('@name')
end end
end end
...@@ -40,13 +40,20 @@ def included_subtemplate(recipient) ...@@ -40,13 +40,20 @@ def included_subtemplate(recipient)
from "tester@example.com" from "tester@example.com"
end end
def included_old_subtemplate(recipient) def mailer_accessor(recipient)
recipients recipient recipients recipient
subject "Including another template in the one being rendered" subject "Mailer Accessor"
from "tester@example.com" from "tester@example.com"
@world = "Earth" render :inline => "Look, <%= mailer.subject %>!"
render :inline => "Hello, <%= render \"subtemplate\" %>" end
def no_instance_variable(recipient)
recipients recipient
subject "No Instance Variable"
from "tester@example.com"
render :inline => "Look, subject.nil? is <%= @subject.nil? %>!"
end end
def initialize_defaults(method_name) def initialize_defaults(method_name)
...@@ -108,6 +115,16 @@ def test_included_subtemplate ...@@ -108,6 +115,16 @@ def test_included_subtemplate
mail = RenderMailer.deliver_included_subtemplate(@recipient) mail = RenderMailer.deliver_included_subtemplate(@recipient)
assert_equal "Hey Ho, let's go!", mail.body.strip assert_equal "Hey Ho, let's go!", mail.body.strip
end end
def test_mailer_accessor
mail = RenderMailer.deliver_mailer_accessor(@recipient)
assert_equal "Look, Mailer Accessor!", mail.body.strip
end
def test_no_instance_variable
mail = RenderMailer.deliver_no_instance_variable(@recipient)
assert_equal "Look, subject.nil? is true!", mail.body.strip
end
end end
class FirstSecondHelperTest < Test::Unit::TestCase class FirstSecondHelperTest < Test::Unit::TestCase
......
...@@ -4,7 +4,7 @@ def ActiveSupport.requirable?(file) ...@@ -4,7 +4,7 @@ def ActiveSupport.requirable?(file)
$LOAD_PATH.any? { |p| Dir.glob("#{p}/#{file}.*").any? } $LOAD_PATH.any? { |p| Dir.glob("#{p}/#{file}.*").any? }
end end
[%w(builder 2.1.2), %w(i18n 0.1.3), %w(memcache-client 1.7.5), %w(tzinfo 0.3.15)].each do |lib, version| [%w(builder 2.1.2), %w(memcache-client 1.7.5), %w(tzinfo 0.3.15)].each do |lib, version|
# If the lib is not already requirable # If the lib is not already requirable
unless ActiveSupport.requirable? lib unless ActiveSupport.requirable? lib
# Try to activate a gem ~> satisfying the requested version first. # Try to activate a gem ~> satisfying the requested version first.
......
Copyright (c) 2008 The Ruby I18n team
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
h1. Ruby I18n gem
I18n and localization solution for Ruby.
For information please refer to http://rails-i18n.org
h2. Authors
* "Matt Aimonetti":http://railsontherun.com
* "Sven Fuchs":http://www.artweb-design.de
* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
* "Saimon Moore":http://saimonmoore.net
* "Stephan Soller":http://www.arkanis-development.de
h2. License
MIT License. See the included MIT-LICENCE file.
task :default => [:test]
task :test do
ruby "test/all.rb"
end
Gem::Specification.new do |s|
s.name = "i18n"
s.version = "0.1.3"
s.date = "2009-01-09"
s.summary = "Internationalization support for Ruby"
s.email = "rails-i18n@googlegroups.com"
s.homepage = "http://rails-i18n.org"
s.description = "Add Internationalization support to your Ruby application."
s.has_rdoc = false
s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
s.files = [
'i18n.gemspec',
'lib/i18n/backend/simple.rb',
'lib/i18n/exceptions.rb',
'lib/i18n.rb',
'MIT-LICENSE',
'README.textile'
]
s.test_files = [
'test/all.rb',
'test/i18n_exceptions_test.rb',
'test/i18n_test.rb',
'test/locale/en.rb',
'test/locale/en.yml',
'test/simple_backend_test.rb'
]
end
#--
# Authors:: Matt Aimonetti (http://railsontherun.com/),
# Sven Fuchs (http://www.artweb-design.de),
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
# Saimon Moore (http://saimonmoore.net),
# Stephan Soller (http://www.arkanis-development.de/)
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT
#++
module I18n
autoload :ArgumentError, 'i18n/exceptions'
module Backend
autoload :Simple, 'i18n/backend/simple'
end
@@backend = nil
@@load_path = nil
@@default_locale = :'en'
@@exception_handler = :default_exception_handler
class << self
# Returns the current backend. Defaults to +Backend::Simple+.
def backend
@@backend ||= Backend::Simple.new
end
# Sets the current backend. Used to set a custom backend.
def backend=(backend)
@@backend = backend
end
# Returns the current default locale. Defaults to :'en'
def default_locale
@@default_locale
end
# Sets the current default locale. Used to set a custom default locale.
def default_locale=(locale)
@@default_locale = locale
end
# Returns the current locale. Defaults to I18n.default_locale.
def locale
Thread.current[:locale] ||= default_locale
end
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
def locale=(locale)
Thread.current[:locale] = locale
end
# Returns an array of locales for which translations are available
def available_locales
backend.available_locales
end
# Sets the exception handler.
def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end
# 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. So for the SimpleBackend clients may
# register translation files like this:
# I18n.load_path << 'path/to/locale/en.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
# Tells the backend to reload translations. Used in situations like the
# Rails development environment. Backends can implement whatever strategy
# is useful.
def reload!
backend.reload!
end
# Translates, pluralizes and interpolates a given key using a given locale,
# scope, and default, as well as interpolation values.
#
# *LOOKUP*
#
# Translation data is organized as a nested hash using the upper-level keys
# as namespaces. <em>E.g.</em>, ActionView ships with the translation:
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
#
# Translations can be looked up at any level of this hash using the key argument
# and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
#
# Key can be either a single key or a dot-separated key (both Strings and Symbols
# work). <em>E.g.</em>, the short format can be looked up using both:
# I18n.t 'date.formats.short'
# I18n.t :'date.formats.short'
#
# Scope can be either a single key, a dot-separated key or an array of keys
# or dot-separated keys. Keys and scopes can be combined freely. So these
# examples will all look up the same short date format:
# I18n.t 'date.formats.short'
# I18n.t 'formats.short', :scope => 'date'
# I18n.t 'short', :scope => 'date.formats'
# I18n.t 'short', :scope => %w(date formats)
#
# *INTERPOLATION*
#
# Translations can contain interpolation variables which will be replaced by
# values passed to #translate as part of the options hash, with the keys matching
# the interpolation variable names.
#
# <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
# value for the key +bar+ will be interpolated into the translation:
# I18n.t :foo, :bar => 'baz' # => 'foo baz'
#
# *PLURALIZATION*
#
# Translation data can contain pluralized translations. Pluralized translations
# are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
#
# Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
# pluralization rules. Other algorithms can be supported by custom backends.
#
# This returns the singular version of a pluralized translation:
# I18n.t :foo, :count => 1 # => 'Foo'
#
# These both return the plural version of a pluralized translation:
# I18n.t :foo, :count => 0 # => 'Foos'
# I18n.t :foo, :count => 2 # => 'Foos'
#
# The <tt>:count</tt> option can be used both for pluralization and interpolation.
# <em>E.g.</em>, with the translation
# <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
# be interpolated to the pluralized translation:
# I18n.t :foo, :count => 1 # => '1 foo'
#
# *DEFAULTS*
#
# This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
# I18n.t :foo, :default => 'default'
#
# This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
# translation for <tt>:foo</tt> was found:
# I18n.t :foo, :default => :bar
#
# Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
# I18n.t :foo, :default => [:bar, 'default']
#
# <b>BULK LOOKUP</b>
#
# This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
# I18n.t [:foo, :bar]
#
# Can be used with dot-separated nested keys:
# I18n.t [:'baz.foo', :'baz.bar']
#
# Which is the same as using a scope option:
# I18n.t [:foo, :bar], :scope => :baz
def translate(key, options = {})
locale = options.delete(:locale) || I18n.locale
backend.translate(locale, key, options)
rescue I18n::ArgumentError => e
raise e if options[:raise]
send(@@exception_handler, e, locale, key, options)
end
alias :t :translate
# Localizes certain objects, such as dates and numbers to local formatting.
def localize(object, options = {})
locale = options[:locale] || I18n.locale
format = options[:format] || :default
backend.localize(locale, object, format)
end
alias :l :localize
protected
# Handles exceptions raised in the backend. All exceptions except for
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
# was caught and the option :raise is not set the handler returns an error
# message string containing the key/scope.
def default_exception_handler(exception, locale, key, options)
return exception.message if MissingTranslationData === exception
raise exception
end
# Merges the given locale, key and scope into a single array of keys.
# Splits keys that contain dots into multiple keys. Makes sure all
# keys are Symbols.
def normalize_translation_keys(locale, key, scope)
keys = [locale] + Array(scope) + [key]
keys = keys.map { |k| k.to_s.split(/\./) }
keys.flatten.map { |k| k.to_sym }
end
end
end
require 'i18n/exceptions'
module I18n
module Backend
class Simple
INTERPOLATION_RESERVED_KEYS = %w(scope default)
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
# Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details.
def load_translations(*filenames)
filenames.each { |filename| load_file(filename) }
end
# Stores translations for the given locale in memory.
# This uses a deep merge for the translations hash, so existing
# translations will be overwritten by new ones only at the deepest
# level of the hash.
def store_translations(locale, data)
merge_translations(locale, data)
end
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
return key.map { |k| translate(locale, k, options) } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
options.delete(:default)
values = options.reject { |name, value| reserved.include?(name) }
entry = lookup(locale, key, scope)
if entry.nil?
entry = default(locale, default, options)
if entry.nil?
raise(I18n::MissingTranslationData.new(locale, key, options))
end
end
entry = pluralize(locale, entry, count)
entry = interpolate(locale, entry, values)
entry
end
# Acts the same as +strftime+, but returns a localized version of the
# formatted date string. Takes a key from the date/time formats
# translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
def localize(locale, object, format = :default)
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'
# TODO only translate these if format is a String?
formats = translate(locale, :"#{type}.formats")
format = formats[format.to_sym] if formats && formats[format.to_sym]
# TODO raise exception unless format found?
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.day_names")[object.wday])
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
object.strftime(format)
end
def initialized?
@initialized ||= false
end
# Returns an array of locales for which translations are available
def available_locales
init_translations unless initialized?
translations.keys
end
def reload!
@initialized = false
@translations = nil
end
protected
def init_translations
load_translations(*I18n.load_path.flatten)
@initialized = true
end
def translations
@translations ||= {}
end
# Looks up a translation from the translations hash. Returns nil if
# eiher key is nil, or locale, scope or key do not exist as a key in the
# nested translations hash. Splits keys or scopes containing dots
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
# <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) do |result, k|
if (x = result[k.to_sym]).nil?
return nil
else
x
end
end
end
# Evaluates a default translation.
# If the given default is a String it is used literally. If it is a Symbol
# it will be translated with the given options. If it is an Array the first
# translation yielded will be returned.
#
# <em>I.e.</em>, <tt>default(locale, [:foo, 'default'])</tt> will return +default+ if
# <tt>translate(locale, :foo)</tt> does not yield a result.
def default(locale, default, options = {})
case default
when String then default
when Symbol then translate locale, default, options
when Array then default.each do |obj|
result = default(locale, obj, options.dup) and return result
end and nil
end
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
# implement more flexible or complex pluralization rules.
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
# raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash)
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= count == 1 ? :one : :other
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
entry[key]
end
# Interpolates values into a given string.
#
# interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
# # => "file test.txt opened by {{user}}"
#
# Note that you have to double escape the <tt>\\</tt> when you want to escape
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
# interpolation).
def interpolate(locale, string, values = {})
return string unless string.is_a?(String)
string.gsub(MATCH) do
escaped, pattern, key = $1, $2, $2.to_sym
if escaped
pattern
elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
raise ReservedInterpolationKey.new(pattern, string)
elsif !values.include?(key)
raise MissingInterpolationArgument.new(pattern, string)
else
values[key].to_s
end
end
end
# Loads a single translations file by delegating to #load_rb or
# #load_yml depending on the file extension and directly merges the
# data to the existing translations. Raises I18n::UnknownFileType
# for all other file extensions.
def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
data.each { |locale, d| merge_translations(locale, d) }
end
# Loads a plain Ruby translations file. eval'ing the file must yield
# a Hash containing translation data with locales as toplevel keys.
def load_rb(filename)
eval(IO.read(filename), binding, filename)
end
# Loads a YAML translations file. The data must have locales as
# toplevel keys.
def load_yml(filename)
require 'yaml' unless defined? :YAML
YAML::load(IO.read(filename))
end
# Deep merges the given translations hash with the existing translations
# for the given locale
def merge_translations(locale, data)
locale = locale.to_sym
translations[locale] ||= {}
data = deep_symbolize_keys(data)
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
translations[locale].merge!(data, &merger)
end
# Return a new hash with all keys and nested keys converted to symbols.
def deep_symbolize_keys(hash)
hash.inject({}) { |result, (key, value)|
value = deep_symbolize_keys(value) if value.is_a? Hash
result[(key.to_sym rescue key) || key] = value
result
}
end
end
end
end
module I18n
class ArgumentError < ::ArgumentError; end
class InvalidLocale < ArgumentError
attr_reader :locale
def initialize(locale)
@locale = locale
super "#{locale.inspect} is not a valid locale"
end
end
class MissingTranslationData < ArgumentError
attr_reader :locale, :key, :options
def initialize(locale, key, options)
@key, @locale, @options = key, locale, options
keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
keys << 'no key' if keys.size < 2
super "translation missing: #{keys.join(', ')}"
end
end
class InvalidPluralizationData < ArgumentError
attr_reader :entry, :count
def initialize(entry, count)
@entry, @count = entry, count
super "translation data #{entry.inspect} can not be used with :count => #{count}"
end
end
class MissingInterpolationArgument < ArgumentError
attr_reader :key, :string
def initialize(key, string)
@key, @string = key, string
super "interpolation argument #{key} missing in #{string.inspect}"
end
end
class ReservedInterpolationKey < ArgumentError
attr_reader :key, :string
def initialize(key, string)
@key, @string = key, string
super "reserved key #{key.inspect} used in #{string.inspect}"
end
end
class UnknownFileType < ArgumentError
attr_reader :type, :filename
def initialize(type, filename)
@type, @filename = type, filename
super "can not load translations from #{filename}, the file type #{type} is not known"
end
end
end
dir = File.dirname(__FILE__)
require dir + '/i18n_test.rb'
require dir + '/simple_backend_test.rb'
require dir + '/i18n_exceptions_test.rb'
# *require* dir + '/custom_backend_test.rb'
\ No newline at end of file
$:.unshift "lib"
require 'rubygems'
require 'test/unit'
require 'i18n'
require 'active_support'
class I18nExceptionsTest < Test::Unit::TestCase
def test_invalid_locale_stores_locale
force_invalid_locale
rescue I18n::ArgumentError => e
assert_nil e.locale
end
def test_invalid_locale_message
force_invalid_locale
rescue I18n::ArgumentError => e
assert_equal 'nil is not a valid locale', e.message
end
def test_missing_translation_data_stores_locale_key_and_options
force_missing_translation_data
rescue I18n::ArgumentError => e
options = {:scope => :bar}
assert_equal 'de', e.locale
assert_equal :foo, e.key
assert_equal options, e.options
end
def test_missing_translation_data_message
force_missing_translation_data
rescue I18n::ArgumentError => e
assert_equal 'translation missing: de, bar, foo', e.message
end
def test_invalid_pluralization_data_stores_entry_and_count
force_invalid_pluralization_data
rescue I18n::ArgumentError => e
assert_equal [:bar], e.entry
assert_equal 1, e.count
end
def test_invalid_pluralization_data_message
force_invalid_pluralization_data
rescue I18n::ArgumentError => e
assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
end
def test_missing_interpolation_argument_stores_key_and_string
force_missing_interpolation_argument
rescue I18n::ArgumentError => e
assert_equal 'bar', e.key
assert_equal "{{bar}}", e.string
end
def test_missing_interpolation_argument_message
force_missing_interpolation_argument
rescue I18n::ArgumentError => e
assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
end
def test_reserved_interpolation_key_stores_key_and_string
force_reserved_interpolation_key
rescue I18n::ArgumentError => e
assert_equal 'scope', e.key
assert_equal "{{scope}}", e.string
end
def test_reserved_interpolation_key_message
force_reserved_interpolation_key
rescue I18n::ArgumentError => e
assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
end
private
def force_invalid_locale
I18n.backend.translate nil, :foo
end
def force_missing_translation_data
I18n.backend.store_translations 'de', :bar => nil
I18n.backend.translate 'de', :foo, :scope => :bar
end
def force_invalid_pluralization_data
I18n.backend.store_translations 'de', :foo => [:bar]
I18n.backend.translate 'de', :foo, :count => 1
end
def force_missing_interpolation_argument
I18n.backend.store_translations 'de', :foo => "{{bar}}"
I18n.backend.translate 'de', :foo, :baz => 'baz'
end
def force_reserved_interpolation_key
I18n.backend.store_translations 'de', :foo => "{{scope}}"
I18n.backend.translate 'de', :foo, :baz => 'baz'
end
end
\ No newline at end of file
$:.unshift "lib"
require 'rubygems'
require 'test/unit'
require 'i18n'
require 'active_support'
class I18nTest < Test::Unit::TestCase
def setup
I18n.backend.store_translations :'en', {
:currency => {
:format => {
:separator => '.',
:delimiter => ',',
}
}
}
end
def test_uses_simple_backend_set_by_default
assert I18n.backend.is_a?(I18n::Backend::Simple)
end
def test_can_set_backend
assert_nothing_raised{ I18n.backend = self }
assert_equal self, I18n.backend
I18n.backend = I18n::Backend::Simple.new
end
def test_uses_en_us_as_default_locale_by_default
assert_equal 'en', I18n.default_locale
end
def test_can_set_default_locale
assert_nothing_raised{ I18n.default_locale = 'de' }
assert_equal 'de', I18n.default_locale
I18n.default_locale = 'en'
end
def test_uses_default_locale_as_locale_by_default
assert_equal I18n.default_locale, I18n.locale
end
def test_can_set_locale_to_thread_current
assert_nothing_raised{ I18n.locale = 'de' }
assert_equal 'de', I18n.locale
assert_equal 'de', Thread.current[:locale]
I18n.locale = 'en'
end
def test_can_set_exception_handler
assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
I18n.exception_handler = :default_exception_handler # revert it
end
def test_uses_custom_exception_handler
I18n.exception_handler = :custom_exception_handler
I18n.expects(:custom_exception_handler)
I18n.translate :bogus
I18n.exception_handler = :default_exception_handler # revert it
end
def test_delegates_translate_to_backend
I18n.backend.expects(:translate).with 'de', :foo, {}
I18n.translate :foo, :locale => 'de'
end
def test_delegates_localize_to_backend
I18n.backend.expects(:localize).with 'de', :whatever, :default
I18n.localize :whatever, :locale => 'de'
end
def test_translate_given_no_locale_uses_i18n_locale
I18n.backend.expects(:translate).with 'en', :foo, {}
I18n.translate :foo
end
def test_translate_on_nested_symbol_keys_works
assert_equal ".", I18n.t(:'currency.format.separator')
end
def test_translate_with_nested_string_keys_works
assert_equal ".", I18n.t('currency.format.separator')
end
def test_translate_with_array_as_scope_works
assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
end
def test_translate_with_array_containing_dot_separated_strings_as_scope_works
assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
end
def test_translate_with_key_array_and_dot_separated_scope_works
assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
end
def test_translate_with_dot_separated_key_array_and_scope_works
assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
end
def test_translate_with_options_using_scope_works
I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
locale.t :precision
end
end
# def test_translate_given_no_args_raises_missing_translation_data
# assert_equal "translation missing: en, no key", I18n.t
# end
def test_translate_given_a_bogus_key_raises_missing_translation_data
assert_equal "translation missing: en, bogus", I18n.t(:bogus)
end
def test_localize_nil_raises_argument_error
assert_raise(I18n::ArgumentError) { I18n.l nil }
end
def test_localize_object_raises_argument_error
assert_raise(I18n::ArgumentError) { I18n.l Object.new }
end
end
{:'en-Ruby' => {:foo => {:bar => "baz"}}}
\ No newline at end of file
...@@ -13,38 +13,39 @@ class WhinyNilTest < Test::Unit::TestCase ...@@ -13,38 +13,39 @@ class WhinyNilTest < Test::Unit::TestCase
def test_unchanged def test_unchanged
nil.method_thats_not_in_whiners nil.method_thats_not_in_whiners
rescue NoMethodError => nme rescue NoMethodError => nme
assert(nme.message =~ /nil:NilClass/) assert_match(/nil:NilClass/, nme.message)
end end
def test_active_record def test_active_record
nil.save! nil.save!
rescue NoMethodError => nme rescue NoMethodError => nme
assert(!(nme.message =~ /nil:NilClass/)) assert_no_match(/nil:NilClass/, nme.message)
assert_match(/nil\.save!/, nme.message) assert_match(/nil\.save!/, nme.message)
end end
def test_array def test_array
nil.each nil.each
rescue NoMethodError => nme rescue NoMethodError => nme
assert(!(nme.message =~ /nil:NilClass/)) assert_no_match(/nil:NilClass/, nme.message)
assert_match(/nil\.each/, nme.message) assert_match(/nil\.each/, nme.message)
end end
def test_id def test_id
nil.id nil.id
rescue RuntimeError => nme rescue RuntimeError => nme
assert(!(nme.message =~ /nil:NilClass/)) assert_no_match(/nil:NilClass/, nme.message)
end end
def test_no_to_ary_coercion def test_no_to_ary_coercion
nil.to_ary nil.to_ary
rescue NoMethodError => nme rescue NoMethodError => nme
assert(nme.message =~ /nil:NilClass/) assert_no_match(/nil:NilClass/, nme.message)
assert_match(/nil\.to_ary/, nme.message)
end end
def test_no_to_str_coercion def test_no_to_str_coercion
nil.to_str nil.to_str
rescue NoMethodError => nme rescue NoMethodError => nme
assert(nme.message =~ /nil:NilClass/) assert_match(/nil:NilClass/, nme.message)
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册