0

I want my app has multiple languages. To do this, I read railscasts #138

But there, the writer put a language column to User model and thus users can see pages only in their language as I understand right. But I want my website can be seen in any language by any user just like usual.

How can this be done?

kalahari
  • 895
  • 5
  • 15
  • 34

3 Answers3

3

I have solved. I added to controllers/application_controller.rb this:

before_filter :set_locale
def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

private

def default_url_options(options={})
  logger.debug "default_url_options is passed options: #{options.inspect}\n"
  { :locale => I18n.locale }
end

Then I added this to views/layouts/_header.html.erb:

<li><% if I18n.locale == I18n.default_locale %>
      <%= link_to "Türkçe", :locale=>'tr'%>
    <% else %>
      <%= link_to "English", :locale=>'en'%>
    <%end%></li>

Then to config/routes.rb this:

scope "(:locale)", :locale => /en|tr/ do   # at the beginning
  match '/home' , to: 'static_pages#home'

  match '/help' ,  to: 'static_pages#help'

  match '/about' ,  to: 'static_pages#about'
  .....
end

Thats it!

kalahari
  • 895
  • 5
  • 15
  • 34
0

You might want to take a look at this great Rails guide.

Pierre-Louis Gottfrois
  • 17,561
  • 8
  • 47
  • 71
  • Thank you for reply :) Actually I read 1/3 of it and then realized I am not understanding most of it. I have 2 days to translate my website. It is now English. I mean, I don't think I have enough time to deeply understand this guide. Can you just tell me the exact way of question if you know? – kalahari Sep 19 '13 at 11:12
0

in addition to @Pierre-Louis answer, you can look at globalize3 gem

okliv
  • 3,909
  • 30
  • 47
  • Thank you too :) I skimmed this gem through [here](http://railscasts.com/episodes/338-globalize3) and it seemed to me is designed to change database based values' languages. Am I misunderstood it? If the answer is in there somewhere, I will look at it. – kalahari Sep 19 '13 at 11:16
  • this gem has methods to create some additional db migrations based on what model's attributes (fields in db) need to be translated… you need to store translations somewhere - right? so you have to modify db. and rails cast you mentioned can help you a lot to understand how todo it. – okliv Sep 19 '13 at 11:38
  • I realized that I don't need to change the data from database to multiple languages. I just need to translate website pages. Thank you anyway – kalahari Sep 20 '13 at 07:36
  • than you have to use `t('page.translated_snippet')` syntax in `html.erb` files with `your-locale.yml` definition inside `config` folder – okliv Sep 20 '13 at 10:18
  • note, that you asked question, you have received some direct answers and even if you realized that you wanted to ask something else, we spent our time for you, so it would be nice to accept one of our answers... – okliv Sep 20 '13 at 10:22
  • I don't want to ask something else. I never mentioned to change database information. Actually I solved my problem now, but none of your answers is the right answer for my question. If I accept them, this would lead other people to misdirections. I am very thankful for your efforts. I wish I knew a way to send you my answer and then you change yours with that and I accept. Do you know any? – kalahari Sep 20 '13 at 11:11
  • I answered myself. If you copy this answer in 1 hour, I will accept it from yours, if you won't I will accept my own answer. I hope you see this – kalahari Sep 20 '13 at 11:21
  • `I just need to translate website pages…` your answer not includes solution for this, to be honest… you just solved how to display language switcher with correct routes handling... never mind. glad you found the solution you searched – okliv Sep 20 '13 at 11:44
  • The `railscasts` link which I gave in the question and my answer together is the solution itself. `But there, the writer put a language column to User model and thus users can see pages only in their language as I understand right. But I want my website can be seen in any language by any user just like usual.` I asked this and I think you may misunderstood the question. – kalahari Sep 20 '13 at 12:20