Skip to content

Commit

Permalink
move exceptions to an extra file and store context information on exc…
Browse files Browse the repository at this point in the history
…eptions
  • Loading branch information
Sven Fuchs committed Jul 6, 2008
1 parent b18b5f9 commit 80a941d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
10 changes: 2 additions & 8 deletions lib/i18n.rb
Expand Up @@ -6,15 +6,9 @@
# Copyright:: Copyright (c) 2008 The Ruby i18n Team # Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT # License:: MIT
require 'i18n/backend/simple' require 'i18n/backend/simple'
require 'i18n/exceptions'


module I18n 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

@@backend = Backend::Simple @@backend = Backend::Simple
@@default_locale = 'en-US' @@default_locale = 'en-US'
@@exception_handler = :default_exception_handler @@exception_handler = :default_exception_handler
Expand Down
10 changes: 5 additions & 5 deletions lib/i18n/backend/simple.rb
Expand Up @@ -22,15 +22,15 @@ def store_translations(locale, data)
end end


def translate(key, locale, options = {}) 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 return key.map{|key| translate key, locale, options } if key.is_a? Array


reserved = :scope, :default reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved) count, scope, default = options.values_at(:count, *reserved)
options.delete(:default) options.delete(:default)
values = options.reject{|name, value| reserved.include? name } 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 = pluralize entry, count
entry = interpolate entry, values entry = interpolate entry, values
entry entry
Expand Down Expand Up @@ -94,7 +94,7 @@ def default(locale, default, options = {})
# implement more flexible or complex pluralization rules. # implement more flexible or complex pluralization rules.
def pluralize(entry, count) def pluralize(entry, count)
return entry unless entry.is_a?(Array) and 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] entry[count == 1 ? 0 : 1]
end end


Expand All @@ -119,8 +119,8 @@ def interpolate(string, values = {})
key = s.scan_until(/\}\}/)[0..-3] key = s.scan_until(/\}\}/)[0..-3]
end_pos = s.pos - 1 end_pos = s.pos - 1


raise ReservedInterpolationKey, %s(reserved key :#{key} used in "#{string}") if %w(scope default).include?(key) raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key)
raise MissingInterpolationArgument, %s(interpolation argument #{key} missing in "#{string}") unless values.has_key? key.to_sym 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.string[start_pos..end_pos] = values[key.to_sym].to_s
s.unscan s.unscan
Expand Down
43 changes: 43 additions & 0 deletions 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

0 comments on commit 80a941d

Please sign in to comment.