enriclluelles/route_translator

Bug: unlocalized path name doesn't include locale

Closed this issue · 3 comments

Bug description

Doing something like

<ul>
  <li><%= web_nutritional_values_path(locale: :fr) %></li>
  <li><%= web_nutritional_values_path(locale: :en) %></li>
  <li><%= web_nutritional_values_path(locale: "fr") %></li>
  <li><%= web_nutritional_values_path(locale: "en") %></li>
</ul>

will generate all identical URLs in the default locale

Capture d’écran, le 2024-07-17 à 12 53 57

Steps to reproduce

Default configuration is used but it doesn't matter if generate_unlocalized_routes is enabled or not except that if it is, the locale is shown as a GET parameter while my routes are under the localized do ... end block

Expected behavior

web_nutritional_values_path(locale: "fr") should behave the same as web_nutritional_values_fr_path

Actual behavior

It acts as if no locale was passed at all.

Reproducible reduced test case

No response

Ruby version

3.3.3

Ruby on Rails version

7.1.2

Route Translator version

14.1

I18n configuration

I18n.available_locales = %i[fr en]
I18n.default_locale = :fr
I18n.enforce_available_locales = false

Route Translator configuration

RouteTranslator.config do |config|
end

Rails routes source

Rails.application.routes.draw do
    scope "/", module: :web, as: :web do
      localized do
        resources :nutritional_values, only: %i[index show], param: :slug
      end
   end
end

Rails locales

fr:
  routes:
    controllers:
      web/nutritional_values:
        nutritional_values: valeurs-nutritives

en:
  routes:
    controllers:
      web/nutritional_values:
        nutritional_values: nutritional-facts

Rails routes

                     web_nutritional_values_en GET       /en/nutritional-facts(.:format)                                                                   web/nutritional_values#index {:locale=>"en"}
                     web_nutritional_values_fr GET       /valeurs-nutritives(.:format)                                                                     web/nutritional_values#index {:locale=>"fr"}
                      web_nutritional_value_en GET       /en/nutritional-facts/:slug(.:format)                                                             web/nutritional_values#show {:locale=>"en"}
                      web_nutritional_value_fr GET       /valeurs-nutritives/:slug(.:format)                                                               web/nutritional_values#show {:locale=>"fr"}

Bug report checklist

  • I have searched for existing issues and to the best of my knowledge this is not a duplicate
  • I understand that debugging Route Translator is a time consuming task, and I have carefully provided all the information requested

Hi,

I guess that this approach never worked

Ref: https://github.com/enriclluelles/route_translator/wiki/Generating-translated-URLs

I would try with a block:

<li><%= I18n.with_locale(:fr) { web_nutritional_values_path } %></li>
<li><%= I18n.with_locale(:en) { web_nutritional_values_path } %></li>

Hi,

I guess that this approach never worked

Ref: https://github.com/enriclluelles/route_translator/wiki/Generating-translated-URLs

I would try with a block:

<li><%= I18n.with_locale(:fr) { web_nutritional_values_path } %></li>
<li><%= I18n.with_locale(:en) { web_nutritional_values_path } %></li>

Hi @tagliala

Thanks for the quick reply!

Could we convert this into a feature request then? :)

I am not seeing any useful use case of having to do conditional statements in my views or anywhere really. I'm not sure why we would want multiple path helpers for the same page per locale either.

The way I see it is if I have an header, a footer, or a sign in button, we all want to link to the same page but in the right locale.

This is where default_url_options comes very handy so I can omit the locale parameter in all my path helpers.

Also, if I were to have a button like "Switch to english", I would simply make its url be url_for(locale: "en")

Exemple

<footer>
  <%= link to "Home", home_path %>
  <%= link to "About (Current lang)", about_path %>
  <%= link to "About (EN lang)", about_path(locale: "en") %>
  <%= link to "Sign in", sign_in_path %>
  <%= link to "Switch to english", url_for(locale: "en") %>
</footer>

Some disclaimers:

  1. I'm not the original author of this gem
  2. When I've started maintaining it, the situation was already the same
  3. I wrote the wiki because I had to deal with this situation myself
  4. I'm not using route_translator in production since 9 years

Could we convert this into a feature request then? :)

With "feature", do you mean "link to the same pages in all the alternative locales"?

As I mentioned before, we have a wiki for this because it depends from the use case

As far as I can tell, there is no silver bullet, so I would avoid to add this feature, because of the many edge cases we can have

I am not seeing any useful use case of having to do conditional statements in my views or anywhere really.

Didn't get this either, where are those conditional statements?

Also, if I were to have a button like "Switch to english", I would simply make its url be url_for(locale: "en")

url_for(locale: ...) should work to have a link to the same page in a different locale