title: Rails Internationalization: Setting the Locale from the Domain Name
body: <p>I'm trying to implement internationalization on my rails app. Here's part of my application controller</p>
  
  <pre><code>before_action :set_locale
  
  def set_locale
    I18n.locale = extract_locale_from_tld || I18n.default_locale
  end
  
  def extract_locale_from_tld
    parsed_locale = request.host.split('.').last
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale  : nil
  end
  </code></pre>
  
  <p>It seems not to work and I can only set the locale from url params making me use <code>scope "(:locale)", locale: /en|se/ do</code> in my routes which I don't want to. </p>
  
  <p>From the rails guide, they point out that the switching menu should be implemented like this. </p>
  
  <pre><code>link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
  </code></pre>
  
  <p>how do you implement this? 
  My current switching menu looks like this.</p>
  
  <pre><code>&lt;% if I18n.locale == I18n.default_locale %&gt;
     &lt;li&gt;&lt;%= link_to image_tag("eng.png", :alt =&gt; "England", :class =&gt;"round"), :locale=&gt;'en' %&gt;
     &lt;li&gt;&lt;h5&gt;&lt;%= link_to_unless I18n.locale == :se, "Swedish", "#{'http://www.natkurser.se'}" %&gt;&lt;/h5&gt;  
  &lt;% else %&gt;
     &lt;li&gt;&lt;%= link_to image_tag("swe.png", :alt =&gt; "Sweden", :class =&gt;"round"), :locale=&gt;'se' %&gt;
     &lt;li&gt;&lt;h5&gt;&lt;%= link_to_unless I18n.locale == :en, "English", "#{'http://gettheskill.com'}" %&gt;&lt;/h5&gt;  
  &lt;%end%&gt;
  </code></pre>
  
  <p>I've added 127.0.0.1 gettheskill.com and 127.0.0.1 natkuser.se to /etc/hosts but it still doesn't work on developement. What file(s) do I modify so that it works on production? I'm thinking nginx configuration files. And ultimately, how is the routes supposed to appear. This is the main thing that seems to have been left out in rails internationalization documentation. A detailed answer would be appreciated.</p>