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 displaying controller name #205

Closed
bgilbank opened this issue Feb 27, 2020 · 4 comments
Closed

Root path displaying controller name #205

bgilbank opened this issue Feb 27, 2020 · 4 comments
Labels

Comments

@bgilbank
Copy link

bgilbank commented Feb 27, 2020

Not sure why the controller name is displaying for the root path when a user clicks on the language switcher? Do I need to set a path for articles?

eg:

resrouces :articles, path '' ?

Steps to reproduce*

routes.rb

localized` do
    resources :articles
    root to: 'articles#index'
 end 

index.html.erb

 <% if I18n.locale == I18n.default_locale %>
  <%= link_to "Français", locale: "fr", only_path: true %>
  <% else %>
  <%= link_to "English", locale: "en-ca", only_path: true %>
  <% end %>

Expected behavior*

localhost/fr

Actual behavior*

localhost/fr/articles

System configuration*

Rails version: 6.0.0
Replace with output of rails -v
Ruby version: 2.6.4
Replace with output of bundle exec rails -v
Route Translator version: 7.0.1
Replace with output of cat Gemfile.lock | grep route_translator

I18n configuration*

    config.i18n.enforce_available_locales = true
    config.i18n.available_locales = ['en-ca', 'fr']
    config.i18n.default_locale = 'en-ca'
    config.i18n.fallbacks = true
    config.i18n.fallbacks = ['en-ca', 'fr']

application controller

 def set_locale
    I18n.locale = params[:locale] || http_accept_language.compatible_language_from(I18n.available_locales) ||  I18n.default_locale
  end
  
  def default_url_options(options={})
    { locale: I18n.locale }.merge(options)
  end

Thank you for your help.

@tagliala
Copy link
Collaborator

Hi!

Thanks for being part of the route_translator community.

Please use this approach:

  <%= link_to "Français", root_fr_path %>
  <%= link_to "English", root_en_ca_path, only_path: true %>

If you are interested in how to generate translated routes, please take a look at our wiki: https://github.com/enriclluelles/route_translator/wiki/Generating-translated-URLs

PS:

 def set_locale
    I18n.locale = params[:locale] || http_accept_language.compatible_language_from(I18n.available_locales) ||  I18n.default_locale
  end

Is this a before filter?

I do not advise to use a before filter. Please check the implementation of the around filter we are using: https://github.com/enriclluelles/route_translator/blob/v7.1.1/lib/route_translator/extensions/action_controller.rb#L12-L23

@bgilbank
Copy link
Author

Hi @tagliala - The language switcher is global in my header, so your implementation will only work for the home page. If a user is on the about page for instance, it will take them back to root if they switch language.

Yes, set_locale is set with a before_action call. Based on your code snippet though, I am not sure what I need to do?

@tagliala
Copy link
Collaborator

The language switcher is global in my header, so your implementation will only work for the home page. If a user is on the about page for instance, it will take them back to root if they switch language.

Please note that this is not so simple. The wiki covers the simplest case (first paragraph) => https://github.com/enriclluelles/route_translator/wiki/Generating-translated-URLs

Yes, set_locale is set with a before_action call. Based on your code snippet though, I am not sure what I need to do?

Please turn it in an around action, because of locale leaking between requests on some web servers => #44

@bgilbank
Copy link
Author

bgilbank commented Feb 28, 2020

Ok thank you, I could just check if the current page is root as well in my language links. Do you have any other recommendations?

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

No branches or pull requests

2 participants