-
Notifications
You must be signed in to change notification settings - Fork 143
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
url_for not working when passing locale symbols #192
Comments
Hi,
|
Not a bug, it has to be a string 😅. PR to support both are welcomed Is there any place of the documentation of Route Translator which states that it should be a symbol? |
Hi,
As you can see in 007 it works with locale as a symbol for a route outside the As a workaround you can specify locale as a string, in which case url_for works also for a localized route, see 009.
I tested {model}_path and it works with locale as a symbol, so it's an issue that is triggered by url_for and locale as a symbol in translated routes.
Please note that I18n.locale are supposed to be symbols, not strings, as reported in Rails literature and common practice, see for example https://guides.rubyonrails.org/i18n.html Can you please help me track the bug down? Thank you. |
This line: of course, adding Also, I'm not the original author of this gem so I don't know if there is a deeper reason for using strings instead of symbols. The first appearance of |
@enriclluelles do you recall if there is any reason for the conversion of the locale from sym to string? |
It's not necessary to change the sanitizer. So maybe there are not so many breaking tests if you just change def translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
scope_params = {
blocks: (scope[:blocks] || []).dup,
constraints: scope[:constraints] || {},
defaults: scope[:defaults] || {},
module: scope[:module],
options: scope[:options] ? scope[:options].merge(translated_options) : translated_options
}
if RouteTranslator.config.verify_host_path_consistency
scope_params[:blocks].push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale)
end
translated_options[:locale] = translated_options[:locale].to_sym if translated_options[:locale].is_a?(String)
::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options
end The important bit is But I had to set So this could fix the issue if you can solve the params thingy without a before filter. 😉 |
@alexanderadam thanks for the detailed feedback
Do you know any other gem that requires locale parameter as a string? |
Yes, indeed 🙈 I just ran into this when investigated into this issue and made this fix mentioned before. I had this issue for example in Alchemy, a CMS I use (I can really recommend it btw.): If But it is no bug in Alchemy CMS and Alchemy also is definitely no exception in this regard. In general it totally make sense: given HTTP parameters can't be typed if there's no additional format on top. So instead of adding |
@alexanderadam thanks again Premise: at the moment I'm quite busy and I cannot allocate too much time on An example of gem using symbols instead? I've tried with your suggested fix but I'm now having 39 failures
And that's expected, now we should use symbols instead of strings to match the route. But, even if I do so, I have 2 other failures. This one about side effects makes me think
# See https://github.com/enriclluelles/route_translator/issues/69
def test_no_side_effects
draw_routes do
localized do
resources :people
end
scope '(:locale)', locale: /(en|es)/ do
get '*id' => 'products#show', as: 'product'
end
end
assert_routing '/es/gente', controller: 'people', action: 'index', locale: :es
assert_routing '/people', controller: 'people', action: 'index', locale: :en
assert_routing '/es/path/to/a/product', controller: 'products', action: 'show', locale: :es, id: 'path/to/a/product'
assert_routing '/path/to/another/product', controller: 'products', action: 'show', id: 'path/to/another/product'
end I think this works because of the regexp and the locale is going to be checked after, but I cannot invest more time, sorry. Anyway... A PR is very welcomed, but I will be pedantic on this change. edit: I guess that the only problem is that |
The failure in |
Ran into this this morning. You can just monkey patch generate. Kinda don't like doing that but it's a decent quick and dirty work around. Patch makes symbols work and non of the existing tests fail. Also pretty sure you want to test with
diff --git a/lib/route_translator/extensions/route_set.rb b/lib/route_translator/extensions/route_set.rb
index 2e863d6..84e84ad 100644
--- a/lib/route_translator/extensions/route_set.rb
+++ b/lib/route_translator/extensions/route_set.rb
@@ -24,6 +24,13 @@ module ActionDispatch
private
+ def generate(route_key, options, recall = {})
+ if options.key?(:locale)
+ options[:locale] = options[:locale].to_s
+ end
+ Generator.new(route_key, options, recall, self).generate
+ end
+
def translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
scope_params = {
blocks: (scope[:blocks] || []).dup, |
@flarecriteria thanks! PR with suggested test is very welcomed |
Hi,
I see
url_for
not working when routes are localized:Routes are OK:
But when I call url_for:
The text was updated successfully, but these errors were encountered: