Rails routes with optional scope ":locale"
Asked Answered
H

6

14

I'm working on a Rails 3.1 app and I'd like to set specific routes for the different languages the app is going to support.

/es/countries
/de/countries
…

For the default language ('en'), I don't want the locale to be displayed in the url.

/countries

Here is the route definition I've set.

scope "(:locale)", :locale => /es|de/ do
   resources :countries
end

It works great, until I try to use a path helper with 'en' as the locale.

In the console :

app.countries_path(:locale => 'fr')
 => "/fr/countries" 

app.countries_path(:locale => 'en')
 => "/countries?locale=en" 

I don't want the "?locale=en".

Is there a way to tell rails that with an 'en' locale, the locale param should not be added to the url?

Thanks

Hotchkiss answered 22/11, 2011 at 9:4 Comment(1)
Your question answered my question more than your answer did. Parentheses around the :locale. Thanks, that's what I was looking for.Emsmus
H
22

I finally figured out how to do it easily. You just have to set the default_url_options in the app controller as below.

  def default_url_options(options={})
    { :locale => I18n.locale == I18n.default_locale ? nil : I18n.locale  }
  end

This way, you are sure the locale isn't sent to the path helpers.

Hotchkiss answered 23/11, 2011 at 5:43 Comment(3)
or set the default in the scope definition: scope "(:locale)", locale: /en|es/, defaults: {locale: "en"} doPectoralis
unfortunately this is overriding the other localesEaster
perfect! no need defaults in routesDevilry
E
25

This SHOULD be a better solution:

In your routes.rb,

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/, defaults: {locale: "en"} do

As MegaTux said, set defaults: {locale: "en"} in the scope.

The advantage: The jlfenaux solution works in most contexts, but not all. In certain contexts (like basically anything outside of your main controllers and views), the path helpers will get confused and put the object or object.id in the locale parameter, which will cause errors. You'll find yourself putting locale: nil in lots of path helpers to avoid those errors.

The possible problem: It seems that defaults: {locale: "en"} always overrides any other value you pass in for locale. The option is named default, so I'd expect it to assign locale to 'en' only when there's no value already, but that's not what happens. Anyone else experiencing this?

Emsmus answered 12/7, 2014 at 11:58 Comment(6)
I have the same Problem.Maggio
Yes, same issue. It always takes en as defaultOmor
Yep. Same issue. Can someone solve this disadvantage?Easter
I'm not sure about not displaying en in the url like the question asks - but I found this answer helpful, but realised all my links, even when the UI language was not English, had English as their locale. so I had to set the default locale as jifenaux did it in the accepted answer rather than as this answer shows it done inside the routes fileBarquentine
Be sure to use strings!Halfback
@Halfback Using string as keys in the defaults hash works for the default but it introduces another issue, link helpers will suddenly have both a string and a symbol with the same name and it gets confused. No route matches {... "muh"=>"xlm", :muh=>"xlx" ...}Cellulous
H
22

I finally figured out how to do it easily. You just have to set the default_url_options in the app controller as below.

  def default_url_options(options={})
    { :locale => I18n.locale == I18n.default_locale ? nil : I18n.locale  }
  end

This way, you are sure the locale isn't sent to the path helpers.

Hotchkiss answered 23/11, 2011 at 5:43 Comment(3)
or set the default in the scope definition: scope "(:locale)", locale: /en|es/, defaults: {locale: "en"} doPectoralis
unfortunately this is overriding the other localesEaster
perfect! no need defaults in routesDevilry
F
4

I'm doing a combination of what @Arcolye and @jifenaux are doing, plus something extra to keep the code as DRY as possible. It might not be suitable for everybody, but in my case, whenever I want to support a new locale I also have to create a new .yml file in config/locales/ anyways, so this is how it works best for me.

config/application.rb:

locale_files = Dir["#{Rails.root}/config/locales/??.yml"]

config.i18n.available_locales = locale_files.map do |d| 
  d.split('/').last.split('.').first
end

config.i18n.default_locale = :en

config/routes.rb

root_path = 'pages#welcome'

scope '(:locale)', locale: /#{I18n.available_locales.join('|')}/ do
  # ...
end

root to: root_path
get '/:locale', to: root_path

app/controllers/application_controller.rb:

private

def default_url_options(options = {})
  if I18n.default_locale != I18n.locale
    {locale: I18n.locale}.merge options
  else
    {locale: nil}.merge options
  end
end
Feculent answered 7/2, 2016 at 0:2 Comment(1)
you can put the root_path into the locale scope and don't need the get route anymoreSherman
W
3

If you don't want the query string you don't have to pass it to the helper:

1.9.2 (main):0 > app.countries_path(:locale=>:de)
=> "/de/countries"
1.9.2 (main):0 > app.countries_path
=> "/countries"
1.9.2 (main):0 > app.countries_path(:locale=>:en)
=> "/countries?locale=en"
1.9.2 (main):0 > app.countries_path
=> "/countries"
1.9.2 (main):0 > app.countries_path(:locale=>nil)
=> "/countries"
Walkin answered 22/11, 2011 at 9:30 Comment(1)
I'm aware of that. But the code I use in my views are designed for all locales, so I have to pass it to the helper. I'd rather not test each time if the local is en...Hotchkiss
I
1

In my case I solved this problem using this technique:

class ApplicationController < ActionController::Base
  layout -> {
    if devise_controller?
      'devise'
    end
  }

  before_action :set_locale

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

  def url_options
    { :locale => I18n.locale }.merge(super)
  end

end
Isolde answered 8/7, 2019 at 9:19 Comment(1)
Can you elaborate what is this layout?Piper
C
0

If you decide to put default_url_options in the application_controller to fix your path helpers, keep in mind you might want to put it in your admin's application_contoller as well

Corroboree answered 13/6, 2018 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.