mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
added i18n support for error messages in Rails 2.2
This commit is contained in:
@@ -26,7 +26,6 @@ module ValidatesTimeliness
|
||||
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
mattr_accessor :error_value_formats
|
||||
mattr_accessor :default_error_messages
|
||||
|
||||
@@ignore_restriction_errors = false
|
||||
|
||||
@@ -36,16 +35,24 @@ module ValidatesTimeliness
|
||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||
}
|
||||
|
||||
@@default_error_messages = {
|
||||
:empty => "cannot be empty",
|
||||
:blank => "cannot be blank",
|
||||
:invalid_date => "is not a valid date",
|
||||
:invalid_time => "is not a valid time",
|
||||
:invalid_datetime => "is not a valid datetime",
|
||||
:before => "must be before %s",
|
||||
:on_or_before => "must be on or before %s",
|
||||
:after => "must be after %s",
|
||||
:on_or_after => "must be on or after %s"
|
||||
}
|
||||
def self.load_error_messages
|
||||
path = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')
|
||||
if Rails::VERSION::STRING < '2.2'
|
||||
messages = YAML::load(IO.read(path))
|
||||
errors = messages['en']['activerecord']['errors']['messages'].inject({}) {|h,(k,v)| h[k.to_sym] = v.gsub(/\{\{\w*\}\}/, '%s');h }
|
||||
::ActiveRecord::Errors.default_error_messages.update(errors)
|
||||
else
|
||||
I18n.load_path += [ path ]
|
||||
end
|
||||
end
|
||||
|
||||
def self.default_error_messages
|
||||
if Rails::VERSION::STRING < '2.2'
|
||||
::ActiveRecord::Errors.default_error_messages
|
||||
else
|
||||
I18n.translate('activerecord.errors.messages')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ValidatesTimeliness.load_error_messages
|
||||
|
||||
11
lib/validates_timeliness/locale/en.yml
Normal file
11
lib/validates_timeliness/locale/en.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
en:
|
||||
activerecord:
|
||||
errors:
|
||||
messages:
|
||||
invalid_date: "is not a valid date"
|
||||
invalid_time: "is not a valid time"
|
||||
invalid_datetime: "is not a valid datetime"
|
||||
before: "must be before {{restriction}}"
|
||||
on_or_before: "must be on or before {{restriction}}"
|
||||
after: "must be after {{restriction}}"
|
||||
on_or_after: "must be on or after {{restriction}}"
|
||||
@@ -18,7 +18,7 @@ module Spec
|
||||
def compile_error_messages
|
||||
validator = ValidatesTimeliness::Validator.new(options)
|
||||
messages = validator.send(:error_messages)
|
||||
@messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(' %s', ''); h }
|
||||
@messages = messages.inject({}) {|h, (k, v)| h[k] = v.gsub(/ (\%s|\{\{\w*\}\})/, ''); h }
|
||||
end
|
||||
|
||||
def matches?(record)
|
||||
|
||||
@@ -48,7 +48,7 @@ module ValidatesTimeliness
|
||||
compare = compare.send(type_cast_method)
|
||||
|
||||
unless value.send(method, compare)
|
||||
add_error(record, attr_name, error_messages[option] % compare.strftime(display))
|
||||
add_error(record, attr_name, option, :restriction => compare.strftime(display))
|
||||
end
|
||||
rescue
|
||||
unless ValidatesTimeliness.ignore_restriction_errors
|
||||
@@ -58,16 +58,25 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def add_error(record, attr_name, message)
|
||||
message = error_messages[message] if message.is_a?(Symbol)
|
||||
record.errors.add(attr_name, message)
|
||||
def add_error(record, attr_name, message, interpolate={})
|
||||
if Rails::VERSION::STRING < '2.2'
|
||||
message = error_messages[message] if message.is_a?(Symbol)
|
||||
message = message % interpolate.values unless interpolate.empty?
|
||||
record.errors.add(attr_name, message)
|
||||
else
|
||||
custom = custom_error_messages[message]
|
||||
record.errors.add(attr_name, custom || message, interpolate)
|
||||
end
|
||||
end
|
||||
|
||||
def error_messages
|
||||
return @error_messages if defined?(@error_messages)
|
||||
custom = {}
|
||||
configuration.each {|k, v| custom[$1.to_sym] = v if k.to_s =~ /(.*)_message$/ }
|
||||
@error_messages = ValidatesTimeliness.default_error_messages.merge(custom)
|
||||
@error_messages = ValidatesTimeliness.default_error_messages.merge(custom_error_messages)
|
||||
end
|
||||
|
||||
def custom_error_messages
|
||||
return @custom_error_messages if defined?(@custom_error_messages)
|
||||
@custom_error_messages = configuration.inject({}) {|h, (k, v)| h[$1.to_sym] = v if k.to_s =~ /(.*)_message$/;h }
|
||||
end
|
||||
|
||||
def self.restriction_value(restriction, record, type)
|
||||
|
||||
Reference in New Issue
Block a user