Rails optional /:locale route

青春壹個敷衍的年華 提交于 2019-11-30 05:23:51

问题


I'm trying to setup a routing system for my rails app that allows for an optional route (/:locale) to be allowed to the base of the website.

So more or less:

/en/home/ would goto the same page as /home/ /en/people/ -> /people/

The only issue I'm having is setting this up in the routes config.


回答1:


Use scope '(:locale)' do...end. You can see an example from Agile Web Development with Rails here:

http://intertwingly.net/projects/AWDwR4/checkdepot-30/section-15.1.html




回答2:


What I usually do is, in config/routes.rb:

MyApp::Application.routes.draw do

  scope "(:locale)", :locale => /en|fr/ do
    #here only two languages are accepted: english and french

  end
end

And in my ApplicationController:

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || "en"
end


来源:https://stackoverflow.com/questions/6636140/rails-optional-locale-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!