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.
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.