繁体   English   中英

Rails国际化和漂亮的URL

[英]Rails Internationalization and pretty URLs

我只想将我的Rails应用程序国际化,因此URL看起来像: site.com/en/products

我做的是:

# routes.rb
scope "(:locale)", :locale => /en|es/ do
  resources :pages
end

# application_controller.rb
def default_url_options(options={})
  logger.debug "default_url_options is passed options: #{options.inspect}\n"
  { :locale => I18n.locale }
end

但是,有了这个,我得到了像site.com/products/?locale=en这样的网址。 我怎样才能获得那些漂亮的URL?

谢谢。

我们使用i18n_routing gem来做到这一点。

如果将它添加到项目中,可以在config / routes.rb中执行类似的操作

localized do
  resources :articles
end

在config / locales / nl.yml文件中:(对于荷兰路由)

nl:
  resources:
    articles: 'nieuws'

后备将是/ articles /:id。 荷兰游客获得/ nieuws /:id url

尝试

url_for :controller => 'pages', :id => 'thepageiwant', :locale => locale 

暂无
暂无

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

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