Emre Emre - 4 months ago 8x
Ruby Question

Rails - Database translation on user selection values

On my rails app there is a select box with values of, fuel_types, such as, Oil, Gas, Petroleum, Diesel. The default language is en so this is fine.
When user changes website to french these data should be shown as french and when user submits the form, the data should be in fr however, I can not do it by using

oil: ""
gas: ""
petroleum: ""

Because then database data will be mixed with fr and en language. I then have to show these data in the show page, if fr or en.

Is this possible with
globalize3 gem

I see that static content in database can be translated with globalize3 but while user filling the form it is either fr or en according to locale variable then the database will be populated with en and fr values, it will make almost impossible to search data and process.

btw: I have a model called fuel_types with name id columns, where I keep oil, gas, petroleum values to show in select tag. Then I save them to car model.


In this way you are right!. But what if I have;

<%= f.collection_select(:fuel_type, Fuel.all, :name, :name, {}, class: "Select-control u-sizeFull") %>

I have model called Fuel and this fuel (belongs to car model) should be seen as the locale variable. Then should be written in one language to database?


So for edited question: third and fourth argument can be anything that responds to call so instead of using name in fourth argument use proc {|fuel| translate(fuel.name)} or any other translation method you have, just call it in proc.