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

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.
5190273ecab4b93b2d4e7529dcc1f7c6?d=identicon&s=25 unknown (Guest)
on 2006-01-05 19:57
(Received via mailing list)
can you share the code so that others can benefit out of it???

silvy mathews

________________________________

From: rails-bounces@lists.rubyonrails.org
[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of sekhar
Sent: Thursday, January 05, 2006 7:14 AM
To: rails@lists.rubyonrails.org
Subject: [Rails] I found a way to dynamically switch languages in your
railsapp -- look at it and give your opinions


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.