fix i18n locale load order to allow customisation

fix interpolation values for i18n
This commit is contained in:
Adam Meehan 2009-06-19 12:06:36 +10:00
parent 4ac4281c8b
commit d61dddfbc6
3 changed files with 3 additions and 9 deletions

View File

@ -31,7 +31,7 @@ module ValidatesTimeliness
def load_error_messages def load_error_messages
if defined?(I18n) if defined?(I18n)
I18n.load_path += [ LOCALE_PATH ] I18n.load_path.unshift(LOCALE_PATH)
I18n.reload! I18n.reload!
else else
defaults = YAML::load(IO.read(LOCALE_PATH))['en'] defaults = YAML::load(IO.read(LOCALE_PATH))['en']

View File

@ -94,7 +94,7 @@ module ValidatesTimeliness
restriction = [restriction] unless restriction.is_a?(Array) restriction = [restriction] unless restriction.is_a?(Array)
if defined?(I18n) if defined?(I18n)
message = custom_error_messages[option] || I18n.t('activerecord.errors.messages')[option] message = I18n.t('activerecord.errors.messages')[option]
subs = message.scan(/\{\{([^\}]*)\}\}/) subs = message.scan(/\{\{([^\}]*)\}\}/)
interpolations = {} interpolations = {}
subs.each_with_index {|s, i| interpolations[s[0].to_sym] = restriction[i].strftime(format) } subs.each_with_index {|s, i| interpolations[s[0].to_sym] = restriction[i].strftime(format) }
@ -118,7 +118,7 @@ module ValidatesTimeliness
def add_error(record, attr_name, message, interpolate=nil) def add_error(record, attr_name, message, interpolate=nil)
if defined?(I18n) if defined?(I18n)
custom = custom_error_messages[message] custom = custom_error_messages[message]
record.errors.add(attr_name, custom || message, interpolate || {}) record.errors.add(attr_name, message, { :default => custom }.merge(interpolate || {}))
else else
message = error_messages[message] if message.is_a?(Symbol) message = error_messages[message] if message.is_a?(Symbol)
message = message % interpolate message = message % interpolate

View File

@ -506,12 +506,6 @@ describe ValidatesTimeliness::Validator do
configure_validator(:type => :date, :before => before) configure_validator(:type => :date, :before => before)
validator.send(:interpolation_values, :before, before.to_date).should == {:restriction => before} validator.send(:interpolation_values, :before, before.to_date).should == {:restriction => before}
end end
it "should return empty hash if no interpolation keys are in message" do
before = '1900-01-01'
configure_validator(:type => :date, :before => before, :before_message => 'too late')
validator.send(:interpolation_values, :before, before.to_date).should be_empty
end
else else
it "should return array of interpolation values" do it "should return array of interpolation values" do
before = '1900-01-01' before = '1900-01-01'