在之前的Rails 2.3项目中,我使用translate_routes gem来执行路由的翻译。效果很好。在我的新Rails 3.1项目中,我再次需要路由转换。不幸的是,translate_routes不再工作了,它的开发者Raul宣布他将不再维护这个gem。我试着使用一个项目的分支,它应该可以在Rails 3.1上工作,但我不能做太多。
有没有一种不需要gem就能构建路由翻译的方法?
下面是一个没有翻译的工作路由的例子。
constraints(:subdomain => 'admin') do
scope "(:locale)", :locale => /fr|de/ do
resources :country, :languages
match '/' => 'home#admin', :as => :admin_home
end
end
正如您所看到的,我还希望有一个不带locale的默认路由,用于我的默认locale: en。
以前有人这样做过吗?由于
对您来说可能有点晚了,但对其他人可能有帮助,请尝试translate_routes:
https://github.com/francesc/rails-translate-routes看到你之前的帖子,但后来发现了另一个解决方案。我想翻译Rails路由和它们的默认资源动作,但我不喜欢他们的方式rails-translate-routes
添加_nl
到我的默认路径名。
我最终这样做了(也适用于rails 4.0),这应该是一个很好的解决方案,当你只用1或2种语言呈现你的应用程序。
# config/routes.rb
Testapp::Application.routes.draw do
# This scope changes resources methods names
scope(path_names: { new: I18n.t('routename.new'), edit: I18n.t('routename.edit') }) do
# devise works fine with this technique
devise_for :users, path: I18n.t('routename.userspath')
# resource path names can be translated like this
resources :cars, path: I18n.t('routename.carspath')
# url prefixes can be translated to
get "#{I18n.t('routename.carspath')}/export", to: 'cars#export'
end
end
和
# config/locales/nl.yml
nl:
routename:
## methods
new: 'nieuw'
edit: 'aanpassen'
## resources, etc.
userpath: 'gebruikers'
carspath: 'voertuigen'
结果:- /voertuigen
- /voertuigen nieuw
- /voertuigen aanpassen
- /voertuigen/出口
update和destroy是不必要的,因为它们作为post操作链接到根目录。保存你的工作;)