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

Bug: en named route always generated ignoring config.available_locales #305

Open
2 tasks done
cbillen opened this issue Jan 5, 2024 · 2 comments
Open
2 tasks done
Labels

Comments

@cbillen
Copy link

cbillen commented Jan 5, 2024

Bug description

Hello, my route_translator.rb is setup as such:

config.available_locales = %i[en-US en-CA]
to handle routes from US or Canadian customers

The problem I have is that route translator is creating the fallback route for everything

e.g in routes.rb

localized do 
  get '/free-consultation'
end
results  in
free_consultation_en_us GET      /en-US/free-consultation
free_consultation_en_ca GET      /en-CA/free-consultation
free_consultation_en GET      /en/free-consultation

But explicitly, I want it to only generate /en_us and /en_ca. Which I thought would be the case by specifying available_locales.
Note that I have not yet created the routes in locale file since in this case the translation is the same for both locale

Other configurations

    config.disable_fallback = false
    config.force_locale = true
    config.generate_unnamed_unlocalized_routes = false
    config.locale_segment_proc = -> (locale) { lang,country_iso = locale.to_s.split('-');[lang,country_iso&.upcase].compact.join('-') }

Steps to reproduce

create a route inside a localized block in routes.rb
create a list of locales in config.available_locales
run rake routes

Expected behavior

Only generate the routes specified by available_locales regardless of fallbacks

Actual behavior

generates available_locales and fallback locales routes

Reproducible reduced test case

n/a

Ruby version

3.3

Ruby on Rails version

7.0

Route Translator version

14.1.1

I18n configuration


  config.i18n.fallbacks = true
  config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
  config.i18n.default_locale = :en
  config.i18n.available_locales = [:en, :'en-US', :'en-CA']
  config.i18n.fallbacks = {
    'en-US': :en,
    'en-CA': :en
  }
  config.enforce_available_locales = true

Route Translator configuration

config.disable_fallback = false
config.force_locale = true
config.generate_unnamed_unlocalized_routes = false
config.locale_segment_proc = -> (locale) { lang,country_iso = locale.to_s.split('-');[lang,country_iso&.upcase].compact.join('-') }
config.available_locales = %i[en-US en-CA]

Rails routes source

localized do
get '/free-consultation', to: 'www/leads#consultation'
end

Rails locales

n/a

Rails routes

                                                   free_consultation_en_us GET      /en-US/free-consultation(.:format)                                                                                                                   www/leads#consultation {:locale=>"en-US"}
                                                   free_consultation_en_ca GET      /en-CA/free-consultation(.:format)                                                                                                                   www/leads#consultation {:locale=>"en-CA"}
                                                      free_consultation_en GET      /free-consultation(.:format)                                                                                                                         www/leads#consultation {:locale=>"en"}

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
@cbillen cbillen added the bug label Jan 5, 2024
@tagliala
Copy link
Collaborator

tagliala commented Jan 5, 2024

Hi,

this is caused by the following method:

def available_locales
locales = RouteTranslator.available_locales
# Make sure the default locale is translated in last place to avoid
# problems with wildcards when default locale is omitted in paths. The
# default routes will catch all paths like wildcard if it is translated first.
locales.delete I18n.default_locale
locales.push I18n.default_locale
end

This change was introduced in #28, but at the time it was not possible to configure available_locales specific to route translator

I understand that the current route translator behavior is not expected, but I'm afraid of the consequences of excluding the default locale from the available locales

I've tried to add

      locales = RouteTranslator.available_locales
+     return locales unless locales.include?(I18n.default_locale)
+

and there are two failing specs

Error:
TranslateRoutesTest#test_path_helper_arguments_fallback:
NoMethodError: undefined method `product_it_path' for module #<Module:0x0000000126c424e0>


bin/rails test /Users/geremia/dev/route_translator/test/routing_test.rb:543

Error:
TranslateRoutesTest#test_path_helper_arguments_fallback_with_hosts:
NoMethodError: undefined method `product_it_path' for module #<Module:0x00000001269efaa8>

bin/rails test /Users/geremia/dev/route_translator/test/routing_test.rb:559

The failure is due to the fact that if there isn't a default route, the system does not know how to build a route for something that should be legit.

In rails, if I do not put the default locale among available locales I have a got a I18n::InvalidLocale error when using I18n.t without arguments

Since I'm not using this gem in a production environment, a PR with proper tests and a brief analysis of the consequences of removing the default locale from available locales is welcomed

@cbillen
Copy link
Author

cbillen commented Jan 5, 2024

Ok, thank you for the explanation

The simplest in my case is to set the default locale to en-US and config.force_locale = true

This then generates the expected routes

free_consultation_en_ca GET /en-CA/free-consultation(.:format) www/leads#consultation {:locale=>"en-CA"}
free_consultation_en_us GET /en-US/free-consultation(.:format) www/leads#consultation {:locale=>"en-US"}

However what it misses is the default route name

free_consultation_path() which could take a locale as a parameter

we're trying to migrate from routing-filter to this gem so we can have route translations as well

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