Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use rails-i18n on ActiveRecordError with association?

I'm trying to translate https://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/associations.rb

In my controller file I have:

@book = Book.find(params[:id])

begin
  @book.destroy
rescue ActiveRecord::DeleteRestrictionError => e
  flash[:error]= e.message # <<< Translate this message ?
end

This is the translation file I use : https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/th.rb

How do I write code for translate "#{e.message}"?

like image 499
user765435 Avatar asked Nov 14 '22 13:11

user765435


1 Answers

You can use this in your en.yml file

activerecord:
   book:
    error: 'book error: %{e}'

and u can chane ur rescue block with this

flash[:error] = t("book.error") % {:e => e.message}

this works in ma case

like image 189
rajith Avatar answered Dec 20 '22 03:12

rajith