root_path takes always application default locale #30

Open
cis-deepesh opened this Issue Jul 21, 2012 · 5 comments

Comments

Projects
None yet
2 participants

I have translated my app in two languages using Rails 3.2.6, de and en. Setting the locale in Application Controller:

before_filter :set_locale

def set_locale
I18n.locale = params[:locale] || I18n.default_locale ##for root_path params[:locale]=de(default locale) always
end

add in the routes.rb
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml',{:no_prefixes => true })

Everything working fine except on the root_path the locale is not setting. Take always application default locale.

Please let me know any solution for this.

Thanks.

Owner

francesc commented Aug 2, 2012

Usually (in documentation examples) the set_locale method takes the params locale or the browser locale, yours take the params locale (none in the case of root) or default app locale, so the behaviour is correct. I suggest you to use the first method mentioned, params locale and browser locale.

def set_locale
  I18n.locale = params[:locale] || ((lang = request.env['HTTP_ACCEPT_LANGUAGE']) && lang[/^[a-z]{2}/])
end

Thanks for you reply really stuck on that issue.

@@francesc the params[:locale] on root is not none as you mentioned above its always "de" so the behavior is not correct
params for root_path
{"controller"=>"dashboard", "action"=>"index", "locale"=>"de"}

@cis-deepesh cis-deepesh closed this Aug 3, 2012

@cis-deepesh cis-deepesh reopened this Aug 3, 2012

Owner

francesc commented Aug 3, 2012

It's always "de" because params locale is empty and you default to I18n.default_code (which I suppose is de for you), first time in root path where you don't have a translated segment to detect the locale or you don't have localized root (/de) you have to guess the language, first thing to try is params locale, then if it's empty second logical option is to use user browser locale (which you don't), and then finally to app default.

I really don't understand where's the problem. If you make a more detailed explanation of what you want to achieve I'll try to help you.

Ok.Let me clear more.

On root_path I am getting the params[:locale] is always "de". It should be empty as you mentioned.

I am taking about params not i18n as you were assuming i think (It's always "de" because params locale is empty)

@cis-deepesh cis-deepesh closed this Aug 3, 2012

@cis-deepesh cis-deepesh reopened this Aug 3, 2012

Owner

francesc commented Aug 3, 2012

Sorry haven't noticed you're using :no_prefixes => true you should extend set_locale method to use browser detection first on root path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment