Search code examples
ruby-on-rails-3internationalizationformtasticdropdownbox

How to create formtastic form with multiple language support (Internationalization)


I need to set up two languages for a single form using internationalization.

This is the proposed form page

<%= semantic_form_for @detail do |f| %>
    <%= f.inputs do %>
    <%= f.input :name %>
    <%= f.input :dob  %> 
    <%= f.input :gender, :as => :radio, :label => "Gender", :collection => [["Male", 'male'], ["Female", 'female']] %>  
  <% end %>
  <%= f.actions do %>
    <%= f.action :submit, :as => :input %>
  <% end %>
<% end %>

This is wk.yml file

wk: 
formtastic:
    labels:
      detail:
        dob: "Data of birtha"
        name: "Youre Nama"
        gender: "Gendera""

This is en.yml file

en: 
  formtastic:
    labels:
      detail:
        dob: "Date of Birth"
        name: "Your Name"
        gender: "gender"

I have added Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true in formtastic.rb initializer.

I was successful in using en.yml.

I need to switch from 'en' to 'wk' and vise-verse.

How to achieve it via drop down box?


Solution

  • That's not something related to formstatic, but rather to your rails code.

    All you have to do in order to switch to wk is

    http://guides.rubyonrails.org/i18n.html

    I18n.locale = :wk
    

    In order to let your client to choose his language for the website, probably a good place to start is on this link: http://guides.rubyonrails.org/i18n.html

    If all you want is to update the form (and not the rest of the website) in different languages on a user action, like selecting the language from a select box, you can use an ajax listener on the select box, that could require something like "http://www.yourwebsite.com/:locale/form/new" which will answer with an ajax action and will replace your form with the selected language (so on :locale you will pass the value of your select box for the language).