Rails optional /:locale route

后端 未结 2 1054
梦如初夏
梦如初夏 2021-01-05 17:58

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:

<
2条回答
  •  礼貌的吻别
    2021-01-05 18:14

    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
    

提交回复
热议问题