2f625a027f2b85aa8f78c025d0f7993fc1107026
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / exceptions.rb
1 module I18n
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.
6   class ExceptionHandler
7     include Module.new {
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)
12           raise exception
13         else
14           throw :exception, exception
15         end
16       end
17     }
18   end
19
20   class ArgumentError < ::ArgumentError; end
21
22   class InvalidLocale < ArgumentError
23     attr_reader :locale
24     def initialize(locale)
25       @locale = locale
26       super "#{locale.inspect} is not a valid locale"
27     end
28   end
29
30   class InvalidLocaleData < ArgumentError
31     attr_reader :filename
32     def initialize(filename)
33       @filename = filename
34       super "can not load translations from #{filename}, expected it to return a hash, but does not"
35     end
36   end
37
38   class MissingTranslation
39     module Base
40       attr_reader :locale, :key, :options
41
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
46
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
51
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
56       end
57
58       def message
59         "translation missing: #{keys.join('.')}"
60       end
61       alias :to_s :message
62
63       def to_exception
64         MissingTranslationData.new(locale, key, options)
65       end
66     end
67
68     include Base
69   end
70
71   class MissingTranslationData < ArgumentError
72     include MissingTranslation::Base
73   end
74
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}"
80     end
81   end
82
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)"
88     end
89   end
90
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}"
96     end
97   end
98
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"
104     end
105   end
106 end