lib/i18n/exceptions.rb in i18n-0.4.2 vs lib/i18n/exceptions.rb in i18n-0.5.0beta1
- old
+ new
@@ -1,14 +1,28 @@
-# encoding: utf-8
-
class KeyError < IndexError
def initialize(message = nil)
super(message || "key not found")
end
end unless defined?(KeyError)
module I18n
+ # Handles exceptions raised in the backend. All exceptions except for
+ # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
+ # was caught the handler returns an error message string containing the key/scope.
+ # Note that the exception handler is not called when the option :raise was given.
+ class ExceptionHandler
+ include Module.new {
+ def call(exception, locale, key, options)
+ if exception.is_a?(MissingTranslationData)
+ options[:rescue_format] == :html ? exception.html_message : exception.message
+ else
+ raise exception
+ end
+ end
+ }
+ end
+
class ArgumentError < ::ArgumentError; end
class InvalidLocale < ArgumentError
attr_reader :locale
def initialize(locale)
@@ -25,18 +39,27 @@
end
end
class MissingTranslationData < ArgumentError
attr_reader :locale, :key, :options
+
def initialize(locale, key, opts = nil)
@key, @locale, @options = key, locale, opts.dup || {}
options.each { |k, v| options[k] = v.inspect if v.is_a?(Proc) }
+ super "translation missing: #{keys.join('.')}"
+ end
- keys = I18n.normalize_keys(locale, key, options[:scope])
- keys << 'no key' if keys.size < 2
- super "translation missing: #{keys.join(', ')}"
+ def html_message
+ key = keys.last.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
+ %(<span class="translation_missing" title="translation missing: #{keys.join('.')}">#{key}</span>)
end
+
+ def keys
+ @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
+ keys << 'no key' if keys.size < 2
+ end
+ end
end
class InvalidPluralizationData < ArgumentError
attr_reader :entry, :count
def initialize(entry, count)
@@ -66,6 +89,6 @@
def initialize(type, filename)
@type, @filename = type, filename
super "can not load translations from #{filename}, the file type #{type} is not known"
end
end
-end
\ No newline at end of file
+end