diff --git a/lib/i18n.rb b/lib/i18n.rb index 61132e8b..24bef854 100755 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -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 diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index 4f9373e2..baf7fe3f 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -22,7 +22,7 @@ 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 @@ -30,7 +30,7 @@ def translate(key, locale, options = {}) 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 diff --git a/lib/i18n/exceptions.rb b/lib/i18n/exceptions.rb new file mode 100644 index 00000000..3e4ce85e --- /dev/null +++ b/lib/i18n/exceptions.rb @@ -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 \ No newline at end of file