i18n 0.5.4 → 0.6.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.

@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'mocha'
4
+ gem 'test_declarative'
5
+
@@ -0,0 +1,14 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ mocha (0.9.9)
5
+ rake
6
+ rake (0.8.7)
7
+ test_declarative (0.0.4)
8
+
9
+ PLATFORMS
10
+ ruby
11
+
12
+ DEPENDENCIES
13
+ mocha
14
+ test_declarative
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '~> 2.3'
4
+ gem 'sqlite3-ruby'
5
+ gem 'mocha'
6
+ gem 'test_declarative'
7
+ gem 'rufus-tokyo'
8
+ gem 'ffi'
9
+
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (2.3.10)
5
+ ffi (0.6.3)
6
+ rake (>= 0.8.7)
7
+ mocha (0.9.9)
8
+ rake
9
+ rake (0.8.7)
10
+ rufus-tokyo (1.0.7)
11
+ sqlite3-ruby (1.3.2)
12
+ test_declarative (0.0.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ activesupport (~> 2.3)
19
+ ffi
20
+ mocha
21
+ rufus-tokyo
22
+ sqlite3-ruby
23
+ test_declarative
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '~> 3.0.0'
4
+ gem 'sqlite3-ruby'
5
+ gem 'mocha'
6
+ gem 'test_declarative'
7
+ gem 'rufus-tokyo'
8
+ gem 'ffi'
9
+
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.3)
5
+ ffi (0.6.3)
6
+ rake (>= 0.8.7)
7
+ mocha (0.9.9)
8
+ rake
9
+ rake (0.8.7)
10
+ rufus-tokyo (1.0.7)
11
+ sqlite3-ruby (1.3.2)
12
+ test_declarative (0.0.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ activesupport (~> 3.0.0)
19
+ ffi
20
+ mocha
21
+ rufus-tokyo
22
+ sqlite3-ruby
23
+ test_declarative
@@ -1,6 +1,6 @@
1
1
  require 'i18n/version'
2
2
  require 'i18n/exceptions'
3
- require 'i18n/interpolate/ruby.rb'
3
+ require 'i18n/interpolate/ruby'
4
4
 
5
5
  module I18n
6
6
  autoload :Backend, 'i18n/backend'
@@ -9,7 +9,7 @@ module I18n
9
9
  autoload :Locale, 'i18n/locale'
10
10
  autoload :Tests, 'i18n/tests'
11
11
 
12
- RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :raise, :rescue_format]
12
+ RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :rescue_format]
13
13
  RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/
14
14
 
15
15
  class << self
@@ -25,7 +25,7 @@ module I18n
25
25
 
26
26
  # Write methods which delegates to the configuration object
27
27
  %w(locale backend default_locale available_locales default_separator
28
- exception_handler load_path enforce_available_locales).each do |method|
28
+ exception_handler load_path).each do |method|
29
29
  module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
30
30
  def #{method}
31
31
  config.#{method}
@@ -141,23 +141,22 @@ module I18n
141
141
  # always return the same translations/values per unique combination of argument
142
142
  # values.
143
143
  def translate(*args)
144
- options = args.last.is_a?(Hash) ? args.pop : {}
145
- key = args.shift
146
- backend = config.backend
147
- locale = options.delete(:locale) || config.locale
148
- raises = options.delete(:raise)
144
+ options = args.last.is_a?(Hash) ? args.pop : {}
145
+ key = args.shift
146
+ backend = config.backend
147
+ locale = options.delete(:locale) || config.locale
148
+ handling = options.delete(:throw) && :throw || options.delete(:raise) && :raise # TODO deprecate :raise
149
149
 
150
- enforce_available_locales!(locale)
151
150
  raise I18n::ArgumentError if key.is_a?(String) && key.empty?
152
151
 
153
- if key.is_a?(Array)
154
- key.map { |k| backend.translate(locale, k, options) }
155
- else
156
- backend.translate(locale, key, options)
152
+ result = catch(:exception) do
153
+ if key.is_a?(Array)
154
+ key.map { |k| backend.translate(locale, k, options) }
155
+ else
156
+ backend.translate(locale, key, options)
157
+ end
157
158
  end
158
- rescue I18n::ArgumentError => exception
159
- raise exception if raises
160
- handle_exception(exception, locale, key, options)
159
+ result.is_a?(MissingTranslation) ? handle_exception(handling, result, locale, key, options) : result
161
160
  end
162
161
  alias :t :translate
163
162
 
@@ -223,7 +222,6 @@ module I18n
223
222
  locale = options && options.delete(:locale) || config.locale
224
223
  raises = options && options.delete(:raise)
225
224
  replacement = options && options.delete(:replacement)
226
- enforce_available_locales!(locale)
227
225
  config.backend.transliterate(locale, key, replacement)
228
226
  rescue I18n::ArgumentError => exception
229
227
  raise exception if raises
@@ -234,7 +232,6 @@ module I18n
234
232
  def localize(object, options = {})
235
233
  locale = options.delete(:locale) || config.locale
236
234
  format = options.delete(:format) || :default
237
- enforce_available_locales!(locale)
238
235
  config.backend.localize(locale, object, format, options)
239
236
  end
240
237
  alias :l :localize
@@ -263,30 +260,13 @@ module I18n
263
260
  keys
264
261
  end
265
262
 
266
- # Returns true when the passed locale is in I18.available_locales.
267
- # Returns false otherwise.
268
- # Compare with Strings as `locale` may be coming from user input
269
- def locale_available?(locale)
270
- I18n.available_locales.map(&:to_s).include?(locale.to_s)
271
- end
272
-
273
- # Raises an InvalidLocale exception when the passed locale is not
274
- # included in I18n.available_locales.
275
- # Returns false otherwise
276
- def enforce_available_locales!(locale)
277
- handle_enforce_available_locales_deprecation
278
-
279
- if config.enforce_available_locales
280
- raise I18n::InvalidLocale.new(locale) if !locale_available?(locale)
281
- end
282
- end
283
-
284
263
  # making these private until Ruby 1.9.2 can send to protected methods again
285
264
  # see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
286
265
  private
287
266
 
288
267
  # Any exceptions thrown in translate will be sent to the @@exception_handler
289
- # which can be a Symbol, a Proc or any other Object.
268
+ # which can be a Symbol, a Proc or any other Object unless they're forced to
269
+ # be raised or thrown (MissingTranslation).
290
270
  #
291
271
  # If exception_handler is a Symbol then it will simply be sent to I18n as
292
272
  # a method call. A Proc will simply be called. In any other case the
@@ -302,19 +282,19 @@ module I18n
302
282
  #
303
283
  # I18n.exception_handler = I18nExceptionHandler.new # an object
304
284
  # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
305
- def handle_exception(exception, locale, key, options)
306
- case handler = options[:exception_handler] || config.exception_handler
307
- when Symbol
308
- send(handler, exception, locale, key, options)
285
+ def handle_exception(handling, exception, locale, key, options)
286
+ case handling
287
+ when :raise
288
+ raise(exception.respond_to?(:to_exception) ? exception.to_exception : exception)
289
+ when :throw
290
+ throw :exception, exception
309
291
  else
310
- handler.call(exception, locale, key, options)
311
- end
312
- end
313
-
314
- def handle_enforce_available_locales_deprecation
315
- if config.enforce_available_locales.nil? && !defined?(@unenforced_available_locales_deprecation)
316
- $stderr.puts "[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message."
317
- @unenforced_available_locales_deprecation = true
292
+ case handler = options[:exception_handler] || config.exception_handler
293
+ when Symbol
294
+ send(handler, exception, locale, key, options)
295
+ else
296
+ handler.call(exception, locale, key, options)
297
+ end
318
298
  end
319
299
  end
320
300
 
@@ -326,7 +306,7 @@ module I18n
326
306
  else
327
307
  keys = key.to_s.split(separator)
328
308
  keys.delete('')
329
- keys.map!{ |k| k.to_sym }
309
+ keys.map! { |k| k.to_sym }
330
310
  keys
331
311
  end
332
312
  end
@@ -345,8 +325,7 @@ module I18n
345
325
  def default_exception_handler(exception, locale, key, options)
346
326
  puts "I18n.default_exception_handler is deprecated. Please use the class I18n::ExceptionHandler instead " +
347
327
  "(an instance of which is set to I18n.exception_handler by default)."
348
- return exception.message if MissingTranslationData === exception
349
- raise exception
328
+ exception.is_a?(MissingTranslation) ? exception.message : raise(exception)
350
329
  end
351
330
  end
352
331
  end
@@ -11,8 +11,8 @@ module I18n
11
11
  # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
12
12
  # for details.
13
13
  def load_translations(*filenames)
14
- filenames = I18n.load_path.flatten if filenames.empty?
15
- filenames.each { |filename| load_file(filename) }
14
+ filenames = I18n.load_path if filenames.empty?
15
+ filenames.flatten.each { |filename| load_file(filename) }
16
16
  end
17
17
 
18
18
  # This method receives a locale, a data hash and options for storing translations.
@@ -34,7 +34,7 @@ module I18n
34
34
  default(locale, key, default, options) : resolve(locale, key, entry, options)
35
35
  end
36
36
 
37
- raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
37
+ throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.nil?
38
38
  entry = entry.dup if entry.is_a?(String)
39
39
 
40
40
  entry = pluralize(locale, entry, count) if count
@@ -108,17 +108,18 @@ module I18n
108
108
  # subjects will be returned directly.
109
109
  def resolve(locale, object, subject, options = {})
110
110
  return subject if options[:resolve] == false
111
- case subject
112
- when Symbol
113
- I18n.translate(subject, options.merge(:locale => locale, :raise => true))
114
- when Proc
115
- date_or_time = options.delete(:object) || object
116
- resolve(locale, object, subject.call(date_or_time, options))
117
- else
118
- subject
111
+ result = catch(:exception) do
112
+ case subject
113
+ when Symbol
114
+ I18n.translate(subject, options.merge(:locale => locale, :throw => true))
115
+ when Proc
116
+ date_or_time = options.delete(:object) || object
117
+ resolve(locale, object, subject.call(date_or_time, options))
118
+ else
119
+ subject
120
+ end
119
121
  end
120
- rescue MissingTranslationData
121
- nil
122
+ result unless result.is_a?(MissingTranslation)
122
123
  end
123
124
 
124
125
  # Picks a translation from an array according to English pluralization
@@ -4,7 +4,7 @@
4
4
  # To enable caching you can simply include the Cache module to the Simple
5
5
  # backend - or whatever other backend you are using:
6
6
  #
7
- # I18n::Backend::Simple.send(:include, I18n::Backend::Cache)
7
+ # I18n::Backend::Simple.include(I18n::Backend::Cache)
8
8
  #
9
9
  # You will also need to set a cache store implementation that you want to use:
10
10
  #
@@ -69,23 +69,17 @@ module I18n
69
69
  protected
70
70
 
71
71
  def fetch(cache_key, &block)
72
- result = fetch_storing_missing_translation_exception(cache_key, &block)
73
- raise result if result.is_a?(Exception)
72
+ result = _fetch(cache_key, &block)
73
+ throw(:exception, result) if result.is_a?(MissingTranslation)
74
74
  result = result.dup if result.frozen? rescue result
75
75
  result
76
76
  end
77
77
 
78
- def fetch_storing_missing_translation_exception(cache_key, &block)
79
- fetch_ignoring_procs(cache_key, &block)
80
- rescue MissingTranslationData => exception
81
- I18n.cache_store.write(cache_key, exception)
82
- exception
83
- end
84
-
85
- def fetch_ignoring_procs(cache_key, &block)
86
- I18n.cache_store.read(cache_key) || yield.tap do |result|
87
- I18n.cache_store.write(cache_key, result) unless result.is_a?(Proc)
88
- end
78
+ def _fetch(cache_key, &block)
79
+ result = I18n.cache_store.read(cache_key) and return result
80
+ result = catch(:exception, &block)
81
+ I18n.cache_store.write(cache_key, result) unless result.is_a?(Proc)
82
+ result
89
83
  end
90
84
 
91
85
  def cache_key(locale, key, options)
@@ -29,7 +29,7 @@ module I18n
29
29
  end
30
30
 
31
31
  def store_translations(locale, data, options = {})
32
- backends.first.store_translations(locale, data, options = {})
32
+ backends.first.store_translations(locale, data, options)
33
33
  end
34
34
 
35
35
  def available_locales
@@ -41,7 +41,7 @@ module I18n
41
41
  options = default_options.except(:default)
42
42
 
43
43
  backends.each do |backend|
44
- begin
44
+ catch(:exception) do
45
45
  options = default_options if backend == backends.last
46
46
  translation = backend.translate(locale, key, options)
47
47
  if namespace_lookup?(translation, options)
@@ -50,22 +50,20 @@ module I18n
50
50
  elsif !translation.nil?
51
51
  return translation
52
52
  end
53
- rescue MissingTranslationData
54
53
  end
55
54
  end
56
55
 
57
56
  return namespace if namespace
58
- raise(I18n::MissingTranslationData.new(locale, key, options))
57
+ throw(:exception, I18n::MissingTranslation.new(locale, key, options))
59
58
  end
60
59
 
61
60
  def localize(locale, object, format = :default, options = {})
62
61
  backends.each do |backend|
63
- begin
62
+ catch(:exception) do
64
63
  result = backend.localize(locale, object, format, options) and return result
65
- rescue MissingTranslationData
66
64
  end
67
65
  end
68
- raise(I18n::MissingTranslationData.new(locale, format, options))
66
+ throw(:exception, I18n::MissingTranslation.new(locale, format, options))
69
67
  end
70
68
 
71
69
  protected
@@ -6,7 +6,7 @@
6
6
  # To enable locale fallbacks you can simply include the Fallbacks module to
7
7
  # the Simple backend - or whatever other backend you are using:
8
8
  #
9
- # I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
9
+ # I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
10
10
  module I18n
11
11
  @@fallbacks = nil
12
12
 
@@ -29,8 +29,7 @@ module I18n
29
29
  # locale :"de-DE" it might try the locales :"de-DE", :de and :en
30
30
  # (depends on the fallbacks implementation) until it finds a result with
31
31
  # the given options. If it does not find any result for any of the
32
- # locales it will then raise a MissingTranslationData exception as
33
- # usual.
32
+ # locales it will then throw MissingTranslation as usual.
34
33
  #
35
34
  # The default option takes precedence over fallback locales
36
35
  # only when it's a Symbol. When the default contains a String or a Proc
@@ -41,20 +40,19 @@ module I18n
41
40
 
42
41
  options[:fallback] = true
43
42
  I18n.fallbacks[locale].each do |fallback|
44
- begin
43
+ catch(:exception) do
45
44
  result = super(fallback, key, options)
46
45
  return result unless result.nil?
47
- rescue I18n::MissingTranslationData
48
46
  end
49
47
  end
50
48
  options.delete(:fallback)
51
49
 
52
50
  return super(locale, nil, options.merge(:default => default)) if default
53
- raise(I18n::MissingTranslationData.new(locale, key, options))
51
+ throw(:exception, I18n::MissingTranslation.new(locale, key, options))
54
52
  end
55
53
 
56
54
  def extract_string_or_lambda_default!(options)
57
- defaults = Array(options[:default])
55
+ defaults = [options[:default]].flatten
58
56
  if index = find_first_string_or_lambda_default(defaults)
59
57
  options[:default] = defaults[0, index]
60
58
  defaults[index]
@@ -6,7 +6,7 @@ require 'i18n/gettext/po_parser'
6
6
  # To use this you can simply include the module to the Simple backend - or
7
7
  # whatever other backend you are using.
8
8
  #
9
- # I18n::Backend::Simple.send(:include, I18n::Backend::Gettext)
9
+ # I18n::Backend::Simple.include(I18n::Backend::Gettext)
10
10
  #
11
11
  # Now you should be able to include your Gettext translation (*.po) files to
12
12
  # the I18n.load_path so they're loaded to the backend and you can use them as
@@ -8,7 +8,7 @@
8
8
  # To enable pre-compiled interpolations you can simply include the
9
9
  # InterpolationCompiler module to the Simple backend:
10
10
  #
11
- # I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationCompiler)
11
+ # I18n::Backend::Simple.include(I18n::Backend::InterpolationCompiler)
12
12
  #
13
13
  # Note that InterpolationCompiler does not yield meaningful results and consequently
14
14
  # should not be used with Ruby 1.9 (YARV) but improves performance everywhere else
@@ -1,5 +1,6 @@
1
1
  require 'i18n/backend/base'
2
2
  require 'active_support/json'
3
+ require 'active_support/ordered_hash' # active_support/json/encoding uses ActiveSupport::OrderedHash but does not require it
3
4
 
4
5
  module I18n
5
6
  module Backend
@@ -3,7 +3,7 @@
3
3
  #
4
4
  # To enable it you can simply include the Memoize module to your backend:
5
5
  #
6
- # I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
6
+ # I18n::Backend::Simple.include(I18n::Backend::Memoize)
7
7
  #
8
8
  # Notice that it's the responsibility of the backend to define whenever the
9
9
  # cache should be cleaned.
@@ -12,7 +12,7 @@
12
12
  # To enable translation metadata you can simply include the Metadata module
13
13
  # into the Simple backend class - or whatever other backend you are using:
14
14
  #
15
- # I18n::Backend::Simple.send(:include, I18n::Backend::Metadata)
15
+ # I18n::Backend::Simple.include(I18n::Backend::Metadata)
16
16
  #
17
17
  module I18n
18
18
  module Backend
@@ -7,7 +7,7 @@
7
7
  # Pluralization module to the Simple backend - or whatever other backend you
8
8
  # are using.
9
9
  #
10
- # I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
10
+ # I18n::Backend::Simple.include(I18n::Backend::Pluralization)
11
11
  #
12
12
  # You also need to make sure to provide pluralization algorithms to the
13
13
  # backend, i.e. include them to your I18n.load_path accordingly.
@@ -19,7 +19,7 @@ module I18n
19
19
  # rule and use it to pluralize the given entry. I.e. the library expects
20
20
  # pluralization rules to be stored at I18n.t(:'i18n.plural.rule')
21
21
  #
22
- # Pluralization rules are expected to respond to #call(entry, count) and
22
+ # Pluralization rules are expected to respond to #call(count) and
23
23
  # return a pluralization key. Valid keys depend on the translation data
24
24
  # hash (entry) but it is generally recommended to follow CLDR's style,
25
25
  # i.e., return one of the keys :zero, :one, :few, :many, :other.
@@ -13,8 +13,10 @@ module I18n
13
13
  # end
14
14
  # end
15
15
  #
16
- # I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
16
+ # I18n::Backend::Simple.include(I18n::Backend::Pluralization)
17
17
  class Simple
18
+ (class << self; self; end).class_eval { public :include }
19
+
18
20
  module Implementation
19
21
  include Base
20
22
 
@@ -8,7 +8,6 @@ module I18n
8
8
 
9
9
  # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
10
10
  def locale=(locale)
11
- I18n.enforce_available_locales!(locale)
12
11
  @locale = locale.to_sym rescue nil
13
12
  end
14
13
 
@@ -29,7 +28,6 @@ module I18n
29
28
 
30
29
  # Sets the current default locale. Used to set a custom default locale.
31
30
  def default_locale=(locale)
32
- I18n.enforce_available_locales!(locale)
33
31
  @@default_locale = locale.to_sym rescue nil
34
32
  end
35
33
 
@@ -84,15 +82,5 @@ module I18n
84
82
  def load_path=(load_path)
85
83
  @@load_path = load_path
86
84
  end
87
-
88
- # [Deprecated] this will default to true in the future
89
- # Defaults to nil so that it triggers the deprecation warning
90
- def enforce_available_locales
91
- defined?(@@enforce_available_locales) ? @@enforce_available_locales : nil
92
- end
93
-
94
- def enforce_available_locales=(enforce_available_locales)
95
- @@enforce_available_locales = enforce_available_locales
96
- end
97
85
  end
98
86
  end
@@ -1,29 +1,17 @@
1
- require 'cgi'
2
-
3
1
  module I18n
4
2
  # Handles exceptions raised in the backend. All exceptions except for
5
- # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
3
+ # MissingTranslationData exceptions are re-thrown. When a MissingTranslationData
6
4
  # was caught the handler returns an error message string containing the key/scope.
7
- # Note that the exception handler is not called when the option :raise was given.
5
+ # Note that the exception handler is not called when the option :throw was given.
8
6
  class ExceptionHandler
9
7
  include Module.new {
10
8
  def call(exception, locale, key, options)
11
- if exception.is_a?(MissingTranslationData)
12
- #
13
- # TODO: this block is to be replaced by `exception.message` when
14
- # rescue_format is removed
15
- if options[:rescue_format] == :html
16
- if !defined?(@rescue_format_deprecation)
17
- $stderr.puts "[DEPRECATED] I18n's :recue_format option will be removed from a future release. All exception messages will be plain text. If you need the exception handler to return an html format please set or pass a custom exception handler."
18
- @rescue_format_deprecation = true
19
- end
20
- exception.html_message
21
- else
22
- exception.message
23
- end
24
-
25
- else
9
+ if exception.is_a?(MissingTranslation)
10
+ options[:rescue_format] == :html ? exception.html_message : exception.message
11
+ elsif exception.is_a?(Exception)
26
12
  raise exception
13
+ else
14
+ throw :exception, exception
27
15
  end
28
16
  end
29
17
  }
@@ -47,33 +35,41 @@ module I18n
47
35
  end
48
36
  end
49
37
 
50
- class MissingTranslationData < ArgumentError
51
- attr_reader :locale, :key, :options
38
+ class MissingTranslation
39
+ module Base
40
+ attr_reader :locale, :key, :options
52
41
 
53
- def initialize(locale, key, opts = nil)
54
- @key, @locale, @options = key, locale, opts.dup || {}
55
- options.each { |k, v| options[k] = v.inspect if v.is_a?(Proc) }
56
- super "translation missing: #{keys.join('.')}"
57
- end
42
+ def initialize(locale, key, options = nil)
43
+ @key, @locale, @options = key, locale, options.dup || {}
44
+ options.each { |k, v| self.options[k] = v.inspect if v.is_a?(Proc) }
45
+ end
58
46
 
59
- def html_message
60
- key = CGI.escapeHTML titleize(keys.last)
61
- path = CGI.escapeHTML keys.join('.')
62
- %(<span class="translation_missing" title="translation missing: #{path}">#{key}</span>)
63
- end
47
+ def html_message
48
+ key = keys.last.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
49
+ %(<span class="translation_missing" title="translation missing: #{keys.join('.')}">#{key}</span>)
50
+ end
64
51
 
65
- def keys
66
- @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
67
- keys << 'no key' if keys.size < 2
52
+ def keys
53
+ @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
54
+ keys << 'no key' if keys.size < 2
55
+ end
68
56
  end
69
- end
70
57
 
71
- protected
58
+ def message
59
+ "translation missing: #{keys.join('.')}"
60
+ end
61
+ alias :to_s :message
72
62
 
73
- # TODO : remove when #html_message is removed
74
- def titleize(key)
75
- key.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
63
+ def to_exception
64
+ MissingTranslationData.new(locale, key, options)
65
+ end
76
66
  end
67
+
68
+ include Base
69
+ end
70
+
71
+ class MissingTranslationData < ArgumentError
72
+ include MissingTranslation::Base
77
73
  end
78
74
 
79
75
  class InvalidPluralizationData < ArgumentError
@@ -89,7 +89,7 @@ module I18n
89
89
  tags
90
90
  end.flatten
91
91
  result.push(*defaults) if include_defaults
92
- result.uniq
92
+ result.uniq.compact
93
93
  end
94
94
  end
95
95
  end
@@ -27,13 +27,13 @@ module I18n
27
27
 
28
28
  test "localize Date: given a format that resolves to a Proc it calls the Proc with the object" do
29
29
  setup_time_proc_translations
30
- date = ::Date.new(2008, 3, 1)
30
+ date = ::Date.new(2008, 3, 1, 6)
31
31
  assert_equal '[Sat, 01 Mar 2008, {}]', I18n.l(date, :format => :proc, :locale => :ru)
32
32
  end
33
33
 
34
34
  test "localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
35
35
  setup_time_proc_translations
36
- date = ::Date.new(2008, 3, 1)
36
+ date = ::Date.new(2008, 3, 1, 6)
37
37
  assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module I18n
2
- VERSION = "0.5.4"
2
+ VERSION = "0.6.0beta1"
3
3
  end
@@ -37,12 +37,14 @@ class I18nBackendCacheTest < Test::Unit::TestCase
37
37
  end
38
38
 
39
39
  test "still raises MissingTranslationData but also caches it" do
40
- I18n.backend.expects(:lookup).returns(nil)
41
40
  assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
42
-
43
- I18n.cache_store.expects(:write).never
44
- I18n.backend.expects(:lookup).never
45
41
  assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
42
+ assert_equal 1, I18n.cache_store.instance_variable_get(:@data).size
43
+
44
+ # I18n.backend.expects(:lookup).returns(nil)
45
+ # assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
46
+ # I18n.backend.expects(:lookup).never
47
+ # assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
46
48
  end
47
49
 
48
50
  test "uses 'i18n' as a cache key namespace by default" do
@@ -57,6 +57,11 @@ class I18nBackendChainTest < Test::Unit::TestCase
57
57
  assert_equal [{ :short => 'short', :long => 'long' }, { :one => 'one' }, 'Bah'], I18n.t([:formats, :plural_2, :bah], :default => 'Bah')
58
58
  end
59
59
 
60
+ test "store_translations options are not dropped while transfering to backend" do
61
+ @first.expects(:store_translations).with(:foo, {:bar => :baz}, {:option => 'persists'})
62
+ I18n.backend.store_translations :foo, {:bar => :baz}, {:option => 'persists'}
63
+ end
64
+
60
65
  protected
61
66
 
62
67
  def backend(translations)
@@ -5,6 +5,13 @@ class I18nBackendExceptionsTest < Test::Unit::TestCase
5
5
  I18n.backend = I18n::Backend::Simple.new
6
6
  end
7
7
 
8
+ test "throw message: MissingTranslation message from #translate includes the given scope and full key" do
9
+ exception = catch(:exception) do
10
+ I18n.t(:'baz.missing', :scope => :'foo.bar', :throw => true)
11
+ end
12
+ assert_equal "translation missing: en.foo.bar.baz.missing", exception.message
13
+ end
14
+
8
15
  test "exceptions: MissingTranslationData message from #translate includes the given scope and full key" do
9
16
  begin
10
17
  I18n.t(:'baz.missing', :scope => :'foo.bar', :raise => true)
@@ -65,6 +65,10 @@ class I18nBackendFallbacksTranslateTest < Test::Unit::TestCase
65
65
  assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :en, :raise => true) }
66
66
  assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :de, :raise => true) }
67
67
  end
68
+
69
+ test "should ensure that default is not splitted on new line char" do
70
+ assert_equal "Default \n Bar", I18n.t(:missing_bar, :default => "Default \n Bar")
71
+ end
68
72
  end
69
73
 
70
74
  class I18nBackendFallbacksLocalizeTest < Test::Unit::TestCase
@@ -42,6 +42,10 @@ class I18nBackendSimpleTest < Test::Unit::TestCase
42
42
  assert_equal expected, translations
43
43
  end
44
44
 
45
+ test "simple load_translations: given file names as array it does not raise anything" do
46
+ assert_nothing_raised { I18n.backend.load_translations(["#{locales_dir}/en.rb", "#{locales_dir}/en.yml"]) }
47
+ end
48
+
45
49
  # storing translations
46
50
 
47
51
  test "simple store_translations: stores translations, ... no, really :-)" do
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class I18nExceptionsTest < Test::Unit::TestCase
4
4
  def test_invalid_locale_stores_locale
5
5
  force_invalid_locale
6
- rescue I18n::ArgumentError => exception
6
+ rescue I18n::ArgumentError => e
7
7
  assert_nil exception.locale
8
8
  end
9
9
 
@@ -28,13 +28,9 @@ class I18nExceptionsTest < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  test "MissingTranslationData html_message is a span with the titlelized last key token" do
31
- exception = I18n::MissingTranslationData.new(:de, :foo, :scope => :bar)
32
- assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', exception.html_message
33
- end
34
-
35
- test "MissingTranslationData html_message html escapes key names" do
36
- exception = I18n::MissingTranslationData.new(:de, '<script>Evil</script>', :scope => '<iframe src="example.com" />')
37
- assert_equal '<span class="translation_missing" title="translation missing: de.&lt;iframe src=&quot;example.com&quot; /&gt;.&lt;script&gt;Evil&lt;/script&gt;">&lt;Script&gt;Evil&lt;/Script&gt;</span>', exception.html_message
31
+ force_missing_translation_data do |exception|
32
+ assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', exception.html_message
33
+ end
38
34
  end
39
35
 
40
36
  test "ExceptionHandler returns the html_message if :rescue_format => :html was given" do
@@ -35,24 +35,6 @@ class I18nTest < Test::Unit::TestCase
35
35
  end
36
36
  end
37
37
 
38
- test "translate given an unavailable locale rases an I18n::InvalidLocale" do
39
- begin
40
- I18n.config.enforce_available_locales = true
41
- assert_raise(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
42
- ensure
43
- I18n.config.enforce_available_locales = false
44
- end
45
- end
46
-
47
- test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
48
- begin
49
- I18n.config.enforce_available_locales = true
50
- assert_raise(I18n::InvalidLocale) { I18n.default_locale = :klingon }
51
- ensure
52
- I18n.config.enforce_available_locales = false
53
- end
54
- end
55
-
56
38
  test "uses the default locale as a locale by default" do
57
39
  assert_equal I18n.default_locale, I18n.locale
58
40
  end
@@ -64,15 +46,6 @@ class I18nTest < Test::Unit::TestCase
64
46
  I18n.locale = :en
65
47
  end
66
48
 
67
- test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
68
- begin
69
- I18n.config.enforce_available_locales = true
70
- assert_raise(I18n::InvalidLocale) { I18n.locale = :klingon }
71
- ensure
72
- I18n.config.enforce_available_locales = false
73
- end
74
- end
75
-
76
49
  test "can set the configuration object" do
77
50
  begin
78
51
  I18n.config = self
@@ -221,74 +194,23 @@ class I18nTest < Test::Unit::TestCase
221
194
  assert_raise(I18n::ArgumentError) { I18n.l Object.new }
222
195
  end
223
196
 
224
- test "localize given an unavailable locale rases an I18n::InvalidLocale" do
225
- begin
226
- I18n.config.enforce_available_locales = true
227
- assert_raise(I18n::InvalidLocale) { I18n.l(Time.now, :locale => 'klingon') }
228
- ensure
229
- I18n.config.enforce_available_locales = false
230
- end
231
- end
232
-
233
197
  test "can use a lambda as an exception handler" do
234
198
  begin
235
199
  previous_exception_handler = I18n.exception_handler
236
- I18n.exception_handler = Proc.new { |exception, locale, key, options| exception }
237
- assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
200
+ I18n.exception_handler = Proc.new { |exception, locale, key, options| key }
201
+ assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
238
202
  ensure
239
203
  I18n.exception_handler = previous_exception_handler
240
204
  end
241
205
  end
242
206
 
243
- test "transliterate given an unavailable locale rases an I18n::InvalidLocale" do
244
- begin
245
- I18n.config.enforce_available_locales = true
246
- assert_raise(I18n::InvalidLocale) { I18n.transliterate('string', :locale => 'klingon') }
247
- ensure
248
- I18n.config.enforce_available_locales = false
249
- end
250
- end
251
-
252
- test "I18n.locale_available? returns true when the passed locale is available" do
253
- I18n.available_locales = [:en, :de]
254
- assert_equal true, I18n.locale_available?(:de)
255
- end
256
-
257
- test "I18n.locale_available? returns true when the passed locale is a string and is available" do
258
- I18n.available_locales = [:en, :de]
259
- assert_equal true, I18n.locale_available?('de')
260
- end
261
-
262
- test "I18n.locale_available? returns false when the passed locale is unavailable" do
263
- assert_equal false, I18n.locale_available?(:klingon)
264
- end
265
-
266
- test "I18n.enforce_available_locales! raises an I18n::InvalidLocale when the passed locale is unavailable" do
267
- begin
268
- I18n.config.enforce_available_locales = true
269
- assert_raise(I18n::InvalidLocale) { I18n.enforce_available_locales!(:klingon) }
270
- ensure
271
- I18n.config.enforce_available_locales = false
272
- end
273
- end
274
-
275
- test "I18n.enforce_available_locales! does nothing when the passed locale is available" do
276
- I18n.available_locales = [:en, :de]
277
- begin
278
- I18n.config.enforce_available_locales = true
279
- assert_nothing_raised { I18n.enforce_available_locales!(:en) }
280
- ensure
281
- I18n.config.enforce_available_locales = false
282
- end
283
- end
284
-
285
207
  test "can use an object responding to #call as an exception handler" do
286
208
  begin
287
209
  previous_exception_handler = I18n.exception_handler
288
210
  I18n.exception_handler = Class.new do
289
- def call(exception, locale, key, options); exception; end
211
+ def call(exception, locale, key, options); key; end
290
212
  end.new
291
- assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
213
+ assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
292
214
  ensure
293
215
  I18n.exception_handler = previous_exception_handler
294
216
  end
@@ -311,13 +233,4 @@ class I18nTest < Test::Unit::TestCase
311
233
  assert_raise(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
312
234
  assert_equal I18n.default_locale, I18n.locale
313
235
  end
314
-
315
- test "I18n.enforce_available_locales config can be set to false" do
316
- begin
317
- I18n.config.enforce_available_locales = false
318
- assert_equal false, I18n.config.enforce_available_locales
319
- ensure
320
- I18n.config.enforce_available_locales = false
321
- end
322
- end
323
236
  end
@@ -1,13 +1,13 @@
1
1
  def bundle_check
2
- `bundle check` == "Resolving dependencies...\nThe Gemfile's dependencies are satisfied\n"
2
+ `bundle check` == "The Gemfile's dependencies are satisfied\n"
3
3
  end
4
4
 
5
- command = 'bundle exec ruby -w -Ilib -Itest test/all.rb'
6
- gemfiles = %w(Gemfile) + Dir['gemfiles/Gemfile*'].reject { |f| f.end_with?('.lock') }
5
+ command = 'ruby -w -Ilib -Itest test/all.rb'
6
+ gemfiles = %w(ci/Gemfile.rails-3.x ci/Gemfile.rails-2.3.x ci/Gemfile.no-rails)
7
7
 
8
8
  results = gemfiles.map do |gemfile|
9
9
  puts "BUNDLE_GEMFILE=#{gemfile}"
10
- ENV['BUNDLE_GEMFILE'] = File.expand_path("../../#{gemfile}", __FILE__)
10
+ ENV['BUNDLE_GEMFILE'] = gemfile
11
11
 
12
12
  unless bundle_check
13
13
  puts "bundle install"
@@ -15,7 +15,7 @@ results = gemfiles.map do |gemfile|
15
15
  end
16
16
 
17
17
  puts command
18
- system command
18
+ system('ruby -w -Ilib -Itest test/all.rb')
19
19
  end
20
20
 
21
21
  exit(results.inject(true) { |a, b| a && b })
@@ -14,7 +14,7 @@ end
14
14
 
15
15
  require 'bundler/setup'
16
16
  require 'i18n'
17
- require 'mocha/setup'
17
+ require 'mocha'
18
18
  require 'test_declarative'
19
19
 
20
20
  class Test::Unit::TestCase
metadata CHANGED
@@ -1,9 +1,17 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: i18n
3
- version: !ruby/object:Gem::Version
4
- version: 0.5.4
3
+ version: !ruby/object:Gem::Version
4
+ hash: 62196417
5
+ prerelease: 5
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ - beta
11
+ - 1
12
+ version: 0.6.0beta1
5
13
  platform: ruby
6
- authors:
14
+ authors:
7
15
  - Sven Fuchs
8
16
  - Joshua Harvey
9
17
  - Matt Aimonetti
@@ -12,19 +20,83 @@ authors:
12
20
  autorequire:
13
21
  bindir: bin
14
22
  cert_chain: []
15
- date: 2014-05-28 00:00:00.000000000 Z
16
- dependencies: []
23
+
24
+ date: 2011-04-26 00:00:00 +02:00
25
+ default_executable:
26
+ dependencies:
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ prerelease: false
30
+ requirement: &id001 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ hash: 7
36
+ segments:
37
+ - 3
38
+ - 0
39
+ - 0
40
+ version: 3.0.0
41
+ type: :development
42
+ version_requirements: *id001
43
+ - !ruby/object:Gem::Dependency
44
+ name: sqlite3-ruby
45
+ prerelease: false
46
+ requirement: &id002 !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ type: :development
56
+ version_requirements: *id002
57
+ - !ruby/object:Gem::Dependency
58
+ name: mocha
59
+ prerelease: false
60
+ requirement: &id003 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id003
71
+ - !ruby/object:Gem::Dependency
72
+ name: test_declarative
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ version_requirements: *id004
17
85
  description: New wave Internationalization support for Ruby.
18
86
  email: rails-i18n@googlegroups.com
19
87
  executables: []
88
+
20
89
  extensions: []
90
+
21
91
  extra_rdoc_files: []
22
- files:
23
- - CHANGELOG.textile
24
- - MIT-LICENSE
25
- - README.textile
26
- - lib/i18n.rb
27
- - lib/i18n/backend.rb
92
+
93
+ files:
94
+ - ci/Gemfile.no-rails
95
+ - ci/Gemfile.no-rails.lock
96
+ - ci/Gemfile.rails-2.3.x
97
+ - ci/Gemfile.rails-2.3.x.lock
98
+ - ci/Gemfile.rails-3.x
99
+ - ci/Gemfile.rails-3.x.lock
28
100
  - lib/i18n/backend/base.rb
29
101
  - lib/i18n/backend/cache.rb
30
102
  - lib/i18n/backend/cascade.rb
@@ -39,35 +111,37 @@ files:
39
111
  - lib/i18n/backend/pluralization.rb
40
112
  - lib/i18n/backend/simple.rb
41
113
  - lib/i18n/backend/transliterator.rb
114
+ - lib/i18n/backend.rb
42
115
  - lib/i18n/config.rb
43
116
  - lib/i18n/core_ext/hash.rb
44
117
  - lib/i18n/core_ext/kernel/surpress_warnings.rb
45
118
  - lib/i18n/core_ext/string/interpolate.rb
46
119
  - lib/i18n/exceptions.rb
47
- - lib/i18n/gettext.rb
48
120
  - lib/i18n/gettext/helpers.rb
49
121
  - lib/i18n/gettext/po_parser.rb
122
+ - lib/i18n/gettext.rb
50
123
  - lib/i18n/interpolate/ruby.rb
51
- - lib/i18n/locale.rb
52
124
  - lib/i18n/locale/fallbacks.rb
53
- - lib/i18n/locale/tag.rb
54
125
  - lib/i18n/locale/tag/parents.rb
55
126
  - lib/i18n/locale/tag/rfc4646.rb
56
127
  - lib/i18n/locale/tag/simple.rb
57
- - lib/i18n/tests.rb
128
+ - lib/i18n/locale/tag.rb
129
+ - lib/i18n/locale.rb
58
130
  - lib/i18n/tests/basics.rb
59
131
  - lib/i18n/tests/defaults.rb
60
132
  - lib/i18n/tests/interpolation.rb
61
133
  - lib/i18n/tests/link.rb
62
- - lib/i18n/tests/localization.rb
63
134
  - lib/i18n/tests/localization/date.rb
64
135
  - lib/i18n/tests/localization/date_time.rb
65
136
  - lib/i18n/tests/localization/procs.rb
66
137
  - lib/i18n/tests/localization/time.rb
138
+ - lib/i18n/tests/localization.rb
67
139
  - lib/i18n/tests/lookup.rb
68
140
  - lib/i18n/tests/pluralization.rb
69
141
  - lib/i18n/tests/procs.rb
142
+ - lib/i18n/tests.rb
70
143
  - lib/i18n/version.rb
144
+ - lib/i18n.rb
71
145
  - test/all.rb
72
146
  - test/api/all_features_test.rb
73
147
  - test/api/cascade_test.rb
@@ -107,27 +181,44 @@ files:
107
181
  - test/test_data/locales/invalid/empty.yml
108
182
  - test/test_data/locales/plurals.rb
109
183
  - test/test_helper.rb
184
+ - README.textile
185
+ - MIT-LICENSE
186
+ - CHANGELOG.textile
187
+ has_rdoc: true
110
188
  homepage: http://github.com/svenfuchs/i18n
111
189
  licenses: []
112
- metadata: {}
190
+
113
191
  post_install_message:
114
192
  rdoc_options: []
115
- require_paths:
193
+
194
+ require_paths:
116
195
  - lib
117
- required_ruby_version: !ruby/object:Gem::Requirement
118
- requirements:
119
- - - ! '>='
120
- - !ruby/object:Gem::Version
121
- version: '0'
122
- required_rubygems_version: !ruby/object:Gem::Requirement
123
- requirements:
124
- - - ! '>='
125
- - !ruby/object:Gem::Version
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ none: false
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ hash: 3
202
+ segments:
203
+ - 0
204
+ version: "0"
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ hash: 17
211
+ segments:
212
+ - 1
213
+ - 3
214
+ - 5
126
215
  version: 1.3.5
127
216
  requirements: []
128
- rubyforge_project: ! '[none]'
129
- rubygems_version: 2.2.2
217
+
218
+ rubyforge_project: "[none]"
219
+ rubygems_version: 1.4.2
130
220
  signing_key:
131
- specification_version: 4
221
+ specification_version: 3
132
222
  summary: New wave Internationalization support for Ruby
133
223
  test_files: []
224
+
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MzZlZTA2MGRlOTAzNTY4MGM1NmM2ODRhNjFjNWFkMTMyODNiNTU4Mg==
5
- data.tar.gz: !binary |-
6
- MGJmZmRhZjU1NGRlMzFjZjdjZDA2OWUxYWM3MmIwNmU0NzkyYmNmMA==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- YjkxZmU3MGJkZmM2ZDAxMjA0ZGI5OGNkYWZiNGU3MDEzNGZlYWU0YWNhNzQ2
10
- YmE1ZDAzZGE2MjQyZDJiMTZlZGFiMTUyOThlYjdkYTQ4MGUxNmYwZjVhOGY5
11
- ZTFkNDkzNzlmOWRlODVkMGI5YmY0ZTk5YmZjYTNhZTlhZjk4NDk=
12
- data.tar.gz: !binary |-
13
- YmQwN2ViOWZhMDkwYmZiMDgyODMyMjg0NjY0ZmViZTg4YThmYWU0NzVjNjRl
14
- ZTk5NzhiMjVjMTExYjc5MzVkYTIwMDAyODI1YjFlZGM4YjY0MWM0NDNiZDZk
15
- MTIxOTI1NWEwNjBkZDhjZTZmNmQ4MTBhNmQyNDhlNjFiNjlhY2I=