There's no internationalisation integrated in Ruby on Rails, but it is very easy to develop websites in multipple languages with it. I'll post here how I did translate the error messages generated by ActiveRecord validation rules
At first, warning: I'm using an as yet unreleased translation plugin. The reason it is not yet available is that I haven't had time to package it right. I have a controller used to manage the translations, and I haven't had the time to really look for a solution to package it in the plugin. If you're interested i can send you a tgz.
The first thing to do, is in the ActiveRecord model, tweak the error message returned for each validation. In the ActiveRecord model, I don't have access to all data needed to determine which language to display. So I only return an id for the translation to display. For example:
validates_confirmation_of :email, :on => :create, :message => "myowndb_email_not_confirmed"
This will return the error message "myowndb_email_not_confirmed" when the email and its confirmation don't match.
My prefered way for validation is to use validates_each, which gives you the most flexibility. Let's say you require a login to be entered during registration, and this login should match a regexp and be at least 6 characters long.. If the login provided is empty, you just want to display a message like "Please provide a login", and not an error message for each validation that failed: "Login too short" and "Login format invalid". Well, that's easy:
validates_each :login do |record, attr, value|
if value.length>0
if value.length<6
record.errors.add attr, 'madb_login_too_short'
end
if !value.match /^[_\w-]+(\.[_\w-]+)*@[\w-]+(\.\w+)*(\.[a-z]{2,3})$/
record.errors.add attr, 'madb_login_not_a_valid_email'
end
if User.count(["login = ?", value])>0
record.errors.add attr, 'madb_login_already_taken'
end
else
record.errors.add attr, 'madb_login_cannot_be_blank'
end
end
This code displays one error message when the login provided is empty, and 1 to 3 error messages when the non-empty login provided fails validation.
Now, in the view, I replace
error_messages_for("user")
by
<% if @user.errors.count>0 %>
<%=t("madb_error_saving_user")%>
<% @user.errors.each do |attr,e| %>
- <%= t(e) %>
<% end %>
<%end%>
As you can see, I don't print the error message directly, I pass it to a method t that that is provided by the translation plugin, and which does all the work: determine the language to translate to, get this translation from a database and return it. That's it!
PS: Can someone tell me how I should do to format code with wordpress?