如何为两个语言环境使用相同的路由路径



我在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

我想做的是有一个像/es/acerca-de/en/about这样的路由,它们使用相同的控制器并具有相同的url_path(),所以当我使用西班牙语时,about_path()将您发送到/en/about,但当我使用英语时,about_path()将您发送到/es/acerca-de

完成!如果答案实际上在ruby on rails指南中…

这是路由中的代码。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

并在application_controller

中添加
class ApplicationController < ActionController::Base
 def set_locale
   I18n.locale = params[:locale] || I18n.default_locale
   Saba::Application.reload_routes!
 end

最新更新