Question

I have a website with 2 languages. I want to create a subdomain for each locale. For example: en.site.com and fr.site.com.
I've googled, but no luck. I've only found solutions that extract locale name from query, for example: site.com/en/post/1
How can I implement such thing?

Was it helpful?

Solution

you can find an example in the rails guides: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name

its about domain names, but you can adapt it to your needs pretty easily.

keep in mind that subdomains introduce a lot of complexity into your app. cookies, javascript and ssl are sensitive to domains. make sure, that it's worth using subdomains vs. paths.

OTHER TIPS

i find a simple example you can follow pretty easy in medium: https://medium.com/unexpected-token/making-your-website-multi-regional-using-top-level-domains-cdbbdb951b65

the idea is to define a clear one-to-one mapping between the locale and the host

HOSTS_MAPPING = { 
   'en' => 'en.example.com',
   'fr' => 'fr.example.com'
 }

then use new mapping in ApplicationController

class ApplicationController < ActionController::Base
  before_action :set_locale

  def set_locale
    I18n.locale = HOSTS_MAPPING.invert[request.host] || I18n.default_locale
  end
end

it's say that your host en.example.com will use locale en

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top