Rails optional /: local route

I am trying to configure a routing system for my rails application, which allows me to specify an additional route (/: locale) for the site base.

So more or less:

/ ru / home / will go to the same page as / home / / ru / people / -> / people /

The only problem I encountered is setting this option in route configurations.

+6
source share
2 answers

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

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

+6
source

What I usually do 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 
+2
source

Source: https://habr.com/ru/post/892401/


All Articles