Rails - 传递没有语言环境部分的当前URL

时间:2014-02-28 12:30:29

标签: ruby-on-rails url locale rails-i18n

在我的Rails应用程序中,我将区域设置范围放入我的网址:

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

如何在没有locale参数的情况下“获取”current_url?

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

目前我有这个:

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

2 个答案:

答案 0 :(得分:1)

您可以保留要排除的可用区域设置列表:

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

然后你可以像这样替换它们:

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

答案 1 :(得分:0)

如果你有这种模式

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

其中X可以是区域设置,但有时也可能是其他东西,那么您需要指定区域设置列表并将其格式化:

"#{request.protocol}#{request.host_with_port}#{request.fullpath.gsub(/^\/#{array_of_locale_strings.map{|s| "(#{s})"}.join("|")}\//,"/")}"