i18n 0.6.11 → 0.7.0.beta1
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.
- checksums.yaml +7 -0
- data/README.md +3 -26
- data/gemfiles/Gemfile.rails-3.2.x +0 -2
- data/gemfiles/Gemfile.rails-3.2.x.lock +5 -9
- data/gemfiles/Gemfile.rails-4.0.x +0 -2
- data/gemfiles/Gemfile.rails-4.0.x.lock +7 -11
- data/gemfiles/Gemfile.rails-4.1.x +0 -2
- data/gemfiles/Gemfile.rails-4.1.x.lock +7 -11
- data/gemfiles/{Gemfile.rails-3.1.x → Gemfile.rails-master} +1 -3
- data/gemfiles/Gemfile.rails-master.lock +40 -0
- data/lib/i18n.rb +7 -30
- data/lib/i18n/backend/base.rb +0 -1
- data/lib/i18n/backend/interpolation_compiler.rb +1 -1
- data/lib/i18n/backend/key_value.rb +0 -1
- data/lib/i18n/config.rb +16 -6
- data/lib/i18n/exceptions.rb +4 -28
- data/lib/i18n/version.rb +1 -1
- data/test/api/key_value_test.rb +2 -6
- data/test/api/memoize_test.rb +2 -6
- data/test/api/override_test.rb +2 -3
- data/test/backend/cache_test.rb +1 -0
- data/test/backend/cascade_test.rb +1 -0
- data/test/backend/chain_test.rb +1 -0
- data/test/backend/exceptions_test.rb +1 -0
- data/test/backend/fallbacks_test.rb +8 -2
- data/test/backend/key_value_test.rb +2 -5
- data/test/backend/memoize_test.rb +2 -2
- data/test/backend/metadata_test.rb +1 -0
- data/test/backend/pluralization_test.rb +1 -0
- data/test/backend/simple_test.rb +2 -1
- data/test/backend/transliterator_test.rb +4 -5
- data/test/gettext/api_test.rb +1 -0
- data/test/gettext/backend_test.rb +73 -82
- data/test/i18n/exceptions_test.rb +0 -15
- data/test/i18n/interpolate_test.rb +2 -1
- data/test/i18n/load_path_test.rb +1 -0
- data/test/i18n_test.rb +35 -0
- data/test/locale/fallbacks_test.rb +1 -4
- data/test/locale/tag/rfc4646_test.rb +1 -0
- data/test/run_all.rb +1 -1
- data/test/test_helper.rb +20 -41
- metadata +19 -28
- data/gemfiles/Gemfile.rails-2.3.x +0 -11
- data/gemfiles/Gemfile.rails-2.3.x.lock +0 -30
- data/gemfiles/Gemfile.rails-3.0.x +0 -11
- data/gemfiles/Gemfile.rails-3.0.x.lock +0 -30
- data/gemfiles/Gemfile.rails-3.1.x.lock +0 -30
- data/lib/i18n/core_ext/string/interpolate.rb +0 -105
- data/test/all.rb +0 -8
- data/test/core_ext/string/interpolate_test.rb +0 -99
data/test/i18n/load_path_test.rb
CHANGED
data/test/i18n_test.rb
CHANGED
@@ -3,6 +3,7 @@ require 'test_helper'
|
|
3
3
|
|
4
4
|
class I18nTest < I18n::TestCase
|
5
5
|
def setup
|
6
|
+
super
|
6
7
|
store_translations(:en, :currency => { :format => { :separator => '.', :delimiter => ',', } })
|
7
8
|
store_translations(:nl, :currency => { :format => { :separator => ',', :delimiter => '.', } })
|
8
9
|
end
|
@@ -37,6 +38,10 @@ class I18nTest < I18n::TestCase
|
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
41
|
+
test "default_locale= doesn't ignore junk" do
|
42
|
+
assert_raise(NoMethodError) { I18n.default_locale = Class }
|
43
|
+
end
|
44
|
+
|
40
45
|
test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
|
41
46
|
begin
|
42
47
|
I18n.config.enforce_available_locales = true
|
@@ -57,6 +62,10 @@ class I18nTest < I18n::TestCase
|
|
57
62
|
I18n.locale = :en
|
58
63
|
end
|
59
64
|
|
65
|
+
test "locale= doesn't ignore junk" do
|
66
|
+
assert_raise(NoMethodError) { I18n.locale = Class }
|
67
|
+
end
|
68
|
+
|
60
69
|
test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
|
61
70
|
begin
|
62
71
|
I18n.config.enforce_available_locales = true
|
@@ -379,4 +388,30 @@ class I18nTest < I18n::TestCase
|
|
379
388
|
I18n.config.enforce_available_locales = false
|
380
389
|
end
|
381
390
|
end
|
391
|
+
|
392
|
+
test 'I18n.reload! reloads the set of locales that are enforced' do
|
393
|
+
begin
|
394
|
+
I18n.enforce_available_locales = true
|
395
|
+
|
396
|
+
assert_raise(I18n::InvalidLocale) { I18n.default_locale = :de }
|
397
|
+
assert_raise(I18n::InvalidLocale) { I18n.locale = :de }
|
398
|
+
|
399
|
+
store_translations(:de, :foo => 'Foo in :de')
|
400
|
+
|
401
|
+
assert_raise(I18n::InvalidLocale) { I18n.default_locale = :de }
|
402
|
+
assert_raise(I18n::InvalidLocale) { I18n.locale = :de }
|
403
|
+
|
404
|
+
I18n.reload!
|
405
|
+
|
406
|
+
store_translations(:en, :foo => 'Foo in :en')
|
407
|
+
store_translations(:de, :foo => 'Foo in :de')
|
408
|
+
store_translations(:pl, :foo => 'Foo in :pl')
|
409
|
+
|
410
|
+
assert_nothing_raised { I18n.default_locale = I18n.locale = :en }
|
411
|
+
assert_nothing_raised { I18n.default_locale = I18n.locale = :de }
|
412
|
+
assert_nothing_raised { I18n.default_locale = I18n.locale = :pl }
|
413
|
+
ensure
|
414
|
+
I18n.enforce_available_locales = false
|
415
|
+
end
|
416
|
+
end
|
382
417
|
end
|
@@ -3,10 +3,6 @@ require 'test_helper'
|
|
3
3
|
include I18n::Locale
|
4
4
|
|
5
5
|
class I18nFallbacksDefaultsTest < I18n::TestCase
|
6
|
-
def teardown
|
7
|
-
I18n.default_locale = :en
|
8
|
-
end
|
9
|
-
|
10
6
|
test "defaults reflect the I18n.default_locale if no default has been set manually" do
|
11
7
|
I18n.default_locale = :'en-US'
|
12
8
|
fallbacks = Fallbacks.new
|
@@ -28,6 +24,7 @@ end
|
|
28
24
|
|
29
25
|
class I18nFallbacksComputationTest < I18n::TestCase
|
30
26
|
def setup
|
27
|
+
super
|
31
28
|
@fallbacks = Fallbacks.new(:'en-US')
|
32
29
|
end
|
33
30
|
|
data/test/run_all.rb
CHANGED
@@ -14,7 +14,7 @@ results = gemfiles.map do |gemfile|
|
|
14
14
|
ENV['BUNDLE_GEMFILE'] = File.expand_path("../../#{gemfile}", __FILE__)
|
15
15
|
|
16
16
|
execute 'bundle install' unless bundle_check
|
17
|
-
execute 'bundle exec
|
17
|
+
execute 'bundle exec rake test'
|
18
18
|
end
|
19
19
|
|
20
20
|
exit results.all?
|
data/test/test_helper.rb
CHANGED
@@ -1,33 +1,16 @@
|
|
1
1
|
$KCODE = 'u' if RUBY_VERSION <= '1.9'
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'minitest/autorun'
|
4
|
+
TEST_CASE = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
|
4
5
|
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
# TODO: Remove these aliases and update tests accordingly.
|
7
|
+
class TEST_CASE
|
8
|
+
alias :assert_raise :assert_raises
|
9
|
+
alias :assert_not_equal :refute_equal
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
alias :assert_raise :assert_raises
|
13
|
-
alias :assert_not_equal :refute_equal
|
14
|
-
|
15
|
-
def assert_nothing_raised(*args)
|
16
|
-
yield
|
17
|
-
end
|
11
|
+
def assert_nothing_raised(*args)
|
12
|
+
yield
|
18
13
|
end
|
19
|
-
rescue LoadError
|
20
|
-
require 'test/unit'
|
21
|
-
TEST_CASE = Test::Unit::TestCase
|
22
|
-
end
|
23
|
-
|
24
|
-
# Do not load the i18n gem from libraries like active_support.
|
25
|
-
#
|
26
|
-
# This is required for testing against Rails 2.3 because active_support/vendor.rb#24 tries
|
27
|
-
# to load I18n using the gem method. Instead, we want to test the local library of course.
|
28
|
-
alias :gem_for_ruby_19 :gem # for 1.9. gives a super ugly seg fault otherwise
|
29
|
-
def gem(gem_name, *version_requirements)
|
30
|
-
gem_name =='i18n' ? puts("skipping loading the i18n gem ...") : super
|
31
14
|
end
|
32
15
|
|
33
16
|
require 'bundler/setup'
|
@@ -36,28 +19,24 @@ require 'mocha/setup'
|
|
36
19
|
require 'test_declarative'
|
37
20
|
|
38
21
|
class I18n::TestCase < TEST_CASE
|
39
|
-
def self.
|
40
|
-
|
41
|
-
rescue LoadError => e
|
42
|
-
puts "can't use KeyValue backend because: #{e.message}"
|
22
|
+
def self.key_value?
|
23
|
+
defined?(ActiveSupport)
|
43
24
|
end
|
44
25
|
|
45
|
-
def
|
26
|
+
def setup
|
46
27
|
super
|
28
|
+
I18n.enforce_available_locales = false
|
29
|
+
end
|
30
|
+
|
31
|
+
def teardown
|
47
32
|
I18n.locale = nil
|
48
|
-
I18n.default_locale =
|
49
|
-
I18n.load_path =
|
33
|
+
I18n.default_locale = nil
|
34
|
+
I18n.load_path = nil
|
50
35
|
I18n.available_locales = nil
|
51
36
|
I18n.backend = nil
|
52
|
-
I18n.
|
53
|
-
|
54
|
-
|
55
|
-
# Ignore Test::Unit::TestCase failing if the test case does not contain any
|
56
|
-
# test, otherwise it will blow up because of this base class.
|
57
|
-
#
|
58
|
-
# TODO: remove when test-unit is not used anymore.
|
59
|
-
def default_test
|
60
|
-
nil
|
37
|
+
I18n.default_separator = nil
|
38
|
+
I18n.enforce_available_locales = true
|
39
|
+
super
|
61
40
|
end
|
62
41
|
|
63
42
|
protected
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.0.beta1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sven Fuchs
|
@@ -13,7 +12,7 @@ authors:
|
|
13
12
|
autorequire:
|
14
13
|
bindir: bin
|
15
14
|
cert_chain: []
|
16
|
-
date: 2014-
|
15
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
17
16
|
dependencies: []
|
18
17
|
description: New wave Internationalization support for Ruby.
|
19
18
|
email: rails-i18n@googlegroups.com
|
@@ -21,18 +20,18 @@ executables: []
|
|
21
20
|
extensions: []
|
22
21
|
extra_rdoc_files: []
|
23
22
|
files:
|
24
|
-
-
|
25
|
-
-
|
26
|
-
- gemfiles/Gemfile.rails-3.0.x
|
27
|
-
- gemfiles/Gemfile.rails-3.0.x.lock
|
28
|
-
- gemfiles/Gemfile.rails-3.1.x
|
29
|
-
- gemfiles/Gemfile.rails-3.1.x.lock
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.md
|
30
25
|
- gemfiles/Gemfile.rails-3.2.x
|
31
26
|
- gemfiles/Gemfile.rails-3.2.x.lock
|
32
27
|
- gemfiles/Gemfile.rails-4.0.x
|
33
28
|
- gemfiles/Gemfile.rails-4.0.x.lock
|
34
29
|
- gemfiles/Gemfile.rails-4.1.x
|
35
30
|
- gemfiles/Gemfile.rails-4.1.x.lock
|
31
|
+
- gemfiles/Gemfile.rails-master
|
32
|
+
- gemfiles/Gemfile.rails-master.lock
|
33
|
+
- lib/i18n.rb
|
34
|
+
- lib/i18n/backend.rb
|
36
35
|
- lib/i18n/backend/base.rb
|
37
36
|
- lib/i18n/backend/cache.rb
|
38
37
|
- lib/i18n/backend/cascade.rb
|
@@ -47,38 +46,34 @@ files:
|
|
47
46
|
- lib/i18n/backend/pluralization.rb
|
48
47
|
- lib/i18n/backend/simple.rb
|
49
48
|
- lib/i18n/backend/transliterator.rb
|
50
|
-
- lib/i18n/backend.rb
|
51
49
|
- lib/i18n/config.rb
|
52
50
|
- lib/i18n/core_ext/hash.rb
|
53
51
|
- lib/i18n/core_ext/kernel/suppress_warnings.rb
|
54
|
-
- lib/i18n/core_ext/string/interpolate.rb
|
55
52
|
- lib/i18n/exceptions.rb
|
53
|
+
- lib/i18n/gettext.rb
|
56
54
|
- lib/i18n/gettext/helpers.rb
|
57
55
|
- lib/i18n/gettext/po_parser.rb
|
58
|
-
- lib/i18n/gettext.rb
|
59
56
|
- lib/i18n/interpolate/ruby.rb
|
57
|
+
- lib/i18n/locale.rb
|
60
58
|
- lib/i18n/locale/fallbacks.rb
|
59
|
+
- lib/i18n/locale/tag.rb
|
61
60
|
- lib/i18n/locale/tag/parents.rb
|
62
61
|
- lib/i18n/locale/tag/rfc4646.rb
|
63
62
|
- lib/i18n/locale/tag/simple.rb
|
64
|
-
- lib/i18n/
|
65
|
-
- lib/i18n/locale.rb
|
63
|
+
- lib/i18n/tests.rb
|
66
64
|
- lib/i18n/tests/basics.rb
|
67
65
|
- lib/i18n/tests/defaults.rb
|
68
66
|
- lib/i18n/tests/interpolation.rb
|
69
67
|
- lib/i18n/tests/link.rb
|
68
|
+
- lib/i18n/tests/localization.rb
|
70
69
|
- lib/i18n/tests/localization/date.rb
|
71
70
|
- lib/i18n/tests/localization/date_time.rb
|
72
71
|
- lib/i18n/tests/localization/procs.rb
|
73
72
|
- lib/i18n/tests/localization/time.rb
|
74
|
-
- lib/i18n/tests/localization.rb
|
75
73
|
- lib/i18n/tests/lookup.rb
|
76
74
|
- lib/i18n/tests/pluralization.rb
|
77
75
|
- lib/i18n/tests/procs.rb
|
78
|
-
- lib/i18n/tests.rb
|
79
76
|
- lib/i18n/version.rb
|
80
|
-
- lib/i18n.rb
|
81
|
-
- test/all.rb
|
82
77
|
- test/api/all_features_test.rb
|
83
78
|
- test/api/cascade_test.rb
|
84
79
|
- test/api/chain_test.rb
|
@@ -101,7 +96,6 @@ files:
|
|
101
96
|
- test/backend/simple_test.rb
|
102
97
|
- test/backend/transliterator_test.rb
|
103
98
|
- test/core_ext/hash_test.rb
|
104
|
-
- test/core_ext/string/interpolate_test.rb
|
105
99
|
- test/gettext/api_test.rb
|
106
100
|
- test/gettext/backend_test.rb
|
107
101
|
- test/i18n/exceptions_test.rb
|
@@ -119,31 +113,28 @@ files:
|
|
119
113
|
- test/test_data/locales/invalid/syntax.yml
|
120
114
|
- test/test_data/locales/plurals.rb
|
121
115
|
- test/test_helper.rb
|
122
|
-
- README.md
|
123
|
-
- MIT-LICENSE
|
124
116
|
homepage: http://github.com/svenfuchs/i18n
|
125
117
|
licenses:
|
126
118
|
- MIT
|
119
|
+
metadata: {}
|
127
120
|
post_install_message:
|
128
121
|
rdoc_options: []
|
129
122
|
require_paths:
|
130
123
|
- lib
|
131
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
-
none: false
|
133
125
|
requirements:
|
134
|
-
- -
|
126
|
+
- - ">="
|
135
127
|
- !ruby/object:Gem::Version
|
136
128
|
version: '0'
|
137
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
-
none: false
|
139
130
|
requirements:
|
140
|
-
- -
|
131
|
+
- - ">="
|
141
132
|
- !ruby/object:Gem::Version
|
142
133
|
version: 1.3.5
|
143
134
|
requirements: []
|
144
|
-
rubyforge_project:
|
145
|
-
rubygems_version:
|
135
|
+
rubyforge_project: "[none]"
|
136
|
+
rubygems_version: 2.2.2
|
146
137
|
signing_key:
|
147
|
-
specification_version:
|
138
|
+
specification_version: 4
|
148
139
|
summary: New wave Internationalization support for Ruby
|
149
140
|
test_files: []
|
@@ -1,30 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ..
|
3
|
-
specs:
|
4
|
-
i18n (0.6.11)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
activesupport (2.3.18)
|
10
|
-
ffi (1.9.3)
|
11
|
-
metaclass (0.0.4)
|
12
|
-
mocha (1.0.0)
|
13
|
-
metaclass (~> 0.0.1)
|
14
|
-
rake (10.3.1)
|
15
|
-
rufus-tokyo (1.0.7)
|
16
|
-
test_declarative (0.0.5)
|
17
|
-
yajl-ruby (1.2.0)
|
18
|
-
|
19
|
-
PLATFORMS
|
20
|
-
ruby
|
21
|
-
|
22
|
-
DEPENDENCIES
|
23
|
-
activesupport (~> 2.3)
|
24
|
-
ffi
|
25
|
-
i18n!
|
26
|
-
mocha
|
27
|
-
rake
|
28
|
-
rufus-tokyo
|
29
|
-
test_declarative
|
30
|
-
yajl-ruby
|
@@ -1,30 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ..
|
3
|
-
specs:
|
4
|
-
i18n (0.6.11)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
activesupport (3.0.20)
|
10
|
-
ffi (1.9.3)
|
11
|
-
metaclass (0.0.4)
|
12
|
-
mocha (1.0.0)
|
13
|
-
metaclass (~> 0.0.1)
|
14
|
-
rake (10.3.1)
|
15
|
-
rufus-tokyo (1.0.7)
|
16
|
-
test_declarative (0.0.5)
|
17
|
-
yajl-ruby (1.2.0)
|
18
|
-
|
19
|
-
PLATFORMS
|
20
|
-
ruby
|
21
|
-
|
22
|
-
DEPENDENCIES
|
23
|
-
activesupport (~> 3.0.0)
|
24
|
-
ffi
|
25
|
-
i18n!
|
26
|
-
mocha
|
27
|
-
rake
|
28
|
-
rufus-tokyo
|
29
|
-
test_declarative
|
30
|
-
yajl-ruby
|
@@ -1,30 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ..
|
3
|
-
specs:
|
4
|
-
i18n (0.6.11)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
activesupport (3.1.12)
|
10
|
-
multi_json (~> 1.0)
|
11
|
-
ffi (1.9.3)
|
12
|
-
metaclass (0.0.4)
|
13
|
-
mocha (1.0.0)
|
14
|
-
metaclass (~> 0.0.1)
|
15
|
-
multi_json (1.10.0)
|
16
|
-
rake (10.3.1)
|
17
|
-
rufus-tokyo (1.0.7)
|
18
|
-
test_declarative (0.0.5)
|
19
|
-
|
20
|
-
PLATFORMS
|
21
|
-
ruby
|
22
|
-
|
23
|
-
DEPENDENCIES
|
24
|
-
activesupport (~> 3.1.0)
|
25
|
-
ffi
|
26
|
-
i18n!
|
27
|
-
mocha
|
28
|
-
rake
|
29
|
-
rufus-tokyo
|
30
|
-
test_declarative
|
@@ -1,105 +0,0 @@
|
|
1
|
-
# This backports the Ruby 1.9 String interpolation syntax to Ruby 1.8.
|
2
|
-
#
|
3
|
-
# This backport has been shipped with I18n for a number of versions. Meanwhile
|
4
|
-
# Rails has started to rely on it and we are going to move it to ActiveSupport.
|
5
|
-
# See https://rails.lighthouseapp.com/projects/8994/tickets/6013-move-19-string-interpolation-syntax-backport-from-i18n-to-activesupport
|
6
|
-
#
|
7
|
-
# Once the above patch has been applied to Rails the following code will be
|
8
|
-
# removed from I18n.
|
9
|
-
|
10
|
-
=begin
|
11
|
-
heavily based on Masao Mutoh's gettext String interpolation extension
|
12
|
-
http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
|
13
|
-
Copyright (C) 2005-2009 Masao Mutoh
|
14
|
-
You may redistribute it and/or modify it under the same license terms as Ruby.
|
15
|
-
=end
|
16
|
-
|
17
|
-
begin
|
18
|
-
raise ArgumentError if ("a %{x}" % {:x=>'b'}) != 'a b'
|
19
|
-
rescue ArgumentError
|
20
|
-
# KeyError is raised by String#% when the string contains a named placeholder
|
21
|
-
# that is not contained in the given arguments hash. Ruby 1.9 includes and
|
22
|
-
# raises this exception natively. We define it to mimic Ruby 1.9's behaviour
|
23
|
-
# in Ruby 1.8.x
|
24
|
-
class KeyError < IndexError
|
25
|
-
def initialize(message = nil)
|
26
|
-
super(message || "key not found")
|
27
|
-
end
|
28
|
-
end unless defined?(KeyError)
|
29
|
-
|
30
|
-
# Extension for String class. This feature is included in Ruby 1.9 or later but not occur TypeError.
|
31
|
-
#
|
32
|
-
# String#% method which accept "named argument". The translator can know
|
33
|
-
# the meaning of the msgids using "named argument" instead of %s/%d style.
|
34
|
-
class String
|
35
|
-
# For older ruby versions, such as ruby-1.8.5
|
36
|
-
alias :bytesize :size unless instance_methods.find {|m| m.to_s == 'bytesize'}
|
37
|
-
alias :interpolate_without_ruby_19_syntax :% # :nodoc:
|
38
|
-
|
39
|
-
INTERPOLATION_PATTERN = Regexp.union(
|
40
|
-
/%\{(\w+)\}/, # matches placeholders like "%{foo}"
|
41
|
-
/%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
|
42
|
-
)
|
43
|
-
|
44
|
-
INTERPOLATION_PATTERN_WITH_ESCAPE = Regexp.union(
|
45
|
-
/%%/,
|
46
|
-
INTERPOLATION_PATTERN
|
47
|
-
)
|
48
|
-
|
49
|
-
# % uses self (i.e. the String) as a format specification and returns the
|
50
|
-
# result of applying it to the given arguments. In other words it interpolates
|
51
|
-
# the given arguments to the string according to the formats the string
|
52
|
-
# defines.
|
53
|
-
#
|
54
|
-
# There are three ways to use it:
|
55
|
-
#
|
56
|
-
# * Using a single argument or Array of arguments.
|
57
|
-
#
|
58
|
-
# This is the default behaviour of the String class. See Kernel#sprintf for
|
59
|
-
# more details about the format string.
|
60
|
-
#
|
61
|
-
# Example:
|
62
|
-
#
|
63
|
-
# "%d %s" % [1, "message"]
|
64
|
-
# # => "1 message"
|
65
|
-
#
|
66
|
-
# * Using a Hash as an argument and unformatted, named placeholders.
|
67
|
-
#
|
68
|
-
# When you pass a Hash as an argument and specify placeholders with %{foo}
|
69
|
-
# it will interpret the hash values as named arguments.
|
70
|
-
#
|
71
|
-
# Example:
|
72
|
-
#
|
73
|
-
# "%{firstname}, %{lastname}" % {:firstname => "Masao", :lastname => "Mutoh"}
|
74
|
-
# # => "Masao Mutoh"
|
75
|
-
#
|
76
|
-
# * Using a Hash as an argument and formatted, named placeholders.
|
77
|
-
#
|
78
|
-
# When you pass a Hash as an argument and specify placeholders with %<foo>d
|
79
|
-
# it will interpret the hash values as named arguments and format the value
|
80
|
-
# according to the formatting instruction appended to the closing >.
|
81
|
-
#
|
82
|
-
# Example:
|
83
|
-
#
|
84
|
-
# "%<integer>d, %<float>.1f" % { :integer => 10, :float => 43.4 }
|
85
|
-
# # => "10, 43.3"
|
86
|
-
def %(args)
|
87
|
-
if args.kind_of?(Hash)
|
88
|
-
dup.gsub(INTERPOLATION_PATTERN_WITH_ESCAPE) do |match|
|
89
|
-
if match == '%%'
|
90
|
-
'%'
|
91
|
-
else
|
92
|
-
key = ($1 || $2).to_sym
|
93
|
-
raise KeyError unless args.has_key?(key)
|
94
|
-
$3 ? sprintf("%#{$3}", args[key]) : args[key]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
elsif self =~ INTERPOLATION_PATTERN
|
98
|
-
raise ArgumentError.new('one hash required')
|
99
|
-
else
|
100
|
-
result = gsub(/%([{<])/, '%%\1')
|
101
|
-
result.send :'interpolate_without_ruby_19_syntax', args
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|