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

时间:2014-07-15 15:33:37

标签: ruby-on-rails ruby-on-rails-3 routes

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

1 个答案:

答案 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