i18n 0.4.2 → 0.5.0beta1

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.

Files changed (43) hide show
  1. data/CHANGELOG.textile +12 -3
  2. data/lib/i18n.rb +18 -21
  3. data/lib/i18n/backend.rb +0 -1
  4. data/lib/i18n/backend/base.rb +0 -13
  5. data/lib/i18n/backend/cache.rb +0 -2
  6. data/lib/i18n/backend/cascade.rb +6 -10
  7. data/lib/i18n/backend/chain.rb +0 -2
  8. data/lib/i18n/backend/cldr.rb +1 -2
  9. data/lib/i18n/backend/fallbacks.rb +0 -2
  10. data/lib/i18n/backend/gettext.rb +0 -2
  11. data/lib/i18n/backend/interpolation_compiler.rb +1 -3
  12. data/lib/i18n/backend/key_value.rb +1 -3
  13. data/lib/i18n/backend/memoize.rb +1 -3
  14. data/lib/i18n/backend/pluralization.rb +0 -2
  15. data/lib/i18n/backend/simple.rb +0 -2
  16. data/lib/i18n/config.rb +2 -2
  17. data/lib/i18n/core_ext/string/interpolate.rb +1 -3
  18. data/lib/i18n/exceptions.rb +29 -6
  19. data/lib/i18n/gettext.rb +0 -2
  20. data/lib/i18n/gettext/helpers.rb +0 -1
  21. data/lib/i18n/locale/fallbacks.rb +0 -2
  22. data/lib/i18n/locale/tag/parents.rb +0 -2
  23. data/lib/i18n/locale/tag/rfc4646.rb +0 -2
  24. data/lib/i18n/locale/tag/simple.rb +0 -2
  25. data/lib/i18n/tests.rb +12 -0
  26. data/lib/i18n/tests/basics.rb +54 -0
  27. data/lib/i18n/tests/defaults.rb +40 -0
  28. data/lib/i18n/tests/interpolation.rb +133 -0
  29. data/lib/i18n/tests/link.rb +56 -0
  30. data/lib/i18n/tests/localization.rb +19 -0
  31. data/lib/i18n/tests/localization/date.rb +84 -0
  32. data/lib/i18n/tests/localization/date_time.rb +77 -0
  33. data/lib/i18n/tests/localization/procs.rb +103 -0
  34. data/lib/i18n/tests/localization/time.rb +76 -0
  35. data/lib/i18n/tests/lookup.rb +74 -0
  36. data/lib/i18n/tests/pluralization.rb +35 -0
  37. data/lib/i18n/tests/procs.rb +55 -0
  38. data/lib/i18n/version.rb +1 -1
  39. metadata +19 -10
  40. data/lib/i18n/backend/active_record.rb +0 -61
  41. data/lib/i18n/backend/active_record/missing.rb +0 -65
  42. data/lib/i18n/backend/active_record/store_procs.rb +0 -38
  43. data/lib/i18n/backend/active_record/translation.rb +0 -110
data/CHANGELOG.textile CHANGED
@@ -1,5 +1,14 @@
1
1
  h1. Changelog
2
2
 
3
+ h2. 0.5.0
4
+
5
+ * "Extract Backend::ActiveRecord to a separate gem":https://github.com/svenfuchs/i18n/commit/197dacebad356b910d69fa69a719c2ad10cf49e6 (see "i18n-active_record":https://github.com/svenfuchs/i18n-active_record)
6
+ * "Improve exception handling":https://github.com/svenfuchs/i18n/commit/2913ff9a7544f223f60e7d7b32c2a0e1af89812b (deprectates I18n.default_exception_handler)
7
+ * "Change MissingTranslationData message to 'translation missing: foo.bar'":https://github.com/svenfuchs/i18n/commit/68fdfe47952325411afe5942e971ce10b2bdf900
8
+ * "Expose MissingTranslationsData#keys method":https://github.com/svenfuchs/i18n/commit/3a37a389ecaac9670355b334e23e775549ee9822
9
+ * "Improve Cascade#lookup (add default options)":https://github.com/svenfuchs/i18n/commit/0b9a1f2058a2be9543106cc19d08071c359511e1
10
+ * "Finally remove deprecated interpolation syntax":https://github.com/svenfuchs/i18n/commit/2d43846d2b2a2e596f30fa58ea1c9ddb2243bb64
11
+
3
12
  h2. 0.4.2 (2010-10-26)
4
13
 
5
14
  * "Improve UTF8 handling":http://github.com/svenfuchs/i18n/commit/e8d5820a3b08eeca28de1a2b9c8a6ad2b9e6476c
@@ -44,7 +53,7 @@ h2. 0.3.6 (2010-03-23)
44
53
 
45
54
  h2. 0.3.5 (2010-02-26)
46
55
 
47
- * "Delegate I18n.normalize_translation_keys to I18n.normalize_keys and deprecate
56
+ * "Delegate I18n.normalize_translation_keys to I18n.normalize_keys and deprecate
48
57
  the former":http://github.com/svenfuchs/i18n/commit/7284b04d5f5dd9679cb68875515cdd0cdfc96fef
49
58
 
50
59
  h2. 0.3.4 (2010-02-25)
@@ -93,7 +102,7 @@ h2. 0.3.0 (2009-11-30)
93
102
  h2. 0.2.0 (2009-07-12)
94
103
 
95
104
  * "Allow using Ruby 1.9 syntax for string interpolation (API addition)":http://github.com/svenfuchs/i18n/commit/c6e0b06d512f2af57199a843a1d8a40241b32861
96
- * "Allow configuring the default scope separator, allow to pass a custom scope separator(API addition)":http://github.com/svenfuchs/i18n/commit/5b75bfbc348061adc11e3790187a187275bfd471 (e.g. I18n.t(:'foo|bar', :separator => '|')
105
+ * "Allow configuring the default scope separator, allow to pass a custom scope separator(API addition)":http://github.com/svenfuchs/i18n/commit/5b75bfbc348061adc11e3790187a187275bfd471 (e.g. I18n.t(:'foo|bar', :separator => '|')
97
106
  * "Pass :format option to #translate for #localize more useful lambda support":http://github.com/svenfuchs/i18n/commit/e277711b3c844fe7589b8d3f9af0f7d1b969a273
98
107
  * "Refactor Simple backend #resolve to #default and #resolve for more consistency. Now allows to pass lambdas as defaults and re-resolve Symbols":http://github.com/svenfuchs/i18n/commit/8c4ce3d923ce5fa73e973fe28217e18165549aba
99
108
  * "Add lambda support to #translate (API addition)":http://github.com/svenfuchs/i18n/commit/c90e62d8f7d3d5b78f34cfe328d871b58884f115
@@ -140,4 +149,4 @@ h2. More information
140
149
  * "Wiki":http://rails-i18n.org/wiki
141
150
  * "Mailinglist":http://groups.google.com/group/rails-i18n
142
151
  * "About the project/history":http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized
143
- * "Initial API Intro":http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
152
+ * "Initial API Intro":http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
data/lib/i18n.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # Authors:: Sven Fuchs (http://www.artweb-design.de),
4
2
  # Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
5
3
  # Stephan Soller (http://www.arkanis-development.de/),
@@ -16,6 +14,7 @@ module I18n
16
14
  autoload :Config, 'i18n/config'
17
15
  autoload :Gettext, 'i18n/gettext'
18
16
  autoload :Locale, 'i18n/locale'
17
+ autoload :Tests, 'i18n/tests'
19
18
 
20
19
  class << self
21
20
  # Gets I18n configuration object.
@@ -252,7 +251,6 @@ module I18n
252
251
  self.locale = current_locale if tmp_locale
253
252
  end
254
253
 
255
-
256
254
  # Merges the given locale, key and scope into a single array of keys.
257
255
  # Splits keys that contain dots into multiple keys. Makes sure all
258
256
  # keys are Symbols.
@@ -270,15 +268,6 @@ module I18n
270
268
  # see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
271
269
  private
272
270
 
273
- # Handles exceptions raised in the backend. All exceptions except for
274
- # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
275
- # was caught and the option :raise is not set the handler returns an error
276
- # message string containing the key/scope.
277
- def default_exception_handler(exception, locale, key, options)
278
- return exception.message if MissingTranslationData === exception
279
- raise exception
280
- end
281
-
282
271
  # Any exceptions thrown in translate will be sent to the @@exception_handler
283
272
  # which can be a Symbol, a Proc or any other Object.
284
273
  #
@@ -297,20 +286,14 @@ module I18n
297
286
  # I18n.exception_handler = I18nExceptionHandler.new # an object
298
287
  # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
299
288
  def handle_exception(exception, locale, key, options)
300
- case config.exception_handler
289
+ case handler = options[:exception_handler] || config.exception_handler
301
290
  when Symbol
302
- send(config.exception_handler, exception, locale, key, options)
291
+ send(handler, exception, locale, key, options)
303
292
  else
304
- config.exception_handler.call(exception, locale, key, options)
293
+ handler.call(exception, locale, key, options)
305
294
  end
306
295
  end
307
296
 
308
- # Deprecated. Will raise a warning in future versions and then finally be
309
- # removed. Use I18n.normalize_keys instead.
310
- def normalize_translation_keys(locale, key, scope, separator = nil)
311
- normalize_keys(locale, key, scope, separator)
312
- end
313
-
314
297
  def normalize_key(key, separator)
315
298
  normalized_key_cache[separator][key] ||=
316
299
  case key
@@ -327,5 +310,19 @@ module I18n
327
310
  def normalized_key_cache
328
311
  @normalized_key_cache ||= Hash.new { |h,k| h[k] = {} }
329
312
  end
313
+
314
+ # DEPRECATED. Use I18n.normalize_keys instead.
315
+ def normalize_translation_keys(locale, key, scope, separator = nil)
316
+ puts "I18n.normalize_translation_keys is deprecated. Please use the class I18n.normalize_keys instead."
317
+ normalize_keys(locale, key, scope, separator)
318
+ end
319
+
320
+ # DEPRECATED. Please use the I18n::ExceptionHandler class instead.
321
+ def default_exception_handler(exception, locale, key, options)
322
+ puts "I18n.default_exception_handler is deprecated. Please use the class I18n::ExceptionHandler instead " +
323
+ "(an instance of which is set to I18n.exception_handler by default)."
324
+ return exception.message if MissingTranslationData === exception
325
+ raise exception
326
+ end
330
327
  end
331
328
  end
data/lib/i18n/backend.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module I18n
2
2
  module Backend
3
- autoload :ActiveRecord, 'i18n/backend/active_record'
4
3
  autoload :Base, 'i18n/backend/base'
5
4
  autoload :InterpolationCompiler, 'i18n/backend/interpolation_compiler'
6
5
  autoload :Cache, 'i18n/backend/cache'
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'yaml'
4
2
  require 'i18n/core_ext/hash'
5
3
 
@@ -10,7 +8,6 @@ module I18n
10
8
 
11
9
  RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback]
12
10
  RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/
13
- DEPRECATED_INTERPOLATION_SYNTAX_PATTERN = /(\\)?\{\{([^\}]+)\}\}/
14
11
 
15
12
  # Accepts a list of paths to translation files. Loads translations from
16
13
  # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
@@ -150,16 +147,6 @@ module I18n
150
147
  def interpolate(locale, string, values = {})
151
148
  return string unless string.is_a?(::String) && !values.empty?
152
149
 
153
- string = string.gsub(DEPRECATED_INTERPOLATION_SYNTAX_PATTERN) do
154
- escaped, key = $1, $2.to_sym
155
- if escaped
156
- "{{#{key}}}"
157
- else
158
- warn_syntax_deprecation!(locale, string)
159
- "%{#{key}}"
160
- end
161
- end
162
-
163
150
  values.each do |key, value|
164
151
  value = value.call(values) if interpolate_lambda?(value, string, key)
165
152
  value = value.to_s unless value.is_a?(::String)
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # This module allows you to easily cache all responses from the backend - thus
4
2
  # speeding up the I18n aspects of your application quite a bit.
5
3
  #
@@ -1,7 +1,3 @@
1
- # encoding: utf-8
2
-
3
- # EXPERIMENTAL
4
- #
5
1
  # The Cascade module adds the ability to do cascading lookups to backends that
6
2
  # are compatible to the Simple backend.
7
3
  #
@@ -38,19 +34,19 @@ module I18n
38
34
  def lookup(locale, key, scope = [], options = {})
39
35
  return super unless cascade = options[:cascade]
40
36
 
37
+ cascade = { :step => 1 } unless cascade.is_a?(Hash)
38
+ step = cascade[:step] || 1
39
+ offset = cascade[:offset] || 1
41
40
  separator = options[:separator] || I18n.default_separator
42
41
  skip_root = cascade.has_key?(:skip_root) ? cascade[:skip_root] : true
43
- step = cascade[:step]
44
42
 
45
- keys = I18n.normalize_keys(nil, key, nil, separator)
46
- offset = options[:cascade][:offset] || keys.length
47
- scope = I18n.normalize_keys(nil, nil, scope, separator) + keys
48
- key = scope.slice!(-offset, offset).join(separator)
43
+ scope = I18n.normalize_keys(nil, key, scope, separator)
44
+ key = (scope.slice!(-offset, offset) || []).join(separator)
49
45
 
50
46
  begin
51
47
  result = super
52
48
  return result unless result.nil?
53
- end while !scope.empty? && scope.slice!(-step, step) && (!scope.empty? || !skip_root)
49
+ end while (!scope.empty? || !skip_root) && scope.slice!(-step, step)
54
50
  end
55
51
  end
56
52
  end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module I18n
4
2
  module Backend
5
3
  # Backend that chains multiple other backends and checks each of them when
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'cldr'
3
2
 
4
3
  module I18n
@@ -97,4 +96,4 @@ module I18n
97
96
  end
98
97
  end
99
98
  end
100
- end
99
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # I18n locale fallbacks are useful when you want your application to use
4
2
  # translations from other locales when translations for the current locale are
5
3
  # missing. E.g. you might want to use :en translations when translations in
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'i18n/gettext'
4
2
  require 'i18n/gettext/po_parser'
5
3
 
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # The InterpolationCompiler module contains optimizations that can tremendously
4
2
  # speed up the interpolation process on the Simple backend.
5
3
  #
@@ -120,4 +118,4 @@ module I18n
120
118
  end
121
119
  end
122
120
  end
123
- end
121
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'i18n/backend/base'
4
2
  require 'active_support/json'
5
3
 
@@ -99,4 +97,4 @@ module I18n
99
97
  include Implementation
100
98
  end
101
99
  end
102
- end
100
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
- #
3
1
  # Memoize module simply memoizes the values returned by lookup using
4
2
  # a flat hash and can tremendously speed up the lookup process in a backend.
5
3
  #
@@ -45,4 +43,4 @@ module I18n
45
43
  end
46
44
  end
47
45
  end
48
- end
46
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # I18n locale fallbacks are useful when you want your application to use
4
2
  # translations from other locales when translations for the current locale are
5
3
  # missing. E.g. you might want to use :en translations when translations in
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module I18n
4
2
  module Backend
5
3
  # A simple backend that reads translations from YAML files and stores them in
data/lib/i18n/config.rb CHANGED
@@ -57,7 +57,7 @@ module I18n
57
57
 
58
58
  # Return the current exception handler. Defaults to :default_exception_handler.
59
59
  def exception_handler
60
- @@exception_handler ||= :default_exception_handler
60
+ @@exception_handler ||= ExceptionHandler.new
61
61
  end
62
62
 
63
63
  # Sets the exception handler.
@@ -83,4 +83,4 @@ module I18n
83
83
  @@load_path = load_path
84
84
  end
85
85
  end
86
- end
86
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  =begin
4
2
  heavily based on Masao Mutoh's gettext String interpolation extension
5
3
  http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
@@ -95,4 +93,4 @@ rescue ArgumentError
95
93
  end
96
94
  end
97
95
  end
98
- end
96
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  class KeyError < IndexError
4
2
  def initialize(message = nil)
5
3
  super(message || "key not found")
@@ -7,6 +5,22 @@ class KeyError < IndexError
7
5
  end unless defined?(KeyError)
8
6
 
9
7
  module I18n
8
+ # Handles exceptions raised in the backend. All exceptions except for
9
+ # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
10
+ # was caught the handler returns an error message string containing the key/scope.
11
+ # Note that the exception handler is not called when the option :raise was given.
12
+ class ExceptionHandler
13
+ include Module.new {
14
+ def call(exception, locale, key, options)
15
+ if exception.is_a?(MissingTranslationData)
16
+ options[:rescue_format] == :html ? exception.html_message : exception.message
17
+ else
18
+ raise exception
19
+ end
20
+ end
21
+ }
22
+ end
23
+
10
24
  class ArgumentError < ::ArgumentError; end
11
25
 
12
26
  class InvalidLocale < ArgumentError
@@ -27,13 +41,22 @@ module I18n
27
41
 
28
42
  class MissingTranslationData < ArgumentError
29
43
  attr_reader :locale, :key, :options
44
+
30
45
  def initialize(locale, key, opts = nil)
31
46
  @key, @locale, @options = key, locale, opts.dup || {}
32
47
  options.each { |k, v| options[k] = v.inspect if v.is_a?(Proc) }
48
+ super "translation missing: #{keys.join('.')}"
49
+ end
50
+
51
+ def html_message
52
+ key = keys.last.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
53
+ %(<span class="translation_missing" title="translation missing: #{keys.join('.')}">#{key}</span>)
54
+ end
33
55
 
34
- keys = I18n.normalize_keys(locale, key, options[:scope])
35
- keys << 'no key' if keys.size < 2
36
- super "translation missing: #{keys.join(', ')}"
56
+ def keys
57
+ @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
58
+ keys << 'no key' if keys.size < 2
59
+ end
37
60
  end
38
61
  end
39
62
 
@@ -68,4 +91,4 @@ module I18n
68
91
  super "can not load translations from #{filename}, the file type #{type} is not known"
69
92
  end
70
93
  end
71
- end
94
+ end
data/lib/i18n/gettext.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module I18n
4
2
  module Gettext
5
3
  PLURAL_SEPARATOR = "\001"
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'i18n/gettext'
3
2
 
4
3
  module I18n
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # Locale Fallbacks
4
2
  #
5
3
  # Extends the I18n module to hold a fallbacks instance which is set to an
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module I18n
4
2
  module Locale
5
3
  module Tag
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # RFC 4646/47 compliant Locale tag implementation that parses locale tags to
4
2
  # subtags such as language, script, region, variant etc.
5
3
  #
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # Simple Locale tag implementation that computes subtags by simply splitting
4
2
  # the locale tag at '-' occurences.
5
3
  module I18n
data/lib/i18n/tests.rb ADDED
@@ -0,0 +1,12 @@
1
+ module I18n
2
+ module Tests
3
+ autoload :Basics, 'i18n/tests/basics'
4
+ autoload :Defaults, 'i18n/tests/defaults'
5
+ autoload :Interpolation, 'i18n/tests/interpolation'
6
+ autoload :Link, 'i18n/tests/link'
7
+ autoload :Localization, 'i18n/tests/localization'
8
+ autoload :Lookup, 'i18n/tests/lookup'
9
+ autoload :Pluralization, 'i18n/tests/pluralization'
10
+ autoload :Procs, 'i18n/tests/procs'
11
+ end
12
+ end
@@ -0,0 +1,54 @@
1
+ module I18n
2
+ module Tests
3
+ module Basics
4
+ def teardown
5
+ I18n.available_locales = nil
6
+ end
7
+
8
+ test "available_locales returns the locales stored to the backend by default" do
9
+ I18n.backend.store_translations('de', :foo => 'bar')
10
+ I18n.backend.store_translations('en', :foo => 'foo')
11
+
12
+ assert I18n.available_locales.include?(:de)
13
+ assert I18n.available_locales.include?(:en)
14
+ end
15
+
16
+ test "available_locales can be set to something else independently from the actual locale data" do
17
+ I18n.backend.store_translations('de', :foo => 'bar')
18
+ I18n.backend.store_translations('en', :foo => 'foo')
19
+
20
+ I18n.available_locales = :foo
21
+ assert_equal [:foo], I18n.available_locales
22
+
23
+ I18n.available_locales = [:foo, 'bar']
24
+ assert_equal [:foo, :bar], I18n.available_locales
25
+
26
+ I18n.available_locales = nil
27
+ assert I18n.available_locales.include?(:de)
28
+ assert I18n.available_locales.include?(:en)
29
+ end
30
+
31
+ test "available_locales memoizes when set explicitely" do
32
+ I18n.backend.expects(:available_locales).never
33
+ I18n.available_locales = [:foo]
34
+ I18n.backend.store_translations('de', :bar => 'baz')
35
+ I18n.reload!
36
+ assert_equal [:foo], I18n.available_locales
37
+ end
38
+
39
+ test "available_locales delegates to the backend when not set explicitely" do
40
+ I18n.backend.expects(:available_locales).twice
41
+ assert_equal I18n.available_locales, I18n.available_locales
42
+ end
43
+
44
+ test "storing a nil value as a translation removes it from the available locale data" do
45
+ I18n.backend.store_translations(:en, :to_be_deleted => 'bar')
46
+ assert_equal 'bar', I18n.t(:to_be_deleted, :default => 'baz')
47
+
48
+ I18n.cache_store.clear if I18n.respond_to?(:cache_store) && I18n.cache_store
49
+ I18n.backend.store_translations(:en, :to_be_deleted => nil)
50
+ assert_equal 'baz', I18n.t(:to_be_deleted, :default => 'baz')
51
+ end
52
+ end
53
+ end
54
+ end