robzdc
robzdc

Reputation: 312

How to use same route path for both locales

I have this code in routes.rb.

scope ":locale", :locale => /es/  do
    match "inicio" => "home#index", :as => "home"
    match "acerca-de" => "about#index", :as => "about"
    match "servicios" => "services#index", :as => "services"
    match "blog" => "blog#index", :as => "blog"
    match "contacto" => "contact#index", :as => "contact"
  end

  scope ":locale", :locale => /en/  do
    match "home" => "home#index", :as => "home"
    match "about" => "about#index", :as => "about"
    match "services" => "services#index", :as => "services"
    match "blog" => "blog#index", :as => "blog"
    match "contact" => "contact#index", :as => "contact"
  end

What I'm trying to do is have a route like /es/acerca-de and /en/about which use the same controller and have the same url_path() so when I'm at spanish language the about_path() sends you to /en/about but when I'm at english language the about_path() sends you to /es/acerca-de.

Upvotes: 0

Views: 57

Answers (1)

robzdc
robzdc

Reputation: 312

Done! I the answer was practically in the ruby on rails guides...

This is the code in routes.rb

scope ":locale", :locale => /es|en/  do
 match "inicio" => "home#index", :as => "home"
 match "acerca-de" => "about#index", :as => "about"
 match "servicios" => "services#index", :as => "services"
 match "blog" => "blog#index", :as => "blog"
 match "contacto" => "contact#index", :as => "contact"
end

and added this in application_controller

class ApplicationController < ActionController::Base

 def set_locale
   I18n.locale = params[:locale] || I18n.default_locale
   Saba::Application.reload_routes!
 end

Upvotes: 1

Related Questions