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.