Forum: Ruby on Rails I found a way to dynamically switch languages in your rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B36621992d1986de445180b73698b561?d=identicon&s=25 sekhar (Guest)
on 2006-01-05 17:44
(Received via mailing list)
step 1) Do what is there in the manual Using Gettext To Translate Your
Rails
Application as it is.

Step2)Now create a table in your project database to store your language
names which you are supporting in the project for example "langs"

step 3)populate your table with language names, namely hindi, japanes
etc.

step 4) create a scffold for with aLang controller, just so that life is
easy.

step 5)now in your layout directory edit the <appname>.rhtml , place a
code
to select the language
<%= start_form_tag :action => 'lang_switch' %>
<%= select ("post","lang_id",Lang.find_all.collect{|p| [p.language]})%>
<%= submit_tag "Go" %>
<%= end_form_tag %>

step 6)now in your application.rb file create a method like this
def lang_switch
    if @params["post"]["lang_id"] == "my_fav_lang"  #compare the
parameters
sent by browser
        cookies[:lang] = { :value =>
"my_fav_lang_dirname_in_po_directory"}
# set the language cookie ,mostly this is like en or gb or jp etc
        flash[:notice] = 'my_fav_lang language selected.'
        redirect_to :action => 'list' #redirect the page
    end
    else if @params["post"]["lang_id"] == "my_fav_lang2"
        cookies[:lang] = { :value =>
"my_fav_lang_dirname_in_po_directory2"}

        flash[:notice] = 'my_fav_lang2 language selected.'
         redirect_to :action => 'list'
     end
  end

Step 7) thats it that does it. you select your language and click go ,
you
get a reloaded page with your favorite language.

NOTE: IT WORKED FOR ME FOR A SMALL APP LIKE A TODO APP. AND I HOPE IT
CAN BE
APPLIED TO APP OF ANY COMPLEXITY.
This topic is locked and can not be replied to.