2 # Handles exceptions raised in the backend. All exceptions except for
3 # MissingTranslationData exceptions are re-thrown. When a MissingTranslationData
4 # was caught the handler returns an error message string containing the key/scope.
5 # Note that the exception handler is not called when the option :throw was given.
8 def call(exception, locale, key, options)
9 if exception.is_a?(MissingTranslation)
10 options[:rescue_format] == :html ? exception.html_message : exception.message
11 elsif exception.is_a?(Exception)
14 throw :exception, exception
20 class ArgumentError < ::ArgumentError; end
22 class InvalidLocale < ArgumentError
24 def initialize(locale)
26 super "#{locale.inspect} is not a valid locale"
30 class InvalidLocaleData < ArgumentError
32 def initialize(filename)
34 super "can not load translations from #{filename}, expected it to return a hash, but does not"
38 class MissingTranslation
40 attr_reader :locale, :key, :options
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) }
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>)
53 @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
54 keys << 'no key' if keys.size < 2
59 "translation missing: #{keys.join('.')}"
64 MissingTranslationData.new(locale, key, options)
71 class MissingTranslationData < ArgumentError
72 include MissingTranslation::Base
75 class InvalidPluralizationData < ArgumentError
76 attr_reader :entry, :count
77 def initialize(entry, count)
78 @entry, @count = entry, count
79 super "translation data #{entry.inspect} can not be used with :count => #{count}"
83 class MissingInterpolationArgument < ArgumentError
84 attr_reader :values, :string
85 def initialize(values, string)
86 @values, @string = values, string
87 super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
91 class ReservedInterpolationKey < ArgumentError
92 attr_reader :key, :string
93 def initialize(key, string)
94 @key, @string = key, string
95 super "reserved key #{key.inspect} used in #{string.inspect}"
99 class UnknownFileType < ArgumentError
100 attr_reader :type, :filename
101 def initialize(type, filename)
102 @type, @filename = type, filename
103 super "can not load translations from #{filename}, the file type #{type} is not known"