我在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()
,所以当我使用西班牙语时{ {1}}会将您发送至about_path()
,但当我使用英语时,/en/about
会将您发送至about_path()
。
答案 0 :(得分:1)
完成! 我答案实际上是在铁轨指南上的红宝石中......
这是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
并在application_controller中添加了这个
class ApplicationController < ActionController::Base
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Saba::Application.reload_routes!
end