Rails optional /:locale route

2020-03-03 05:13发布

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.

2条回答
仙女界的扛把子
2楼-- · 2020-03-03 05:27

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
查看更多
SAY GOODBYE
3楼-- · 2020-03-03 05:47
登录 后发表回答