route_translator
route_translator copied to clipboard
where is the generate_unnamed_unlocalized_routes redirect?
The documentation says you get a redirect when you set this config to true:
generate_unnamed_unlocalized_routes Set this option to true to add the behavior of force_locale, but with a named default route which behaves as if generate_unlocalized_routes was true. root_path will redirect to /en or /es depending on the value of I18n.locale. Defaults to false.
… but I am not getting any redirect?
Am I missing something here?
Nevermind. I think I understood what I needed to do. I am now using:
generate_unnamed_unlocalized_routes = true
and added this to my application_controller.rb:
before_action :locale_redirect
def locale_redirect
unless params[:locale]
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
redirect_to root_path
end
end
This seems to get the trick of detecting users preferred language and then redirect to appropriate URL. Every URL is locale prefixed so which is good for SEO.
Please let me know if you think this is OK. Thanks for the good job!
I think your request is legit.
I have no idea of that feature, but I understand the Readme as you do.
I'm going to reopen this one
Tests don't help: https://github.com/enriclluelles/route_translator/blob/e62be7f48c77760b2141d797e0b47fe2a97fbecf/test/routing_test.rb#L403
@enriclluelles ?
@AlexEscalante Your snippet doesn't seem to work I get:
undefined local variable or method `http_accept_language'
The gem is great but I haven't been able to work out how to enforce always having /locale/ prepend all my paths. I want the app to 404 (or at least redirect to default locale) if paths are entered without an appropriate locale.
Any ideas?
Edit: Came up with a solution which carries out a permanent redirect to the default locale localised path if a locale isn't present.
before_action :locale_redirect
def locale_redirect
unless params[:locale]
redirect_to RouteTranslator::Translator.translate_path(request.path, I18n.locale), status: :moved_permanently
end
end