Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid adding the default Locale in generated URLs?

If you follow the I18N Rails Guide, all generated links contain the locale parameter (localhost/en/about, localhost/fr/about). This is because we use the method default_url_options to always add the Locale parameter :

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

Is it possible to remove the locale parameter from the generated url when the locale is unknown or the default one ?

What I need :

  • Locale unknown : mysite/about
  • Locale en : mysite/about (and not localhost/en/about)
  • Locale fr : mysite/fr/about

I tried to only set the locale if it was not the default one, but the result is that the generated links never contain the locale parameter...

I tried many things like this one

def default_url_options(options={})
  if I18n.locale == :fr
    { :locale => I18n.locale }
  else
    { :locale => nil }
  end
end

Whole code :

ApplicationController.rb :

before_filter :set_locale
def set_locale
  I18n.locale = params[:locale]
end

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

routes.rb

scope "(:locale)", :locale => /en|fr/ do
  match 'about'   => 'static_pages#about',   :via => :get
  match 'contact' => 'static_pages#contact', :via => :get
  match '/' => 'search#index', :as => :search
end

root :to => 'search#index'
like image 614
Tom Avatar asked Mar 10 '11 15:03

Tom


3 Answers

The selected answer is totally right on and provides everything you need to redirect URLs without a locale to your default locale, but I wanted to do something a bit more and figured I'd share it with you.

I wanted to avoid having to use the default locale at all, that is to say

  • mysite.com/en/page should be the same as
  • mysite.com/page

AND all links when viewing from the default locale should NOT include the locale, meaning mysite.com should have links that do not include the default locale (en) in them. Instead of mysite.com linking to

  • mysite.com/en/page it should link to
  • mysite.com/page

I achieved this via the following edits to default_url_options:

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

You must define config.i18n.default_locale in config/application.rb for this to work. You can also assign fallbacks for unsupported locales via config.i18n.fallbacks = [ :en ] where that array is a priority-ordered list of languages to fallback to.

like image 75
fredugolon Avatar answered Nov 01 '22 19:11

fredugolon


Ok I understand much better. Indeed, you almost did it.

You just need a very useful operator in Ruby: ||

If the first value exists, it's used, otherwise the second argument is taken into account.

def set_locale
  I18n.locale = params[:locale] || :en
end
like image 27
apneadiving Avatar answered Nov 01 '22 19:11

apneadiving


Rails 4 + https://github.com/svenfuchs/routing-filter

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

def default_url_options(options = {})
  (I18n.locale.to_sym.eql?(I18n.default_locale.to_sym) ? {} : {locale: I18n.locale}).merge options
end
like image 4
everyman Avatar answered Nov 01 '22 18:11

everyman