OmniBus
OmniBus

Reputation: 874

How to write model related translation in rails?

Rails scaffold have text like:

link_to 'New Product', new_product_path
<h1>Edit Product<h1>

What is the best practice to translate model related words like 'New Product' and 'Edit Product' in Rails?

Upvotes: 0

Views: 520

Answers (1)

Maran
Maran

Reputation: 2736

I am not sure what is the best practice, I can tell you what I often use. Perhaps it can help you.

First of I define a few static translations that I can use for translations that I will have a lot, for instance for the common crud links.

For instance:

en:
  default:
    new_model: "New %{model_name}"
    edit_model: "Edit %{model_name}"
    delete_model: "Delete %{model_name}" 

I then use standard active record i18n to come up with model translations. For instance:

en:
  activerecord:
    models:
      product: Artikel 
    attributes:
      product:
       name: Naam

I can then combine the two to come up with the following example.

<%= link_to t("default.new_model", :model_name => Product.model_name.human), new_product_path %>
<h1><%= t("default.edit_model", :model_name => Product.model_name.human) %><h1>

You could even go as far as building some helpers to come up with some even more standardized ways of doing this.

I hope this helps.

Upvotes: 1

Related Questions