Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect To Same Page In New Domain after Locale changes in Rails 3

Application using Rails 3.2.8 with below gems

gem 'friendly_id', '~> 4.0'
gem 'route_translator'

In /config/initializers/i18n.rb

TLD_LOCALES = {
  "com"  => :en,
  "jobs" => :en,
  "net"  => :en,
  "in"   => :en,
  "de"   => :de,
  "ch"   => :de,
  "at"   => :de,
  "br"   => :pt,
  "ar"   => :es,
  "cl"   => :es,
  "mx"   => :es 
}

In /app/controllers/application_controller.rb, uses a before-filter to set the locale for each request:

before_filter :set_auto_locale
def set_auto_locale
  I18n.locale = TLD_LOCALES[request.host.split('.').last]
end

In routes.rb

localized do
  match "label_vacancies/:view_job"=>"job_seekers#view_job"
  get "label_aboutus", :to => "home#about_us", :as => "about_us"
end

While user requests to change language locale, below domains should load based on locale user requested.

In initializers

domain_based_on_locale = {
    :en => "xxxxx.com",
    :de => "xxxxx.de",
    :es => "xxxxx.mx",
    :pt => "xxxxx.com.br"   
}

In /app/controllers/application_controller.rb

def set_manual_locale
  if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
    cookies['locale'] = { :value => params[:locale], :expires => 1.year.from_now }
    I18n.locale = params[:locale].to_sym
  elsif cookies['locale'] && I18n.available_locales.include?(cookies['locale'].to_sym)
    I18n.locale = cookies['locale'].to_sym
  end
  if domain_based_on_locale[I18n.locale] != request.host
    redirect_to "#{request.protocol}#{domain_based_on_locale[I18n.locale]}#{request.fullpath}", :status => :moved_permanently 
  else
    redirect_to root_path
  end
end

In this case, user changing language in URLs like below are having issue to redirect because the same page has different URL according to language.

Aboutus:
http://xxxxxxx.com/about-us  # About us route in English
http://xxxxxxx.de/uber-uns      # About us route in German
http://xxxxxxx.mx/quienes-somos # About us route in Spanish

view Job:
http://xxxxxxx.com/jobs/rp-be-company-representante-de-ventas-22042015
http://xxxxxxx.de/ofertas-de-empleo/rp-be-company-representante-de-ventas-22042015

After manual language locale change, how to redirect to same page in new domain also. And is it possible to carry running session to new domain. Thanks for your help.

like image 826
kanna Avatar asked Apr 22 '15 12:04

kanna


1 Answers

You'll need to translate each segment of request.fullpath (except for the last one, which looks like a resource slug). You can do that manually using Rails' I18n:

current_path = request.fullpath.split('/')
slug = current_path.pop
locale_path = current_path.map{|part| translate(part)}.join('/')
redirect_to "#{request.protocol}#{domain}#{locale_path}#{slug}"

Or, there are gems that handle route translation:

  • https://github.com/francesc/rails-translate-routes
  • https://github.com/enriclluelles/route_translator

As far as the session, it's not natively possible to share cookies across domains. If you make your locales subdomains (eg. de.xxxxx.com), you can share the cookie across all of them. Lots of sites do it via the path, eg. xxxxx.com/de/, which removes the problem altogether.

Supporting totally different domains requires you to transfer the session manually. You could accomplish that by something like:

  • Generate random transfer token xxx123 and save it server-side with the session it's attached to
  • Redirect to new.domain/path?token=xxx123
  • Use the token to look up the user's session and set it in their browser
  • Remove the token to prevent replay attacks

Think carefully about the transfer process - it's easy to introduce security problems when you're doing this kind of thing. This SO thread has an approach using images loaded from the other domain.

like image 96
Kristján Avatar answered Oct 17 '22 08:10

Kristján