i18n 0.2.0 → 0.2.1

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.

@@ -1,59 +1,59 @@
1
- class KeyError < IndexError
2
- def initialize(message = nil)
3
- super(message || "key not found")
4
- end
5
- end unless defined?(KeyError)
6
-
7
- module I18n
8
- class ArgumentError < ::ArgumentError; end
9
-
10
- class InvalidLocale < ArgumentError
11
- attr_reader :locale
12
- def initialize(locale)
13
- @locale = locale
14
- super "#{locale.inspect} is not a valid locale"
15
- end
16
- end
17
-
18
- class MissingTranslationData < ArgumentError
19
- attr_reader :locale, :key, :options
20
- def initialize(locale, key, options)
21
- @key, @locale, @options = key, locale, options
22
- keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
23
- keys << 'no key' if keys.size < 2
24
- super "translation missing: #{keys.join(', ')}"
25
- end
26
- end
27
-
28
- class InvalidPluralizationData < ArgumentError
29
- attr_reader :entry, :count
30
- def initialize(entry, count)
31
- @entry, @count = entry, count
32
- super "translation data #{entry.inspect} can not be used with :count => #{count}"
33
- end
34
- end
35
-
36
- class MissingInterpolationArgument < ArgumentError
37
- attr_reader :values, :string
38
- def initialize(values, string)
39
- @values, @string = values, string
40
- super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
41
- end
42
- end
43
-
44
- class ReservedInterpolationKey < ArgumentError
45
- attr_reader :key, :string
46
- def initialize(key, string)
47
- @key, @string = key, string
48
- super "reserved key #{key.inspect} used in #{string.inspect}"
49
- end
50
- end
51
-
52
- class UnknownFileType < ArgumentError
53
- attr_reader :type, :filename
54
- def initialize(type, filename)
55
- @type, @filename = type, filename
56
- super "can not load translations from #{filename}, the file type #{type} is not known"
57
- end
58
- end
1
+ class KeyError < IndexError
2
+ def initialize(message = nil)
3
+ super(message || "key not found")
4
+ end
5
+ end unless defined?(KeyError)
6
+
7
+ module I18n
8
+ class ArgumentError < ::ArgumentError; end
9
+
10
+ class InvalidLocale < ArgumentError
11
+ attr_reader :locale
12
+ def initialize(locale)
13
+ @locale = locale
14
+ super "#{locale.inspect} is not a valid locale"
15
+ end
16
+ end
17
+
18
+ class MissingTranslationData < ArgumentError
19
+ attr_reader :locale, :key, :options
20
+ def initialize(locale, key, options)
21
+ @key, @locale, @options = key, locale, options
22
+ keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
23
+ keys << 'no key' if keys.size < 2
24
+ super "translation missing: #{keys.join(', ')}"
25
+ end
26
+ end
27
+
28
+ class InvalidPluralizationData < ArgumentError
29
+ attr_reader :entry, :count
30
+ def initialize(entry, count)
31
+ @entry, @count = entry, count
32
+ super "translation data #{entry.inspect} can not be used with :count => #{count}"
33
+ end
34
+ end
35
+
36
+ class MissingInterpolationArgument < ArgumentError
37
+ attr_reader :values, :string
38
+ def initialize(values, string)
39
+ @values, @string = values, string
40
+ super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
41
+ end
42
+ end
43
+
44
+ class ReservedInterpolationKey < ArgumentError
45
+ attr_reader :key, :string
46
+ def initialize(key, string)
47
+ @key, @string = key, string
48
+ super "reserved key #{key.inspect} used in #{string.inspect}"
49
+ end
50
+ end
51
+
52
+ class UnknownFileType < ArgumentError
53
+ attr_reader :type, :filename
54
+ def initialize(type, filename)
55
+ @type, @filename = type, filename
56
+ super "can not load translations from #{filename}, the file type #{type} is not known"
57
+ end
58
+ end
59
59
  end
data/lib/i18n/string.rb CHANGED
@@ -1,93 +1,93 @@
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
- if RUBY_VERSION < '1.9'
9
-
10
- # KeyError is raised by String#% when the string contains a named placeholder
11
- # that is not contained in the given arguments hash. Ruby 1.9 includes and
12
- # raises this exception natively. We define it to mimic Ruby 1.9's behaviour
13
- # in Ruby 1.8.x
14
-
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
-
26
- class String
27
- # For older ruby versions, such as ruby-1.8.5
28
- alias :bytesize :size unless instance_methods.find {|m| m.to_s == 'bytesize'}
29
- alias :interpolate_without_ruby_19_syntax :% # :nodoc:
30
-
31
- INTERPOLATION_PATTERN = Regexp.union(
32
- /%%/,
33
- /%\{(\w+)\}/, # matches placeholders like "%{foo}"
34
- /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
35
- )
36
-
37
- # % uses self (i.e. the String) as a format specification and returns the
38
- # result of applying it to the given arguments. In other words it interpolates
39
- # the given arguments to the string according to the formats the string
40
- # defines.
41
- #
42
- # There are three ways to use it:
43
- #
44
- # * Using a single argument or Array of arguments.
45
- #
46
- # This is the default behaviour of the String class. See Kernel#sprintf for
47
- # more details about the format string.
48
- #
49
- # Example:
50
- #
51
- # "%d %s" % [1, "message"]
52
- # # => "1 message"
53
- #
54
- # * Using a Hash as an argument and unformatted, named placeholders.
55
- #
56
- # When you pass a Hash as an argument and specify placeholders with %{foo}
57
- # it will interpret the hash values as named arguments.
58
- #
59
- # Example:
60
- #
61
- # "%{firstname}, %{lastname}" % {:firstname => "Masao", :lastname => "Mutoh"}
62
- # # => "Masao Mutoh"
63
- #
64
- # * Using a Hash as an argument and formatted, named placeholders.
65
- #
66
- # When you pass a Hash as an argument and specify placeholders with %<foo>d
67
- # it will interpret the hash values as named arguments and format the value
68
- # according to the formatting instruction appended to the closing >.
69
- #
70
- # Example:
71
- #
72
- # "%<integer>d, %<float>.1f" % { :integer => 10, :float => 43.4 }
73
- # # => "10, 43.3"
74
- def %(args)
75
- if args.kind_of?(Hash)
76
- dup.gsub(INTERPOLATION_PATTERN) do |match|
77
- if match == '%%'
78
- '%'
79
- else
80
- key = ($1 || $2).to_sym
81
- raise KeyError unless args.has_key?(key)
82
- $3 ? sprintf("%#{$3}", args[key]) : args[key]
83
- end
84
- end
85
- elsif self =~ INTERPOLATION_PATTERN
86
- raise ArgumentError.new('one hash required')
87
- else
88
- result = gsub(/%([{<])/, '%%\1')
89
- result.send :'interpolate_without_ruby_19_syntax', args
90
- end
91
- end
92
- end
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
+ if RUBY_VERSION < '1.9'
9
+
10
+ # KeyError is raised by String#% when the string contains a named placeholder
11
+ # that is not contained in the given arguments hash. Ruby 1.9 includes and
12
+ # raises this exception natively. We define it to mimic Ruby 1.9's behaviour
13
+ # in Ruby 1.8.x
14
+
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
+
26
+ class String
27
+ # For older ruby versions, such as ruby-1.8.5
28
+ alias :bytesize :size unless instance_methods.find {|m| m.to_s == 'bytesize'}
29
+ alias :interpolate_without_ruby_19_syntax :% # :nodoc:
30
+
31
+ INTERPOLATION_PATTERN = Regexp.union(
32
+ /%%/,
33
+ /%\{(\w+)\}/, # matches placeholders like "%{foo}"
34
+ /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
35
+ )
36
+
37
+ # % uses self (i.e. the String) as a format specification and returns the
38
+ # result of applying it to the given arguments. In other words it interpolates
39
+ # the given arguments to the string according to the formats the string
40
+ # defines.
41
+ #
42
+ # There are three ways to use it:
43
+ #
44
+ # * Using a single argument or Array of arguments.
45
+ #
46
+ # This is the default behaviour of the String class. See Kernel#sprintf for
47
+ # more details about the format string.
48
+ #
49
+ # Example:
50
+ #
51
+ # "%d %s" % [1, "message"]
52
+ # # => "1 message"
53
+ #
54
+ # * Using a Hash as an argument and unformatted, named placeholders.
55
+ #
56
+ # When you pass a Hash as an argument and specify placeholders with %{foo}
57
+ # it will interpret the hash values as named arguments.
58
+ #
59
+ # Example:
60
+ #
61
+ # "%{firstname}, %{lastname}" % {:firstname => "Masao", :lastname => "Mutoh"}
62
+ # # => "Masao Mutoh"
63
+ #
64
+ # * Using a Hash as an argument and formatted, named placeholders.
65
+ #
66
+ # When you pass a Hash as an argument and specify placeholders with %<foo>d
67
+ # it will interpret the hash values as named arguments and format the value
68
+ # according to the formatting instruction appended to the closing >.
69
+ #
70
+ # Example:
71
+ #
72
+ # "%<integer>d, %<float>.1f" % { :integer => 10, :float => 43.4 }
73
+ # # => "10, 43.3"
74
+ def %(args)
75
+ if args.kind_of?(Hash)
76
+ dup.gsub(INTERPOLATION_PATTERN) do |match|
77
+ if match == '%%'
78
+ '%'
79
+ else
80
+ key = ($1 || $2).to_sym
81
+ raise KeyError unless args.has_key?(key)
82
+ $3 ? sprintf("%#{$3}", args[key]) : args[key]
83
+ end
84
+ end
85
+ elsif self =~ INTERPOLATION_PATTERN
86
+ raise ArgumentError.new('one hash required')
87
+ else
88
+ result = gsub(/%([{<])/, '%%\1')
89
+ result.send :'interpolate_without_ruby_19_syntax', args
90
+ end
91
+ end
92
+ end
93
93
  end
data/test/all.rb CHANGED
@@ -1,3 +1,3 @@
1
- Dir[File.dirname(__FILE__) + '/**/*_test.rb'].each do |file|
2
- require file
3
- end
1
+ Dir[File.dirname(__FILE__) + '/**/*_test.rb'].each do |file|
2
+ require file
3
+ end
data/test/api/basics.rb CHANGED
@@ -1,13 +1,13 @@
1
- module Tests
2
- module Backend
3
- module Api
4
- module Basics
5
- def test_available_locales
6
- backend_store_translations 'de', :foo => 'bar'
7
- backend_store_translations 'en', :foo => 'foo'
8
- assert_equal ['de', 'en'], I18n.backend.available_locales.map{|locale| locale.to_s }.sort
9
- end
10
- end
11
- end
12
- end
1
+ module Tests
2
+ module Backend
3
+ module Api
4
+ module Basics
5
+ def test_available_locales
6
+ backend_store_translations 'de', :foo => 'bar'
7
+ backend_store_translations 'en', :foo => 'foo'
8
+ assert_equal ['de', 'en'], I18n.backend.available_locales.map{|locale| locale.to_s }.sort
9
+ end
10
+ end
11
+ end
12
+ end
13
13
  end
@@ -1,63 +1,63 @@
1
- module Tests
2
- module Backend
3
- module Api
4
- module Interpolation
5
- def interpolate(options)
6
- I18n.backend.translate('en', nil, options)
7
- end
8
-
9
- def test_interpolation_given_no_interpolation_values_it_does_not_alter_the_string
10
- assert_equal 'Hi {{name}}!', interpolate(:default => 'Hi {{name}}!')
11
- end
12
-
13
- def test_interpolation_given_interpolation_values_it_interpolates_the_values_to_the_string
14
- assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
15
- end
16
-
17
- def test_interpolation_given_interpolation_values_with_nil_values_it_interpolates_the_values_to_the_string
18
- assert_equal 'Hi !', interpolate(:default => 'Hi {{name}}!', :name => nil)
19
- end
20
-
21
- def test_interpolate_with_ruby_1_9_syntax
22
- assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
23
- end
24
-
25
- def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
26
- assert_equal 'Häi David!', interpolate(:default => 'Häi {{name}}!', :name => 'David')
27
- end
28
-
29
- def test_interpolate_given_a_unicode_value_hash_interpolates_to_the_string
30
- assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi {{name}}!', :name => 'ゆきひろ')
31
- end
32
-
33
- def test_interpolate_given_a_unicode_value_hash_interpolates_into_unicode_string
34
- assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
35
- end
36
-
37
- if Kernel.const_defined?(:Encoding)
38
- def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
39
- assert_equal euc_jp('Hi ゆきひろ!'), interpolate(:default => 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
40
- end
41
-
42
- def test_interpolate_given_a_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
43
- assert_raises(Encoding::CompatibilityError) do
44
- interpolate(:default => euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
45
- end
46
- end
47
-
48
- def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
49
- assert_raises(Encoding::CompatibilityError) do
50
- interpolate(:default => 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
51
- end
52
- end
53
- end
54
-
55
- def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
56
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{default}}', :foo => :bar) }
57
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{scope}}', :foo => :bar) }
58
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{separator}}', :foo => :bar) }
59
- end
60
- end
61
- end
62
- end
1
+ module Tests
2
+ module Backend
3
+ module Api
4
+ module Interpolation
5
+ def interpolate(options)
6
+ I18n.backend.translate('en', nil, options)
7
+ end
8
+
9
+ def test_interpolation_given_no_interpolation_values_it_does_not_alter_the_string
10
+ assert_equal 'Hi {{name}}!', interpolate(:default => 'Hi {{name}}!')
11
+ end
12
+
13
+ def test_interpolation_given_interpolation_values_it_interpolates_the_values_to_the_string
14
+ assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
15
+ end
16
+
17
+ def test_interpolation_given_interpolation_values_with_nil_values_it_interpolates_the_values_to_the_string
18
+ assert_equal 'Hi !', interpolate(:default => 'Hi {{name}}!', :name => nil)
19
+ end
20
+
21
+ def test_interpolate_with_ruby_1_9_syntax
22
+ assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
23
+ end
24
+
25
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
26
+ assert_equal 'Häi David!', interpolate(:default => 'Häi {{name}}!', :name => 'David')
27
+ end
28
+
29
+ def test_interpolate_given_a_unicode_value_hash_interpolates_to_the_string
30
+ assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi {{name}}!', :name => 'ゆきひろ')
31
+ end
32
+
33
+ def test_interpolate_given_a_unicode_value_hash_interpolates_into_unicode_string
34
+ assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
35
+ end
36
+
37
+ if Kernel.const_defined?(:Encoding)
38
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
39
+ assert_equal euc_jp('Hi ゆきひろ!'), interpolate(:default => 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
40
+ end
41
+
42
+ def test_interpolate_given_a_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
43
+ assert_raises(Encoding::CompatibilityError) do
44
+ interpolate(:default => euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
45
+ end
46
+ end
47
+
48
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
49
+ assert_raises(Encoding::CompatibilityError) do
50
+ interpolate(:default => 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
56
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{default}}', :foo => :bar) }
57
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{scope}}', :foo => :bar) }
58
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{separator}}', :foo => :bar) }
59
+ end
60
+ end
61
+ end
62
+ end
63
63
  end