i18n 0.5.0beta2 → 0.5.0beta3
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.
data/ci/Gemfile.no-rails.lock
CHANGED
data/ci/Gemfile.rails-2.3.x.lock
CHANGED
data/ci/Gemfile.rails-3.x.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activesupport (3.0.
|
4
|
+
activesupport (3.0.3)
|
5
5
|
ffi (0.6.3)
|
6
6
|
rake (>= 0.8.7)
|
7
7
|
mocha (0.9.9)
|
@@ -21,6 +21,3 @@ DEPENDENCIES
|
|
21
21
|
rufus-tokyo
|
22
22
|
sqlite3-ruby
|
23
23
|
test_declarative
|
24
|
-
|
25
|
-
METADATA
|
26
|
-
version: 1.0.6
|
@@ -0,0 +1,96 @@
|
|
1
|
+
=begin
|
2
|
+
heavily based on Masao Mutoh's gettext String interpolation extension
|
3
|
+
http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
|
4
|
+
Copyright (C) 2005-2009 Masao Mutoh
|
5
|
+
You may redistribute it and/or modify it under the same license terms as Ruby.
|
6
|
+
=end
|
7
|
+
|
8
|
+
begin
|
9
|
+
raise ArgumentError if ("a %{x}" % {:x=>'b'}) != 'a b'
|
10
|
+
rescue ArgumentError
|
11
|
+
# KeyError is raised by String#% when the string contains a named placeholder
|
12
|
+
# that is not contained in the given arguments hash. Ruby 1.9 includes and
|
13
|
+
# raises this exception natively. We define it to mimic Ruby 1.9's behaviour
|
14
|
+
# in Ruby 1.8.x
|
15
|
+
class KeyError < IndexError
|
16
|
+
def initialize(message = nil)
|
17
|
+
super(message || "key not found")
|
18
|
+
end
|
19
|
+
end unless defined?(KeyError)
|
20
|
+
|
21
|
+
# Extension for String class. This feature is included in Ruby 1.9 or later but not occur TypeError.
|
22
|
+
#
|
23
|
+
# String#% method which accept "named argument". The translator can know
|
24
|
+
# the meaning of the msgids using "named argument" instead of %s/%d style.
|
25
|
+
class String
|
26
|
+
# For older ruby versions, such as ruby-1.8.5
|
27
|
+
alias :bytesize :size unless instance_methods.find {|m| m.to_s == 'bytesize'}
|
28
|
+
alias :interpolate_without_ruby_19_syntax :% # :nodoc:
|
29
|
+
|
30
|
+
INTERPOLATION_PATTERN = Regexp.union(
|
31
|
+
/%\{(\w+)\}/, # matches placeholders like "%{foo}"
|
32
|
+
/%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
|
33
|
+
)
|
34
|
+
|
35
|
+
INTERPOLATION_PATTERN_WITH_ESCAPE = Regexp.union(
|
36
|
+
/%%/,
|
37
|
+
INTERPOLATION_PATTERN
|
38
|
+
)
|
39
|
+
|
40
|
+
# % uses self (i.e. the String) as a format specification and returns the
|
41
|
+
# result of applying it to the given arguments. In other words it interpolates
|
42
|
+
# the given arguments to the string according to the formats the string
|
43
|
+
# defines.
|
44
|
+
#
|
45
|
+
# There are three ways to use it:
|
46
|
+
#
|
47
|
+
# * Using a single argument or Array of arguments.
|
48
|
+
#
|
49
|
+
# This is the default behaviour of the String class. See Kernel#sprintf for
|
50
|
+
# more details about the format string.
|
51
|
+
#
|
52
|
+
# Example:
|
53
|
+
#
|
54
|
+
# "%d %s" % [1, "message"]
|
55
|
+
# # => "1 message"
|
56
|
+
#
|
57
|
+
# * Using a Hash as an argument and unformatted, named placeholders.
|
58
|
+
#
|
59
|
+
# When you pass a Hash as an argument and specify placeholders with %{foo}
|
60
|
+
# it will interpret the hash values as named arguments.
|
61
|
+
#
|
62
|
+
# Example:
|
63
|
+
#
|
64
|
+
# "%{firstname}, %{lastname}" % {:firstname => "Masao", :lastname => "Mutoh"}
|
65
|
+
# # => "Masao Mutoh"
|
66
|
+
#
|
67
|
+
# * Using a Hash as an argument and formatted, named placeholders.
|
68
|
+
#
|
69
|
+
# When you pass a Hash as an argument and specify placeholders with %<foo>d
|
70
|
+
# it will interpret the hash values as named arguments and format the value
|
71
|
+
# according to the formatting instruction appended to the closing >.
|
72
|
+
#
|
73
|
+
# Example:
|
74
|
+
#
|
75
|
+
# "%<integer>d, %<float>.1f" % { :integer => 10, :float => 43.4 }
|
76
|
+
# # => "10, 43.3"
|
77
|
+
def %(args)
|
78
|
+
if args.kind_of?(Hash)
|
79
|
+
dup.gsub(INTERPOLATION_PATTERN_WITH_ESCAPE) do |match|
|
80
|
+
if match == '%%'
|
81
|
+
'%'
|
82
|
+
else
|
83
|
+
key = ($1 || $2).to_sym
|
84
|
+
raise KeyError unless args.has_key?(key)
|
85
|
+
$3 ? sprintf("%#{$3}", args[key]) : args[key]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
elsif self =~ INTERPOLATION_PATTERN
|
89
|
+
raise ArgumentError.new('one hash required')
|
90
|
+
else
|
91
|
+
result = gsub(/%([{<])/, '%%\1')
|
92
|
+
result.send :'interpolate_without_ruby_19_syntax', args
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/i18n/version.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# thanks to Masao's String extensions these should work the same in
|
4
|
+
# Ruby 1.8 (patched) and Ruby 1.9 (native)
|
5
|
+
# some tests taken from Masao's tests
|
6
|
+
# http://github.com/mutoh/gettext/blob/edbbe1fa8238fa12c7f26f2418403015f0270e47/test/test_string.rb
|
7
|
+
|
8
|
+
class I18nCoreExtStringInterpolationTest < Test::Unit::TestCase
|
9
|
+
test "String interpolates a single argument" do
|
10
|
+
assert_equal "Masao", "%s" % "Masao"
|
11
|
+
end
|
12
|
+
|
13
|
+
test "String interpolates an array argument" do
|
14
|
+
assert_equal "1 message", "%d %s" % [1, 'message']
|
15
|
+
end
|
16
|
+
|
17
|
+
test "String interpolates a hash argument w/ named placeholders" do
|
18
|
+
assert_equal "Masao Mutoh", "%{first} %{last}" % { :first => 'Masao', :last => 'Mutoh' }
|
19
|
+
end
|
20
|
+
|
21
|
+
test "String interpolates a hash argument w/ named placeholders (reverse order)" do
|
22
|
+
assert_equal "Mutoh, Masao", "%{last}, %{first}" % { :first => 'Masao', :last => 'Mutoh' }
|
23
|
+
end
|
24
|
+
|
25
|
+
test "String interpolates named placeholders with sprintf syntax" do
|
26
|
+
assert_equal "10, 43.4", "%<integer>d, %<float>.1f" % {:integer => 10, :float => 43.4}
|
27
|
+
end
|
28
|
+
|
29
|
+
test "String interpolates named placeholders with sprintf syntax, does not recurse" do
|
30
|
+
assert_equal "%<not_translated>s", "%{msg}" % { :msg => '%<not_translated>s', :not_translated => 'should not happen' }
|
31
|
+
end
|
32
|
+
|
33
|
+
test "String interpolation does not replace anything when no placeholders are given" do
|
34
|
+
assert_equal("aaa", "aaa" % {:num => 1})
|
35
|
+
assert_equal("bbb", "bbb" % [1])
|
36
|
+
end
|
37
|
+
|
38
|
+
test "String interpolation sprintf behaviour equals Ruby 1.9 behaviour" do
|
39
|
+
assert_equal("1", "%<num>d" % {:num => 1})
|
40
|
+
assert_equal("0b1", "%<num>#b" % {:num => 1})
|
41
|
+
assert_equal("foo", "%<msg>s" % {:msg => "foo"})
|
42
|
+
assert_equal("1.000000", "%<num>f" % {:num => 1.0})
|
43
|
+
assert_equal(" 1", "%<num>3.0f" % {:num => 1.0})
|
44
|
+
assert_equal("100.00", "%<num>2.2f" % {:num => 100.0})
|
45
|
+
assert_equal("0x64", "%<num>#x" % {:num => 100.0})
|
46
|
+
assert_raise(ArgumentError) { "%<num>,d" % {:num => 100} }
|
47
|
+
assert_raise(ArgumentError) { "%<num>/d" % {:num => 100} }
|
48
|
+
end
|
49
|
+
|
50
|
+
test "String interpolation old-style sprintf still works" do
|
51
|
+
assert_equal("foo 1.000000", "%s %f" % ["foo", 1.0])
|
52
|
+
end
|
53
|
+
|
54
|
+
test "String interpolation raises an ArgumentError when the string has extra placeholders (Array)" do
|
55
|
+
assert_raise(ArgumentError) do # Ruby 1.9 msg: "too few arguments"
|
56
|
+
"%s %s" % %w(Masao)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "String interpolation raises a KeyError when the string has extra placeholders (Hash)" do
|
61
|
+
assert_raise(KeyError) do # Ruby 1.9 msg: "key not found"
|
62
|
+
"%{first} %{last}" % { :first => 'Masao' }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
test "String interpolation does not raise when passed extra values (Array)" do
|
67
|
+
assert_nothing_raised do
|
68
|
+
assert_equal "Masao", "%s" % %w(Masao Mutoh)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
test "String interpolation does not raise when passed extra values (Hash)" do
|
73
|
+
assert_nothing_raised do
|
74
|
+
assert_equal "Masao Mutoh", "%{first} %{last}" % { :first => 'Masao', :last => 'Mutoh', :salutation => 'Mr.' }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
test "% acts as escape character in String interpolation" do
|
79
|
+
assert_equal "%{first}", "%%{first}" % { :first => 'Masao' }
|
80
|
+
assert_equal("% 1", "%% %<num>d" % {:num => 1.0})
|
81
|
+
assert_equal("%{num} %<num>d", "%%{num} %%<num>d" % {:num => 1})
|
82
|
+
end
|
83
|
+
|
84
|
+
test "% can be used in Ruby's own sprintf behavior" do
|
85
|
+
assert_equal "70%", "%d%%" % 70
|
86
|
+
assert_equal "70-100%", "%d-%d%%" % [70, 100]
|
87
|
+
assert_equal "+2.30%", "%+.2f%%" % 2.3
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_sprintf_mix_unformatted_and_formatted_named_placeholders
|
91
|
+
assert_equal("foo 1.000000", "%{name} %<num>f" % {:name => "foo", :num => 1.0})
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_string_interpolation_raises_an_argument_error_when_mixing_named_and_unnamed_placeholders
|
95
|
+
assert_raise(ArgumentError) { "%{name} %f" % [1.0] }
|
96
|
+
assert_raise(ArgumentError) { "%{name} %f" % [1.0, 2.0] }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: -
|
4
|
+
hash: -1150612295
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 0beta3
|
10
|
+
version: 0.5.0beta3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Fuchs
|
@@ -162,6 +162,7 @@ files:
|
|
162
162
|
- test/backend/simple_test.rb
|
163
163
|
- test/backend/transliterator_test.rb
|
164
164
|
- test/core_ext/hash_test.rb
|
165
|
+
- test/core_ext/string/interpolate_test.rb
|
165
166
|
- test/gettext/api_test.rb
|
166
167
|
- test/gettext/backend_test.rb
|
167
168
|
- test/i18n/exceptions_test.rb
|