route_translator icon indicating copy to clipboard operation
route_translator copied to clipboard

where is the generate_unnamed_unlocalized_routes redirect?

Open AlexEscalante opened this issue 9 years ago • 3 comments

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?

AlexEscalante avatar Aug 13 '15 20:08 AlexEscalante

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!

AlexEscalante avatar Aug 13 '15 22:08 AlexEscalante

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 ?

tagliala avatar Aug 18 '15 20:08 tagliala

@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

patricklindsay avatar Feb 06 '16 08:02 patricklindsay