russian 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,192 @@
1
+ require 'yaml'
2
+
3
+ module I18n
4
+ module Backend
5
+ class Simple
6
+ INTERPOLATION_RESERVED_KEYS = %w(scope default)
7
+ MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
8
+
9
+ # Accepts a list of paths to translation files. Loads translations from
10
+ # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
11
+ # for details.
12
+ def load_translations(*filenames)
13
+ filenames.each {|filename| load_file filename }
14
+ end
15
+
16
+ # Stores translations for the given locale in memory.
17
+ # This uses a deep merge for the translations hash, so existing
18
+ # translations will be overwritten by new ones only at the deepest
19
+ # level of the hash.
20
+ def store_translations(locale, data)
21
+ merge_translations(locale, data)
22
+ end
23
+
24
+ def translate(locale, key, options = {})
25
+ raise InvalidLocale.new(locale) if locale.nil?
26
+ return key.map{|k| translate locale, k, options } if key.is_a? Array
27
+
28
+ reserved = :scope, :default
29
+ count, scope, default = options.values_at(:count, *reserved)
30
+ options.delete(:default)
31
+ values = options.reject{|name, value| reserved.include? name }
32
+
33
+ entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
34
+ entry = pluralize locale, entry, count
35
+ entry = interpolate locale, entry, values
36
+ entry
37
+ end
38
+
39
+ # Acts the same as +strftime+, but returns a localized version of the
40
+ # formatted date string. Takes a key from the date/time formats
41
+ # translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
42
+ def localize(locale, object, format = :default)
43
+ raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
44
+
45
+ type = object.respond_to?(:sec) ? 'time' : 'date'
46
+ # TODO only translate these if format is a String?
47
+ formats = translate(locale, :"#{type}.formats")
48
+ format = formats[format.to_sym] if formats && formats[format.to_sym]
49
+ # TODO raise exception unless format found?
50
+ format = format.to_s.dup
51
+
52
+ # TODO only translate these if the format string is actually present
53
+ # TODO check which format strings are present, then bulk translate then, then replace them
54
+ format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
55
+ format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
56
+ format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
57
+ format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
58
+ format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
59
+ object.strftime(format)
60
+ end
61
+
62
+ protected
63
+
64
+ def translations
65
+ @translations ||= {}
66
+ end
67
+
68
+ # Looks up a translation from the translations hash. Returns nil if
69
+ # eiher key is nil, or locale, scope or key do not exist as a key in the
70
+ # nested translations hash. Splits keys or scopes containing dots
71
+ # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
72
+ # <tt>%w(currency format)</tt>.
73
+ def lookup(locale, key, scope = [])
74
+ return unless key
75
+ keys = I18n.send :normalize_translation_keys, locale, key, scope
76
+ keys.inject(translations){|result, k| result[k.to_sym] or return nil }
77
+ end
78
+
79
+ # Evaluates a default translation.
80
+ # If the given default is a String it is used literally. If it is a Symbol
81
+ # it will be translated with the given options. If it is an Array the first
82
+ # translation yielded will be returned.
83
+ #
84
+ # <em>I.e.</em>, <tt>default(locale, [:foo, 'default'])</tt> will return +default+ if
85
+ # <tt>translate(locale, :foo)</tt> does not yield a result.
86
+ def default(locale, default, options = {})
87
+ case default
88
+ when String then default
89
+ when Symbol then translate locale, default, options
90
+ when Array then default.each do |obj|
91
+ result = default(locale, obj, options.dup) and return result
92
+ end and nil
93
+ end
94
+ rescue MissingTranslationData
95
+ nil
96
+ end
97
+
98
+ # Picks a translation from an array according to English pluralization
99
+ # rules. It will pick the first translation if count is not equal to 1
100
+ # and the second translation if it is equal to 1. Other backends can
101
+ # implement more flexible or complex pluralization rules.
102
+ def pluralize(locale, entry, count)
103
+ return entry unless entry.is_a?(Hash) and count
104
+ # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash)
105
+ key = :zero if count == 0 && entry.has_key?(:zero)
106
+ key ||= count == 1 ? :one : :other
107
+ raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
108
+ entry[key]
109
+ end
110
+
111
+ # Interpolates values into a given string.
112
+ #
113
+ # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
114
+ # # => "file test.txt opened by {{user}}"
115
+ #
116
+ # Note that you have to double escape the <tt>\\</tt> when you want to escape
117
+ # the <tt>{{...}}</tt> key in a string (once for the string and once for the
118
+ # interpolation).
119
+ def interpolate(locale, string, values = {})
120
+ return string unless string.is_a?(String)
121
+
122
+ string = string.gsub(/%d/, '{{count}}').gsub(/%s/, '{{value}}')
123
+
124
+ if string.respond_to?(:force_encoding)
125
+ original_encoding = string.encoding
126
+ string.force_encoding(Encoding::BINARY)
127
+ end
128
+
129
+ result = string.gsub(MATCH) do
130
+ escaped, pattern, key = $1, $2, $2.to_sym
131
+
132
+ if escaped
133
+ pattern
134
+ elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
135
+ raise ReservedInterpolationKey.new(pattern, string)
136
+ elsif !values.include?(key)
137
+ raise MissingInterpolationArgument.new(pattern, string)
138
+ else
139
+ values[key].to_s
140
+ end
141
+ end
142
+
143
+ result.force_encoding(original_encoding) if original_encoding
144
+ result
145
+ end
146
+
147
+ # Loads a single translations file by delegating to #load_rb or
148
+ # #load_yml depending on the file extension and directly merges the
149
+ # data to the existing translations. Raises I18n::UnknownFileType
150
+ # for all other file extensions.
151
+ def load_file(filename)
152
+ type = File.extname(filename).tr('.', '').downcase
153
+ raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}"
154
+ data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
155
+ data.each{|locale, d| merge_translations locale, d }
156
+ end
157
+
158
+ # Loads a plain Ruby translations file. eval'ing the file must yield
159
+ # a Hash containing translation data with locales as toplevel keys.
160
+ def load_rb(filename)
161
+ eval IO.read(filename), binding, filename
162
+ end
163
+
164
+ # Loads a YAML translations file. The data must have locales as
165
+ # toplevel keys.
166
+ def load_yml(filename)
167
+ YAML::load IO.read(filename)
168
+ end
169
+
170
+ # Deep merges the given translations hash with the existing translations
171
+ # for the given locale
172
+ def merge_translations(locale, data)
173
+ locale = locale.to_sym
174
+ translations[locale] ||= {}
175
+ data = deep_symbolize_keys data
176
+
177
+ # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
178
+ merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
179
+ translations[locale].merge! data, &merger
180
+ end
181
+
182
+ # Return a new hash with all keys and nested keys converted to symbols.
183
+ def deep_symbolize_keys(hash)
184
+ hash.inject({}){|result, (key, value)|
185
+ value = deep_symbolize_keys(value) if value.is_a? Hash
186
+ result[(key.to_sym rescue key) || key] = value
187
+ result
188
+ }
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,53 @@
1
+ module I18n
2
+ class ArgumentError < ::ArgumentError; end
3
+
4
+ class InvalidLocale < ArgumentError
5
+ attr_reader :locale
6
+ def initialize(locale)
7
+ @locale = locale
8
+ super "#{locale.inspect} is not a valid locale"
9
+ end
10
+ end
11
+
12
+ class MissingTranslationData < ArgumentError
13
+ attr_reader :locale, :key, :options
14
+ def initialize(locale, key, options)
15
+ @key, @locale, @options = key, locale, options
16
+ keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
17
+ keys << 'no key' if keys.size < 2
18
+ super "translation missing: #{keys.join(', ')}"
19
+ end
20
+ end
21
+
22
+ class InvalidPluralizationData < ArgumentError
23
+ attr_reader :entry, :count
24
+ def initialize(entry, count)
25
+ @entry, @count = entry, count
26
+ super "translation data #{entry.inspect} can not be used with :count => #{count}"
27
+ end
28
+ end
29
+
30
+ class MissingInterpolationArgument < ArgumentError
31
+ attr_reader :key, :string
32
+ def initialize(key, string)
33
+ @key, @string = key, string
34
+ super "interpolation argument #{key} missing in #{string.inspect}"
35
+ end
36
+ end
37
+
38
+ class ReservedInterpolationKey < ArgumentError
39
+ attr_reader :key, :string
40
+ def initialize(key, string)
41
+ @key, @string = key, string
42
+ super "reserved key #{key.inspect} used in #{string.inspect}"
43
+ end
44
+ end
45
+
46
+ class UnknownFileType < ArgumentError
47
+ attr_reader :type, :filename
48
+ def initialize(type, filename)
49
+ @type, @filename = type, filename
50
+ super "can not load translations from #{filename}, the file type #{type} is not known"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ dir = File.dirname(__FILE__)
2
+ require dir + '/i18n_test.rb'
3
+ require dir + '/simple_backend_test.rb'
4
+ require dir + '/i18n_exceptions_test.rb'
5
+ # require dir + '/custom_backend_test.rb'
@@ -0,0 +1,100 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nExceptionsTest < Test::Unit::TestCase
10
+ def test_invalid_locale_stores_locale
11
+ force_invalid_locale
12
+ rescue I18n::ArgumentError => e
13
+ assert_nil e.locale
14
+ end
15
+
16
+ def test_invalid_locale_message
17
+ force_invalid_locale
18
+ rescue I18n::ArgumentError => e
19
+ assert_equal 'nil is not a valid locale', e.message
20
+ end
21
+
22
+ def test_missing_translation_data_stores_locale_key_and_options
23
+ force_missing_translation_data
24
+ rescue I18n::ArgumentError => e
25
+ options = {:scope => :bar}
26
+ assert_equal 'de-DE', e.locale
27
+ assert_equal :foo, e.key
28
+ assert_equal options, e.options
29
+ end
30
+
31
+ def test_missing_translation_data_message
32
+ force_missing_translation_data
33
+ rescue I18n::ArgumentError => e
34
+ assert_equal 'translation missing: de-DE, bar, foo', e.message
35
+ end
36
+
37
+ def test_invalid_pluralization_data_stores_entry_and_count
38
+ force_invalid_pluralization_data
39
+ rescue I18n::ArgumentError => e
40
+ assert_equal [:bar], e.entry
41
+ assert_equal 1, e.count
42
+ end
43
+
44
+ def test_invalid_pluralization_data_message
45
+ force_invalid_pluralization_data
46
+ rescue I18n::ArgumentError => e
47
+ assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
48
+ end
49
+
50
+ def test_missing_interpolation_argument_stores_key_and_string
51
+ force_missing_interpolation_argument
52
+ rescue I18n::ArgumentError => e
53
+ assert_equal 'bar', e.key
54
+ assert_equal "{{bar}}", e.string
55
+ end
56
+
57
+ def test_missing_interpolation_argument_message
58
+ force_missing_interpolation_argument
59
+ rescue I18n::ArgumentError => e
60
+ assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
61
+ end
62
+
63
+ def test_reserved_interpolation_key_stores_key_and_string
64
+ force_reserved_interpolation_key
65
+ rescue I18n::ArgumentError => e
66
+ assert_equal 'scope', e.key
67
+ assert_equal "{{scope}}", e.string
68
+ end
69
+
70
+ def test_reserved_interpolation_key_message
71
+ force_reserved_interpolation_key
72
+ rescue I18n::ArgumentError => e
73
+ assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
74
+ end
75
+
76
+ private
77
+ def force_invalid_locale
78
+ I18n.backend.translate nil, :foo
79
+ end
80
+
81
+ def force_missing_translation_data
82
+ I18n.backend.store_translations 'de-DE', :bar => nil
83
+ I18n.backend.translate 'de-DE', :foo, :scope => :bar
84
+ end
85
+
86
+ def force_invalid_pluralization_data
87
+ I18n.backend.store_translations 'de-DE', :foo => [:bar]
88
+ I18n.backend.translate 'de-DE', :foo, :count => 1
89
+ end
90
+
91
+ def force_missing_interpolation_argument
92
+ I18n.backend.store_translations 'de-DE', :foo => "{{bar}}"
93
+ I18n.backend.translate 'de-DE', :foo, :baz => 'baz'
94
+ end
95
+
96
+ def force_reserved_interpolation_key
97
+ I18n.backend.store_translations 'de-DE', :foo => "{{scope}}"
98
+ I18n.backend.translate 'de-DE', :foo, :baz => 'baz'
99
+ end
100
+ end
@@ -0,0 +1,125 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nTest < Test::Unit::TestCase
10
+ def setup
11
+ I18n.backend.store_translations :'en-US', {
12
+ :currency => {
13
+ :format => {
14
+ :separator => '.',
15
+ :delimiter => ',',
16
+ }
17
+ }
18
+ }
19
+ end
20
+
21
+ def test_uses_simple_backend_set_by_default
22
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
23
+ end
24
+
25
+ def test_can_set_backend
26
+ assert_nothing_raised{ I18n.backend = self }
27
+ assert_equal self, I18n.backend
28
+ I18n.backend = I18n::Backend::Simple.new
29
+ end
30
+
31
+ def test_uses_en_us_as_default_locale_by_default
32
+ assert_equal 'en-US', I18n.default_locale
33
+ end
34
+
35
+ def test_can_set_default_locale
36
+ assert_nothing_raised{ I18n.default_locale = 'de-DE' }
37
+ assert_equal 'de-DE', I18n.default_locale
38
+ I18n.default_locale = 'en-US'
39
+ end
40
+
41
+ def test_uses_default_locale_as_locale_by_default
42
+ assert_equal I18n.default_locale, I18n.locale
43
+ end
44
+
45
+ def test_can_set_locale_to_thread_current
46
+ assert_nothing_raised{ I18n.locale = 'de-DE' }
47
+ assert_equal 'de-DE', I18n.locale
48
+ assert_equal 'de-DE', Thread.current[:locale]
49
+ I18n.locale = 'en-US'
50
+ end
51
+
52
+ def test_can_set_exception_handler
53
+ assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
54
+ I18n.exception_handler = :default_exception_handler # revert it
55
+ end
56
+
57
+ def test_uses_custom_exception_handler
58
+ I18n.exception_handler = :custom_exception_handler
59
+ I18n.expects(:custom_exception_handler)
60
+ I18n.translate :bogus
61
+ I18n.exception_handler = :default_exception_handler # revert it
62
+ end
63
+
64
+ def test_delegates_translate_to_backend
65
+ I18n.backend.expects(:translate).with 'de-DE', :foo, {}
66
+ I18n.translate :foo, :locale => 'de-DE'
67
+ end
68
+
69
+ def test_delegates_localize_to_backend
70
+ I18n.backend.expects(:localize).with 'de-DE', :whatever, :default
71
+ I18n.localize :whatever, :locale => 'de-DE'
72
+ end
73
+
74
+ def test_translate_given_no_locale_uses_i18n_locale
75
+ I18n.backend.expects(:translate).with 'en-US', :foo, {}
76
+ I18n.translate :foo
77
+ end
78
+
79
+ def test_translate_on_nested_symbol_keys_works
80
+ assert_equal ".", I18n.t(:'currency.format.separator')
81
+ end
82
+
83
+ def test_translate_with_nested_string_keys_works
84
+ assert_equal ".", I18n.t('currency.format.separator')
85
+ end
86
+
87
+ def test_translate_with_array_as_scope_works
88
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
89
+ end
90
+
91
+ def test_translate_with_array_containing_dot_separated_strings_as_scope_works
92
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
93
+ end
94
+
95
+ def test_translate_with_key_array_and_dot_separated_scope_works
96
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
97
+ end
98
+
99
+ def test_translate_with_dot_separated_key_array_and_scope_works
100
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
101
+ end
102
+
103
+ def test_translate_with_options_using_scope_works
104
+ I18n.backend.expects(:translate).with('de-DE', :precision, :scope => :"currency.format")
105
+ I18n.with_options :locale => 'de-DE', :scope => :'currency.format' do |locale|
106
+ locale.t :precision
107
+ end
108
+ end
109
+
110
+ # def test_translate_given_no_args_raises_missing_translation_data
111
+ # assert_equal "translation missing: en-US, no key", I18n.t
112
+ # end
113
+
114
+ def test_translate_given_a_bogus_key_raises_missing_translation_data
115
+ assert_equal "translation missing: en-US, bogus", I18n.t(:bogus)
116
+ end
117
+
118
+ def test_localize_nil_raises_argument_error
119
+ assert_raises(I18n::ArgumentError) { I18n.l nil }
120
+ end
121
+
122
+ def test_localize_object_raises_argument_error
123
+ assert_raises(I18n::ArgumentError) { I18n.l Object.new }
124
+ end
125
+ end