Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

root_path takes always application default locale #30

Open
cis-deepesh opened this issue Jul 21, 2012 · 6 comments
Open

root_path takes always application default locale #30

cis-deepesh opened this issue Jul 21, 2012 · 6 comments

Comments

@cis-deepesh
Copy link

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.

@francesc
Copy link
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

@cis-deepesh
Copy link
Author

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"}

@francesc
Copy link
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.

@cis-deepesh
Copy link
Author

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)

@francesc
Copy link
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.

@Tioneb12
Copy link

Tioneb12 commented May 24, 2018

I have the same problem... how can i do to set locale for the root_path ? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants