ruby on rails - How to access translation key for ActiveModel validation error? -


i have situation, wan't store translation key validation error in db instead of error message self. imagine following situation:

class car < activerecord::base   validates_presence_of :year, :fuel end  car = car.new(:fuel => 'diesel') car.save! #=> activerecord::recordinvalid 

now if call:

car.errors #=> :year=>["can't blank"] 

i translated error message.

instead extract translation key generate (in case think errors.messages.blank), can store in database in different model e.g. failedcar can later on generate i18n customized form filling in missing information manually in view.

update

i think it's this method need hook into. want fetch key , options returned, can perform translations again @ later point in time.

okay got it!

the solution not patch or on rails side of things - instead - answer lies in i18n gem.

i18n, rails uses translations standard, has ability plugin new backends provide more flexibility it. in case, backend called metadata needed. when adding i18n::backend::simple.include(i18n::backend::metadata) initializer, gives me possibility of extracting translation related information directly error message string adding method call translation_metadata.

a fantastic simple solution complicated problem :-)


Comments