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

where is the generate_unnamed_unlocalized_routes redirect? #110

Open
AlexEscalante opened this issue Aug 13, 2015 · 3 comments
Open

where is the generate_unnamed_unlocalized_routes redirect? #110

AlexEscalante opened this issue Aug 13, 2015 · 3 comments

Comments

@AlexEscalante
Copy link

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 AlexEscalante changed the title where is the generate_unnamed_unlocalized_routes redirect redirect? where is the generate_unnamed_unlocalized_routes redirect? Aug 13, 2015
@AlexEscalante
Copy link
Author

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!

@tagliala
Copy link
Collaborator

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:

def test_generate_unnamed_unlocalized_routes

@enriclluelles ?

@patricklindsay
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants