francesc / rails-translate-routes

MIT License
147 stars 50 forks source link

root_path takes always application default locale #30

Open cis-deepesh opened 12 years ago

cis-deepesh commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 6 years ago

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