Permalink
Browse files

move exceptions to an extra file and store context information on exc…

…eptions
  • Loading branch information...
1 parent b18b5f9 commit 80a941df071c81c98c2d6cb423bf424c1c3eb6d2 Sven Fuchs committed Jul 6, 2008
Showing with 50 additions and 13 deletions.
  1. +2 −8 lib/i18n.rb
  2. +5 −5 lib/i18n/backend/simple.rb
  3. +43 −0 lib/i18n/exceptions.rb
View
@@ -6,15 +6,9 @@
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT
require 'i18n/backend/simple'
+require 'i18n/exceptions'
-module I18n
- class ArgumentError < ::ArgumentError; end
- class InvalidLocale < ArgumentError; end
- class MissingTranslationData < ArgumentError; end
- class InvalidPluralizationData < ArgumentError; end
- class MissingInterpolationArgument < ArgumentError; end
- class ReservedInterpolationKey < ArgumentError; end
-
+module I18n
@@backend = Backend::Simple
@@default_locale = 'en-US'
@@exception_handler = :default_exception_handler
View
@@ -22,15 +22,15 @@ def store_translations(locale, data)
end
def translate(key, locale, options = {})
- raise InvalidLocale, 'locale is nil in I18n::Backend::Simple#translate' if locale.nil?
+ raise InvalidLocale.new(locale) if locale.nil?
return key.map{|key| translate key, locale, options } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
options.delete(:default)
values = options.reject{|name, value| reserved.include? name }
- entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData, "translation data missing for #{I18n.send(:normalize_translation_keys, locale, key, scope).inspect}")
+ entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(key, locale, options))
entry = pluralize entry, count
entry = interpolate entry, values
entry
@@ -94,7 +94,7 @@ def default(locale, default, options = {})
# implement more flexible or complex pluralization rules.
def pluralize(entry, count)
return entry unless entry.is_a?(Array) and count
- raise InvalidPluralizationData, "translation data #{entry} can not be used with :count => #{count}" unless entry.size == 2
+ raise InvalidPluralizationData.new(entry, count) unless entry.size == 2
entry[count == 1 ? 0 : 1]
end
@@ -119,8 +119,8 @@ def interpolate(string, values = {})
key = s.scan_until(/\}\}/)[0..-3]
end_pos = s.pos - 1
- raise ReservedInterpolationKey, %s(reserved key :#{key} used in "#{string}") if %w(scope default).include?(key)
- raise MissingInterpolationArgument, %s(interpolation argument #{key} missing in "#{string}") unless values.has_key? key.to_sym
+ raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key)
+ raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym
s.string[start_pos..end_pos] = values[key.to_sym].to_s
s.unscan
View
@@ -0,0 +1,43 @@
+module I18n
+ class ArgumentError < ::ArgumentError; end
+ class InvalidLocale < ArgumentError
+ attr_reader :locale
+ def initialize(locale)
+ @locale = locale
+ super "#{locale.inspect} is not a valid locale"
+ end
+ end
+
+ class MissingTranslationData < ArgumentError
+ attr_reader :key, :locale, :options
+ def initialize(key, locale, options)
+ @key, @locale, @options = key, locale, options
+ keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
+ super "translation data missing for #{keys.inspect}"
+ end
+ end
+
+ class InvalidPluralizationData < ArgumentError
+ attr_reader :entry, :count
+ def initialize(entry, count)
+ @entry, @count = entry, count
+ super "translation data #{entry.inspect} can not be used with :count => #{count}"
+ end
+ end
+
+ class MissingInterpolationArgument < ArgumentError
+ attr_reader :key, :string
+ def initialize(key, string)
+ @key, @string = key, string
+ super %s(interpolation argument #{key} missing in "#{string}")
+ end
+ end
+
+ class ReservedInterpolationKey < ArgumentError
+ attr_reader :key, :string
+ def initialize(key, string)
+ @key, @string = key, string
+ super "reserved key #{key.inspect} used in #{string.inspect}"
+ end
+ end
+end

0 comments on commit 80a941d

Please sign in to comment.