Search code examples
ruby-on-railsactiverecordinternationalizationrails-i18n

ActiveRecord RecordNotFound localization


I am trying to localize my rails application (webservice). I installed the gem 'rails-i18n', which works fine.

Except that it does not translate ActiveRecord::RecordNotFound message. In rails code: https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/relation/finder_methods.rb

raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"

It seems that the message is hard coded.

Is there any solution, apart not using the Model.find_by_'attribute' ?


Solution

  • Sufficient for my reason to tell user more informative message:

    exception.message.match /^Couldn't find (\w+) with (id=([\S]*))?/
    msg = t 'activerecord.exceptions.not_found', klass: $1, id: $3
    

    and since I wanted a czech transaltion:

    cs:
      activerecord:
        exceptions:
          not_found: "Nelze nalézt %{klass} s id=%{id}"
    

    ..when is something hard coded the only way is to hard de-code it