[DRE-commits] [ruby-i18n] 01/08: Imported Upstream version 0.6.5
Jonas Genannt
jonas at brachium-system.net
Wed Nov 27 18:46:14 UTC 2013
This is an automated email from the git hooks/post-receive script.
hggh-guest pushed a commit to branch master
in repository ruby-i18n.
commit 27984423a90a89ae0d7f4cd349e6935df4fd6768
Author: Jonas Genannt <jonas at brachium-system.net>
Date: Wed Nov 27 19:24:04 2013 +0100
Imported Upstream version 0.6.5
---
MIT-LICENSE | 0
ci/Gemfile.no-rails | 2 +-
ci/Gemfile.rails-2.3.x | 2 +-
ci/Gemfile.rails-3.x | 2 +-
lib/i18n.rb | 2 +-
lib/i18n/backend/base.rb | 5 +--
lib/i18n/backend/interpolation_compiler.rb | 2 +-
lib/i18n/config.rb | 23 ++++++++++++++
lib/i18n/interpolate/ruby.rb | 8 ++++-
lib/i18n/tests/localization/date_time.rb | 9 ++++--
lib/i18n/tests/localization/time.rb | 9 ++++--
lib/i18n/version.rb | 2 +-
metadata.yml | 48 ++++++++++++++---------------
test/backend/exceptions_test.rb | 5 +++
test/backend/interpolation_compiler_test.rb | 17 ++++++++++
test/i18n/interpolate_test.rb | 18 +++++++++++
16 files changed, 117 insertions(+), 37 deletions(-)
diff --git a/MIT-LICENSE b/MIT-LICENSE
old mode 100755
new mode 100644
diff --git a/ci/Gemfile.no-rails b/ci/Gemfile.no-rails
index d4300c9..449613e 100644
--- a/ci/Gemfile.no-rails
+++ b/ci/Gemfile.no-rails
@@ -1,4 +1,4 @@
-source :rubygems
+source 'https://rubygems.org'
gem 'mocha'
gem 'test_declarative'
diff --git a/ci/Gemfile.rails-2.3.x b/ci/Gemfile.rails-2.3.x
index 8f8afbe..3227b3a 100644
--- a/ci/Gemfile.rails-2.3.x
+++ b/ci/Gemfile.rails-2.3.x
@@ -1,4 +1,4 @@
-source :rubygems
+source 'https://rubygems.org'
gem 'activesupport', '~> 2.3'
gem 'sqlite3-ruby'
diff --git a/ci/Gemfile.rails-3.x b/ci/Gemfile.rails-3.x
index 782c844..d0947e5 100644
--- a/ci/Gemfile.rails-3.x
+++ b/ci/Gemfile.rails-3.x
@@ -1,4 +1,4 @@
-source :rubygems
+source 'https://rubygems.org'
gem 'activesupport', '~> 3.0.0'
gem 'sqlite3-ruby'
diff --git a/lib/i18n.rb b/lib/i18n.rb
index 418bd0f..a32e0cb 100755
--- a/lib/i18n.rb
+++ b/lib/i18n.rb
@@ -131,7 +131,7 @@ module I18n
# called and passed the key and options.
#
# E.g. assuming the key <tt>:salutation</tt> resolves to:
- # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
+ # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
#
# Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
#
diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb
index b866072..ad73a08 100644
--- a/lib/i18n/backend/base.rb
+++ b/lib/i18n/backend/base.rb
@@ -56,13 +56,14 @@ module I18n
end
# format = resolve(locale, object, format, options)
- format = format.to_s.gsub(/%[aAbBp]/) do |match|
+ format = format.to_s.gsub(/%[aAbBpP]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
- when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
+ when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
+ when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
end
end
diff --git a/lib/i18n/backend/interpolation_compiler.rb b/lib/i18n/backend/interpolation_compiler.rb
index 2c7ce21..c544070 100644
--- a/lib/i18n/backend/interpolation_compiler.rb
+++ b/lib/i18n/backend/interpolation_compiler.rb
@@ -77,7 +77,7 @@ module I18n
end
def missing_key(key)
- "raise(MissingInterpolationArgument.new(#{key}, {}, self))"
+ "I18n.config.missing_interpolation_argument_handler.call(#{key}, v, self)"
end
def reserved_key(key)
diff --git a/lib/i18n/config.rb b/lib/i18n/config.rb
index 5fe05f7..b64896f 100644
--- a/lib/i18n/config.rb
+++ b/lib/i18n/config.rb
@@ -65,6 +65,29 @@ module I18n
@@exception_handler = exception_handler
end
+ # Returns the current handler for situations when interpolation argument
+ # is missing. MissingInterpolationArgument will be raised by default.
+ def missing_interpolation_argument_handler
+ @@missing_interpolation_argument_handler ||= lambda do |missing_key, provided_hash, string|
+ raise MissingInterpolationArgument.new(missing_key, provided_hash, string)
+ end
+ end
+
+ # Sets the missing interpolation argument handler. It can be any
+ # object that responds to #call. The arguments that will be passed to #call
+ # are the same as for MissingInterpolationArgument initializer. Use +Proc.new+
+ # if you don't care about arity.
+ #
+ # == Example:
+ # You can supress raising an exception and return string instead:
+ #
+ # I18n.config.missing_interpolation_argument_handler = Proc.new do |key|
+ # "#{key} is missing"
+ # end
+ def missing_interpolation_argument_handler=(exception_handler)
+ @@missing_interpolation_argument_handler = exception_handler
+ end
+
# Allow clients to register paths providing translation data sources. The
# backend defines acceptable sources.
#
diff --git a/lib/i18n/interpolate/ruby.rb b/lib/i18n/interpolate/ruby.rb
index 3ad14a1..442677f 100644
--- a/lib/i18n/interpolate/ruby.rb
+++ b/lib/i18n/interpolate/ruby.rb
@@ -9,6 +9,8 @@ module I18n
)
class << self
+ # Return String or raises MissingInterpolationArgument exception.
+ # Missing argument's logic is handled by I18n.config.missing_interpolation_argument_handler.
def interpolate(string, values)
raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ RESERVED_KEYS_PATTERN
raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash)
@@ -21,7 +23,11 @@ module I18n
'%'
else
key = ($1 || $2).to_sym
- value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(key, values, string))
+ value = if values.key?(key)
+ values[key]
+ else
+ config.missing_interpolation_argument_handler.call(key, values, string)
+ end
value = value.call(values) if value.respond_to?(:call)
$3 ? sprintf("%#{$3}", value) : value
end
diff --git a/lib/i18n/tests/localization/date_time.rb b/lib/i18n/tests/localization/date_time.rb
index a926d1c..7a30bff 100644
--- a/lib/i18n/tests/localization/date_time.rb
+++ b/lib/i18n/tests/localization/date_time.rb
@@ -43,8 +43,13 @@ module I18n
end
test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator" do
- assert_equal 'am', I18n.l(@datetime, :format => '%p', :locale => :de)
- assert_equal 'pm', I18n.l(@other_datetime, :format => '%p', :locale => :de)
+ assert_equal 'AM', I18n.l(@datetime, :format => '%p', :locale => :de)
+ assert_equal 'PM', I18n.l(@other_datetime, :format => '%p', :locale => :de)
+ end
+
+ test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator in downcase" do
+ assert_equal 'am', I18n.l(@datetime, :format => '%P', :locale => :de)
+ assert_equal 'pm', I18n.l(@other_datetime, :format => '%P', :locale => :de)
end
test "localize DateTime: given an unknown format it does not fail" do
diff --git a/lib/i18n/tests/localization/time.rb b/lib/i18n/tests/localization/time.rb
index 599f21f..8bbba43 100644
--- a/lib/i18n/tests/localization/time.rb
+++ b/lib/i18n/tests/localization/time.rb
@@ -43,8 +43,13 @@ module I18n
end
test "localize Time: given a meridian indicator format it returns the correct meridian indicator" do
- assert_equal 'am', I18n.l(@time, :format => '%p', :locale => :de)
- assert_equal 'pm', I18n.l(@other_time, :format => '%p', :locale => :de)
+ assert_equal 'AM', I18n.l(@time, :format => '%p', :locale => :de)
+ assert_equal 'PM', I18n.l(@other_time, :format => '%p', :locale => :de)
+ end
+
+ test "localize Time: given a meridian indicator format it returns the correct meridian indicator in upcase" do
+ assert_equal 'am', I18n.l(@time, :format => '%P', :locale => :de)
+ assert_equal 'pm', I18n.l(@other_time, :format => '%P', :locale => :de)
end
test "localize Time: given an unknown format it does not fail" do
diff --git a/lib/i18n/version.rb b/lib/i18n/version.rb
index 9f1bfda..c5e4154 100644
--- a/lib/i18n/version.rb
+++ b/lib/i18n/version.rb
@@ -1,3 +1,3 @@
module I18n
- VERSION = "0.6.4"
+ VERSION = "0.6.5"
end
diff --git a/metadata.yml b/metadata.yml
index 83075c8..6dd4810 100644
--- a/metadata.yml
+++ b/metadata.yml
@@ -1,8 +1,8 @@
--- !ruby/object:Gem::Specification
name: i18n
version: !ruby/object:Gem::Version
+ version: 0.6.5
prerelease:
- version: 0.6.4
platform: ruby
authors:
- Sven Fuchs
@@ -13,72 +13,72 @@ authors:
autorequire:
bindir: bin
cert_chain: []
-date: 2013-02-27 00:00:00.000000000 Z
+date: 2013-08-13 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
- version_requirements: !ruby/object:Gem::Requirement
+ name: activesupport
+ requirement: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 3.0.0
- none: false
- name: activesupport
type: :development
prerelease: false
- requirement: !ruby/object:Gem::Requirement
+ version_requirements: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 3.0.0
- none: false
- !ruby/object:Gem::Dependency
- version_requirements: !ruby/object:Gem::Requirement
+ name: sqlite3
+ requirement: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
- name: sqlite3
type: :development
prerelease: false
- requirement: !ruby/object:Gem::Requirement
+ version_requirements: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
- !ruby/object:Gem::Dependency
- version_requirements: !ruby/object:Gem::Requirement
+ name: mocha
+ requirement: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
- name: mocha
type: :development
prerelease: false
- requirement: !ruby/object:Gem::Requirement
+ version_requirements: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
- !ruby/object:Gem::Dependency
- version_requirements: !ruby/object:Gem::Requirement
+ name: test_declarative
+ requirement: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
- name: test_declarative
type: :development
prerelease: false
- requirement: !ruby/object:Gem::Requirement
+ version_requirements: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
description: New wave Internationalization support for Ruby.
email: rails-i18n at googlegroups.com
executables: []
@@ -188,17 +188,17 @@ rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- none: false
required_rubygems_version: !ruby/object:Gem::Requirement
+ none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 1.3.5
- none: false
requirements: []
rubyforge_project: ! '[none]'
rubygems_version: 1.8.23
diff --git a/test/backend/exceptions_test.rb b/test/backend/exceptions_test.rb
index 48fbc43..fc0f557 100644
--- a/test/backend/exceptions_test.rb
+++ b/test/backend/exceptions_test.rb
@@ -27,4 +27,9 @@ class I18nBackendExceptionsTest < Test::Unit::TestCase
end
assert_equal "translation missing: en.time.formats.foo", exception.message
end
+
+ test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do
+ exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
+ assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
+ end
end
diff --git a/test/backend/interpolation_compiler_test.rb b/test/backend/interpolation_compiler_test.rb
index 4af4271..f4d287a 100644
--- a/test/backend/interpolation_compiler_test.rb
+++ b/test/backend/interpolation_compiler_test.rb
@@ -76,6 +76,23 @@ class InterpolationCompilerTest < Test::Unit::TestCase
assert_equal '\";eval("a")', compile_and_interpolate('\";eval("a")%{a}', :a => '' )
assert_equal "\na", compile_and_interpolate("\n%{a}", :a => 'a')
end
+
+ def test_raises_exception_when_argument_is_missing
+ assert_raise(I18n::MissingInterpolationArgument) do
+ compile_and_interpolate('%{first} %{last}', :first => 'first')
+ end
+ end
+
+ def test_custom_missing_interpolation_argument_handler
+ old_handler = I18n.config.missing_interpolation_argument_handler
+ I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
+ "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
+ end
+ assert_equal %|first missing key is last, values are {:first=>"first"}, given string is '%{first} %{last}'|,
+ compile_and_interpolate('%{first} %{last}', :first => 'first')
+ ensure
+ I18n.config.missing_interpolation_argument_handler = old_handler
+ end
end
class I18nBackendInterpolationCompilerTest < Test::Unit::TestCase
diff --git a/test/i18n/interpolate_test.rb b/test/i18n/interpolate_test.rb
index ee8849b..7041b32 100644
--- a/test/i18n/interpolate_test.rb
+++ b/test/i18n/interpolate_test.rb
@@ -59,3 +59,21 @@ class I18nInterpolateTest < Test::Unit::TestCase
assert_equal "foo 1.000000", I18n.interpolate("%{name} %<num>f", :name => "foo", :num => 1.0)
end
end
+
+class I18nMissingInterpolationCustomHandlerTest < Test::Unit::TestCase
+ def setup
+ @old_handler = I18n.config.missing_interpolation_argument_handler
+ I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
+ "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
+ end
+ end
+
+ def teardown
+ I18n.config.missing_interpolation_argument_handler = @old_handler
+ end
+
+ test "String interpolation can use custom missing interpolation handler" do
+ assert_equal %|Masao missing key is last, values are {:first=>"Masao"}, given string is '%{first} %{last}'|,
+ I18n.interpolate("%{first} %{last}", :first => 'Masao')
+ end
+end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-i18n.git
More information about the Pkg-ruby-extras-commits
mailing list