Reputation: 171
I have form with dropdown menu, which is populated from model:
<%= f.association :cargo_price, :label_method => :price, :value_method => :id %>
There is model called cargo_price with only ID and price columns. I would like to i18n this selection. Is there any best practice for such a case?
I had an idea to rename price column to price_en, and add another language mutation as separate columns. But I do not know, how to instruct simple_form to load correct column as label.
Upvotes: 0
Views: 1085
Reputation: 171
I had to figure solution by myself, based on already solved similar problems, described on Internet forums and QA. Prerequisite is, there is model CargoPrice, with 3 records:
ID | Price
---------------
1 | all_in
2 | plus_plus
3 | make_offer
So first of all I created new method in CargoPrice model, this method is named translated_price:
def translated_price
I18n.t(price, :scope => 'cargo_price')
end
Next step was to define correct translation to en.yaml / other language file:
en:
cargo_price:
all_in: "All in"
plus_plus: "++"
make_offer: "Make an offer"
And last step was to change form to (note label_method):
<%= f.association :cargo_price, label_method => :translated_price, :value_method => :id %>
Now, dropdown selection box is populated from model, and every select-able option is translated.
Upvotes: 1
Reputation: 1478
Take a look at the Rails Internationalization guide. The convention is to use Rails translation helpers:
I18n.t 'store.title'
These reference values in a translation yaml file:
en:
store:
title: "Example Store"
de:
store:
title: "Beispiel Speicher"
In your case, it sounds like you want to apply translations to your model column names. I would not advise doing that, your cargo_price
model should be the same regardless of the language it is being translated into.
Upvotes: 0