Search code examples
ruby-on-railsurllocalerails-i18n

Rails - Current URL without locale part passed


In my Rails app I scope locale into my urls:

http://localhost:3000/de/blah/blubb
http://localhost:3000/en/blah/blubb
http://localhost:3000/es/blah/blubb
                     ^^^

How can I "get" the current_url without the locale parameter?

http://localhost:3000/blah/blubb
                    ^^^

At the moment I have this:

<%=  "#{request.protocol}#{request.host_with_port}#{request.fullpath}" %>
# http://localhost:3000/de/blah/blubb

Solution

  • You can keep a list of available locales you want to exclude:

    LOCALES = %w(en de es).map { |l| l.prepend '/' }
    

    Then you can replace them like this:

    <%= "#{request.protocol}#{request.host_with_port}#{request.fullpath.sub(Regexp.union(LOCALES), '')}" %>
    # /blah/blubb