Question

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
Was it helpful?

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

OTHER TIPS

If you have this pattern

http://localhost:3000/X/blah/blubb

where X can be a locale but can also sometimes be something else, then you will need to specify the list of locales and gsub them out:

"#{request.protocol}#{request.host_with_port}#{request.fullpath.gsub(/^\/#{array_of_locale_strings.map{|s| "(#{s})"}.join("|")}\//,"/")}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top