jeudi 2 juin 2016

Rails - Globalize the database checkboxes, select tags and radiobuttons

I have a rails app with user and boat models. Im trying to use globalization or internationalization with checkboxes & radio buttons but I have couple problems. I am trying to launch the site with 2 languages en & fr. I have a locale variable which keeps the language selection so it stored as params[:locale] either en or fr

1. Radio Button

So I have radio buttons that user can select the type of the boat. Such as Catamaran, Gulet, Motoryatch etc. I know that I can show the labels with en or french such as;

            <label class="btn btn-primary list-button max-width-none known_boat" > 
              <%= image_tag "catamaran.png"%>
              <br>
              <%= t('boats.new.catamaran') %>
              <%= f.radio_button(:boat_type, "Cataraman", class: "display-none") %>
            </label>

While <%= t('boats.new.catamaran') %> would show the the language depending on locale variable. As <%= f.radio_button(:boat_type, "Cataraman", class: "display-none") %> says Catamaran, it will be saved to database as Catamaran in english.

Then when user wants to edit the :boat_type in input field. They see the value as Catamaran in english again. So I should be translating it if the locale is fr but dont know how.

I think I should do something like what what should I write to value;

<%= f.text_field :boat_type, :value => @boat.boat_type #translation of Catamaran  ,class: 'form-control' %>

Lastly, I would like to show the boat type in boat#show action as;

<%= @boat.boat_type %>

But that also should be set according to locale variable either en or fr.

2.1. Collection Select

So I have collection select where values are stored in the database. I have fuel model and user can select the :fuel_type as;

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

Fuel types are, diesel, gas, electric and other. Same goes there when user selects the type it will be saved to :fuel_type. The Fuel model is stored in english and I should show en or fr then when user goes to boat#show action this value should be seen according to locale variable en or fr as;

<%= @boat.fuel_type %>

2.2. Select tag

I have :reservation_type and same goes here, I show en or fr user selects and saves en to english and when I show it, it shows according to locale var.

<%= f.select :reservation_type, [['Sailor','Sailor'],['Chef','Chef'],['Hostess','Hostess'],['Babysitter','Babysitter'],['Other','Other']], {}, {class: "Select-control u-sizeFull", id: "extra_crew"} %>

3. Checkboxes

I have checkbox model called Features which belongs to boat. It is many to many through featureboat.

I have these features;

enter image description here

and I call them as;

              <% Feature.all.each do |feature| %>
                <%= check_box_tag "boat[feature_ids][]", feature.id, @boat.feature_ids.include?(feature.id), id: dom_id(feature) %>
                <%= label_tag dom_id(feature), feature.name %> </br>
              <% end %> 

So I think I should change the feature.name part in label tag but then how I should show these in fr in boat#show action as they are in english in database. Currently they are all english regardless the language.

-------------------------------------*****-------------------------------------

Sorry for the long post, I have put all them here as they all relevant with the language translation. I am a bit stuck here any my app almost complete. I am really appreciate any help I can get!. Thank you in advance




Aucun commentaire:

Enregistrer un commentaire