简体   繁体   English

Ruby on Rails重定向I18n

[英]Ruby on Rails redirect I18n

According to the Railscasts video Ruby on Rails - Railscasts PRO #138 I18n (revised) to redirect the user going to domain.com/products to the default locale domain.com/en/products I should add to my routes.rb: 根据Railscasts的视频Ruby on Rails-Railscasts PRO#138 I18n(修订版),将要转到domain.com/products的用户重定向到默认语言环境domain.com/en/products,我应该添加到我的route.rb中:

match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
match '', to: redirect("/#{I18n.default_locale}")

This doesn't work. 这行不通。 First I get an error saying I should replace "match" with "get". 首先,我收到一条错误消息,说我应该将“ match”替换为“ get”。 Then when I do that I get an error from my browser saying ERR_TOO_MANY_REDIRECTS. 然后,当我这样做时,我从浏览器中收到一条错误消息,说ERR_TOO_MANY_REDIRECTS。

Any ideas why this isn't working? 任何想法为什么这不起作用? Thanks alot in advance! 在此先多谢!

You can use: 您可以使用:

get '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
get '', to: redirect("/#{I18n.default_locale}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM