How to find translation for validation message?

You might wonder where is the translation for messages like:

  • Title has already been used
  • Title is invalid…

Those error messages could show when you create or update something. But the translation is a bit harder to find out. They are generated by Rails.

You can find the general format by searching key errors.format which is %{attribute} %{message}. In the example above, %{attribute} is title and %{message} are generated by errors.messages.has_already_been_used and errors.messages.is_invalid.

Those attributes are translated by keys under activerecord.attributes. For example, when Rails validates topic, if there is error, Rails will look into activerecord.attributes.topic.title. If there isn’t a translation, it will fall back to code - Title in this case.

Translation tips

  1. Find a way to present a general sentence by defining errors.format.
  2. Find activerecord.attributes.<model_name>.<attribute_name>. If there isn’t one and you want to translate it, submit a pull request where you define such a key on GitHub. (See diff below)
  3. Validation message translations are under errors.messages. However, you can use %{attribute} in the translation even it’s not appeared in the source language (en). (See diff below)

Example for tweaking zh_CN translation as a diff:

https://github.com/fantasticfears/discourse/compare/51b0b5f...2540cb2

Dev notices

  1. You can delete those dangling validation translation. See the post below and pull request above.
  2. key activerecord might be changed. When adding a new validation message, they also need a attribute here to be translated.
  3. Those error messages appear in two different place which might be confusing.

https://meta.discourse.org/t/non-existence-of-strings-in-the-source-code/36206/3?u=fantasticfears

3 Likes