I've got a problem with I18n. I can't find the right way to translate the ActionController error messages like
The action '...' could not be found for ...Controller
I can't found a translation in any of this example files in this repository: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
I found a solution for validation errors etc, but not for this kind of errors.
Is it possible to translate this kind of error messages with I18n files?
update: I want to translate them because they are shown to the user. I did it like in this railscast: http://railscasts.com/episodes/53-handling-exceptions-revised
All exceptions are processed by the railsapp itself instead through middelware with this configuration:
config.exceptions_app = self.routes
Then are all exceptions routed to a single action:
match '(errors)/:status', to: 'errors#show', constraints: {status: /\d{3}/} # via: :all
In this action the error message is used by an variable:
def show
@exception = env["action_dispatch.exception"]
respond_to do |format|
format.html { render action: request.path[1..-1] }
format.json { render json: {status: request.path[1..-1], error: @exception.message} }
end
end
This variable is then shown in the production view:
<h1>Forbidden.</h1>
<% if @exception %>
<p><%= @exception.message %></p>
<% else %>
<p>You are not authorized to perform that action.</p>
<% end %>
Now the error message is dynamic. I think in general it is a good thing, because the user gets more information what went wrong and how to behave to avoid the specific error. But It is a tad too much information. I don't want them to see e.g. which controller processed the action. Therefore I want to "translate" the error messages to a safer variant of them.
You can use the exception class name as a key for your I18n
translations. That way, you can have in your translations files a key-value pair using the exception class name (essentially what the exception was) and then using your own words for that specific exception.
I18n.t(@exception.class.name.underscore, default: "Generic Error")