reworked i18n messages, interpolation to behave on missing translation

This commit is contained in:
Adam Meehan 2009-12-11 15:15:16 +11:00
parent 9cfbb2a458
commit 5258256d5e
2 changed files with 20 additions and 30 deletions

View File

@ -115,27 +115,26 @@ module Spec
pass pass
end end
def error_message_for(option) def error_message_for(message)
msg = @validator.error_messages[option] restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[message], @type, @record)
restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[option], @type, @record)
if restriction if restriction
restriction = [restriction] unless restriction.is_a?(Array) restriction = @validator.class.send(:type_cast_value, restriction, @type)
restriction.map! {|r| @validator.class.send(:type_cast_value, r, @type) } interpolate = @validator.send(:interpolation_values, message, restriction)
interpolate = @validator.send(:interpolation_values, option, restriction ) end
# get I18n message if defined and has interpolation keys in msg if defined?(I18n)
if defined?(I18n) && !@validator.send(:custom_error_messages).include?(option) interpolate ||= {}
msg = if defined?(ActiveRecord::Error) options = interpolate.merge(:default => @validator.send(:custom_error_messages)[message])
ActiveRecord::Error.new(@record, @expected, option, interpolate).message if defined?(ActiveRecord::Error)
else ActiveRecord::Error.new(@record, @expected, message, options).message
@record.errors.generate_message(@expected, option, interpolate) else
end @record.errors.generate_message(@expected, message, options)
else end
msg = msg % interpolate else
end interpolate ||= nil
@validator.error_messages[message] % interpolate
end end
msg
end end
def format_value(value) def format_value(value)

View File

@ -46,7 +46,7 @@ module ValidatesTimeliness
end end
def error_messages def error_messages
@error_messages ||= self.class.default_error_messages.merge(custom_error_messages) @error_messages ||= ::ActiveRecord::Errors.default_error_messages.merge(custom_error_messages)
end end
private private
@ -87,10 +87,9 @@ module ValidatesTimeliness
restriction = [restriction] unless restriction.is_a?(Array) restriction = [restriction] unless restriction.is_a?(Array)
if defined?(I18n) if defined?(I18n)
message = I18n.t('activerecord.errors.messages')[option]
subs = message.scan(/\{\{([^\}]*)\}\}/)
interpolations = {} interpolations = {}
subs.each_with_index {|s, i| interpolations[s[0].to_sym] = restriction[i].strftime(format) } keys = restriction.size == 1 ? [:restriction] : [:earliest, :latest]
keys.each_with_index {|key, i| interpolations[key] = restriction[i].strftime(format) }
interpolations interpolations
else else
restriction.map {|r| r.strftime(format) } restriction.map {|r| r.strftime(format) }
@ -154,14 +153,6 @@ module ValidatesTimeliness
# class methods # class methods
class << self class << self
def default_error_messages
if defined?(I18n)
I18n.t('activerecord.errors.messages')
else
::ActiveRecord::Errors.default_error_messages
end
end
def error_value_format_for(type) def error_value_format_for(type)
if defined?(I18n) if defined?(I18n)
# work around for syntax check in vendored I18n for Rails <= 2.3.3 # work around for syntax check in vendored I18n for Rails <= 2.3.3