How to paste locale automatically in routes in Rails?

54 Views Asked by At

Can I paste the default locale or locale from the session[:lang] as locale param when I type:

domain.com/feed => domain.com/:locale/feed

Now when I type domain.com/feed it prints the next locale error:

"feed" is not a valid locale

in the

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

How can I redirect automatically? Can I?

1

There are 1 best solutions below

0
On

You can make it optional:

domain.com/feed => domain.com/(:locale/)feed, 
                   constraints: { locale: /#{I18n.available_locales.join("|")}/ }

Read 3.2 Dynamic Segments.