i18n 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of i18n might be problematic. Click here for more details.

File without changes
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'mocha'
4
4
  gem 'test_declarative'
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'activesupport', '~> 2.3'
4
4
  gem 'sqlite3-ruby'
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'activesupport', '~> 3.0.0'
4
4
  gem 'sqlite3-ruby'
@@ -131,7 +131,7 @@ module I18n
131
131
  # called and passed the key and options.
132
132
  #
133
133
  # E.g. assuming the key <tt>:salutation</tt> resolves to:
134
- # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
134
+ # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
135
135
  #
136
136
  # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
137
137
  #
@@ -56,13 +56,14 @@ module I18n
56
56
  end
57
57
 
58
58
  # format = resolve(locale, object, format, options)
59
- format = format.to_s.gsub(/%[aAbBp]/) do |match|
59
+ format = format.to_s.gsub(/%[aAbBpP]/) do |match|
60
60
  case match
61
61
  when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
62
62
  when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
63
63
  when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
64
64
  when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
65
- when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
65
+ when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
66
+ when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
66
67
  end
67
68
  end
68
69
 
@@ -77,7 +77,7 @@ module I18n
77
77
  end
78
78
 
79
79
  def missing_key(key)
80
- "raise(MissingInterpolationArgument.new(#{key}, {}, self))"
80
+ "I18n.config.missing_interpolation_argument_handler.call(#{key}, v, self)"
81
81
  end
82
82
 
83
83
  def reserved_key(key)
@@ -65,6 +65,29 @@ module I18n
65
65
  @@exception_handler = exception_handler
66
66
  end
67
67
 
68
+ # Returns the current handler for situations when interpolation argument
69
+ # is missing. MissingInterpolationArgument will be raised by default.
70
+ def missing_interpolation_argument_handler
71
+ @@missing_interpolation_argument_handler ||= lambda do |missing_key, provided_hash, string|
72
+ raise MissingInterpolationArgument.new(missing_key, provided_hash, string)
73
+ end
74
+ end
75
+
76
+ # Sets the missing interpolation argument handler. It can be any
77
+ # object that responds to #call. The arguments that will be passed to #call
78
+ # are the same as for MissingInterpolationArgument initializer. Use +Proc.new+
79
+ # if you don't care about arity.
80
+ #
81
+ # == Example:
82
+ # You can supress raising an exception and return string instead:
83
+ #
84
+ # I18n.config.missing_interpolation_argument_handler = Proc.new do |key|
85
+ # "#{key} is missing"
86
+ # end
87
+ def missing_interpolation_argument_handler=(exception_handler)
88
+ @@missing_interpolation_argument_handler = exception_handler
89
+ end
90
+
68
91
  # Allow clients to register paths providing translation data sources. The
69
92
  # backend defines acceptable sources.
70
93
  #
@@ -9,6 +9,8 @@ module I18n
9
9
  )
10
10
 
11
11
  class << self
12
+ # Return String or raises MissingInterpolationArgument exception.
13
+ # Missing argument's logic is handled by I18n.config.missing_interpolation_argument_handler.
12
14
  def interpolate(string, values)
13
15
  raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ RESERVED_KEYS_PATTERN
14
16
  raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash)
@@ -21,7 +23,11 @@ module I18n
21
23
  '%'
22
24
  else
23
25
  key = ($1 || $2).to_sym
24
- value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(key, values, string))
26
+ value = if values.key?(key)
27
+ values[key]
28
+ else
29
+ config.missing_interpolation_argument_handler.call(key, values, string)
30
+ end
25
31
  value = value.call(values) if value.respond_to?(:call)
26
32
  $3 ? sprintf("%#{$3}", value) : value
27
33
  end
@@ -43,8 +43,13 @@ module I18n
43
43
  end
44
44
 
45
45
  test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator" do
46
- assert_equal 'am', I18n.l(@datetime, :format => '%p', :locale => :de)
47
- assert_equal 'pm', I18n.l(@other_datetime, :format => '%p', :locale => :de)
46
+ assert_equal 'AM', I18n.l(@datetime, :format => '%p', :locale => :de)
47
+ assert_equal 'PM', I18n.l(@other_datetime, :format => '%p', :locale => :de)
48
+ end
49
+
50
+ test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator in downcase" do
51
+ assert_equal 'am', I18n.l(@datetime, :format => '%P', :locale => :de)
52
+ assert_equal 'pm', I18n.l(@other_datetime, :format => '%P', :locale => :de)
48
53
  end
49
54
 
50
55
  test "localize DateTime: given an unknown format it does not fail" do
@@ -43,8 +43,13 @@ module I18n
43
43
  end
44
44
 
45
45
  test "localize Time: given a meridian indicator format it returns the correct meridian indicator" do
46
- assert_equal 'am', I18n.l(@time, :format => '%p', :locale => :de)
47
- assert_equal 'pm', I18n.l(@other_time, :format => '%p', :locale => :de)
46
+ assert_equal 'AM', I18n.l(@time, :format => '%p', :locale => :de)
47
+ assert_equal 'PM', I18n.l(@other_time, :format => '%p', :locale => :de)
48
+ end
49
+
50
+ test "localize Time: given a meridian indicator format it returns the correct meridian indicator in upcase" do
51
+ assert_equal 'am', I18n.l(@time, :format => '%P', :locale => :de)
52
+ assert_equal 'pm', I18n.l(@other_time, :format => '%P', :locale => :de)
48
53
  end
49
54
 
50
55
  test "localize Time: given an unknown format it does not fail" do
@@ -1,3 +1,3 @@
1
1
  module I18n
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.5"
3
3
  end
@@ -27,4 +27,9 @@ class I18nBackendExceptionsTest < Test::Unit::TestCase
27
27
  end
28
28
  assert_equal "translation missing: en.time.formats.foo", exception.message
29
29
  end
30
+
31
+ test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do
32
+ exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
33
+ assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
34
+ end
30
35
  end
@@ -76,6 +76,23 @@ class InterpolationCompilerTest < Test::Unit::TestCase
76
76
  assert_equal '\";eval("a")', compile_and_interpolate('\";eval("a")%{a}', :a => '' )
77
77
  assert_equal "\na", compile_and_interpolate("\n%{a}", :a => 'a')
78
78
  end
79
+
80
+ def test_raises_exception_when_argument_is_missing
81
+ assert_raise(I18n::MissingInterpolationArgument) do
82
+ compile_and_interpolate('%{first} %{last}', :first => 'first')
83
+ end
84
+ end
85
+
86
+ def test_custom_missing_interpolation_argument_handler
87
+ old_handler = I18n.config.missing_interpolation_argument_handler
88
+ I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
89
+ "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
90
+ end
91
+ assert_equal %|first missing key is last, values are {:first=>"first"}, given string is '%{first} %{last}'|,
92
+ compile_and_interpolate('%{first} %{last}', :first => 'first')
93
+ ensure
94
+ I18n.config.missing_interpolation_argument_handler = old_handler
95
+ end
79
96
  end
80
97
 
81
98
  class I18nBackendInterpolationCompilerTest < Test::Unit::TestCase
@@ -59,3 +59,21 @@ class I18nInterpolateTest < Test::Unit::TestCase
59
59
  assert_equal "foo 1.000000", I18n.interpolate("%{name} %<num>f", :name => "foo", :num => 1.0)
60
60
  end
61
61
  end
62
+
63
+ class I18nMissingInterpolationCustomHandlerTest < Test::Unit::TestCase
64
+ def setup
65
+ @old_handler = I18n.config.missing_interpolation_argument_handler
66
+ I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
67
+ "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
68
+ end
69
+ end
70
+
71
+ def teardown
72
+ I18n.config.missing_interpolation_argument_handler = @old_handler
73
+ end
74
+
75
+ test "String interpolation can use custom missing interpolation handler" do
76
+ assert_equal %|Masao missing key is last, values are {:first=>"Masao"}, given string is '%{first} %{last}'|,
77
+ I18n.interpolate("%{first} %{last}", :first => 'Masao')
78
+ end
79
+ end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.6.5
4
5
  prerelease:
5
- version: 0.6.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sven Fuchs
@@ -13,72 +13,72 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-02-27 00:00:00.000000000 Z
16
+ date: 2013-08-13 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
- version_requirements: !ruby/object:Gem::Requirement
19
+ name: activesupport
20
+ requirement: !ruby/object:Gem::Requirement
21
+ none: false
20
22
  requirements:
21
23
  - - ! '>='
22
24
  - !ruby/object:Gem::Version
23
25
  version: 3.0.0
24
- none: false
25
- name: activesupport
26
26
  type: :development
27
27
  prerelease: false
28
- requirement: !ruby/object:Gem::Requirement
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ none: false
29
30
  requirements:
30
31
  - - ! '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: 3.0.0
33
- none: false
34
34
  - !ruby/object:Gem::Dependency
35
- version_requirements: !ruby/object:Gem::Requirement
35
+ name: sqlite3
36
+ requirement: !ruby/object:Gem::Requirement
37
+ none: false
36
38
  requirements:
37
39
  - - ! '>='
38
40
  - !ruby/object:Gem::Version
39
41
  version: '0'
40
- none: false
41
- name: sqlite3
42
42
  type: :development
43
43
  prerelease: false
44
- requirement: !ruby/object:Gem::Requirement
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ none: false
45
46
  requirements:
46
47
  - - ! '>='
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
- none: false
50
50
  - !ruby/object:Gem::Dependency
51
- version_requirements: !ruby/object:Gem::Requirement
51
+ name: mocha
52
+ requirement: !ruby/object:Gem::Requirement
53
+ none: false
52
54
  requirements:
53
55
  - - ! '>='
54
56
  - !ruby/object:Gem::Version
55
57
  version: '0'
56
- none: false
57
- name: mocha
58
58
  type: :development
59
59
  prerelease: false
60
- requirement: !ruby/object:Gem::Requirement
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ none: false
61
62
  requirements:
62
63
  - - ! '>='
63
64
  - !ruby/object:Gem::Version
64
65
  version: '0'
65
- none: false
66
66
  - !ruby/object:Gem::Dependency
67
- version_requirements: !ruby/object:Gem::Requirement
67
+ name: test_declarative
68
+ requirement: !ruby/object:Gem::Requirement
69
+ none: false
68
70
  requirements:
69
71
  - - ! '>='
70
72
  - !ruby/object:Gem::Version
71
73
  version: '0'
72
- none: false
73
- name: test_declarative
74
74
  type: :development
75
75
  prerelease: false
76
- requirement: !ruby/object:Gem::Requirement
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ none: false
77
78
  requirements:
78
79
  - - ! '>='
79
80
  - !ruby/object:Gem::Version
80
81
  version: '0'
81
- none: false
82
82
  description: New wave Internationalization support for Ruby.
83
83
  email: rails-i18n@googlegroups.com
84
84
  executables: []
@@ -188,17 +188,17 @@ rdoc_options: []
188
188
  require_paths:
189
189
  - lib
190
190
  required_ruby_version: !ruby/object:Gem::Requirement
191
+ none: false
191
192
  requirements:
192
193
  - - ! '>='
193
194
  - !ruby/object:Gem::Version
194
195
  version: '0'
195
- none: false
196
196
  required_rubygems_version: !ruby/object:Gem::Requirement
197
+ none: false
197
198
  requirements:
198
199
  - - ! '>='
199
200
  - !ruby/object:Gem::Version
200
201
  version: 1.3.5
201
- none: false
202
202
  requirements: []
203
203
  rubyforge_project: ! '[none]'
204
204
  rubygems_version: 1.8.23