(虽然这里讨论的是 Blacklight 引擎,但我相信这个问题实际上纯粹是关于 Rails。)
我正在尝试将国际化添加到我的 Blacklight 应用程序中。为此,我
将所有内容都包裹在
config/routes.rb,scope "(:locale)", locale: /en|ja/和在
app/controllers/application_controller.rb我添加before_action :set_locale并覆盖default_url_options
正如Rails i18n 指南所建议的那样。大多数事情都有效,但有一件事我无法弄清楚。
我的所有应用程序路由都已正确映射,例如http://www.example.com/en/catalog/12345与 正确匹配(/:locale)/catalog/:id(.:format),并被路由到catalog#show) {:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/}。Devise 的所有 URL 都很好。一切正常……除了mount-ed Blacklight 引擎。
显然,Blacklight 引擎不听scope. rake routes显示:
Routes for Blacklight::Engine:
search_history GET /search_history(.:format) search_history#index
....
而不是(:locale)/search_history(.:format)我希望的那样。
我已经修改了 Blacklight 模板,以便获得一个指向当前页面的日文和英文语言选择器,但是当我导航到时search_history,url_for遇到:locale参数时突然抛出。
为什么mount忽略scope?如何解决我的问题(引擎路线也响应:locale)?
这是我默认的 Blacklight-generated config/routes.rb,修改为scope:
Rails.application.routes.draw do
scope "(:locale)", locale: /en|ja/ do
mount Blacklight::Engine => '/'
root to: "catalog#index"
concern :searchable, Blacklight::Routes::Searchable.new
resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog', id: /[^\/]+(?=\.json|\.html|$|\/)/ do
concerns :searchable
end
devise_for :users
concern :exportable, Blacklight::Routes::Exportable.new
resources :solr_documents, only: [:show], path: '/catalog', controller: 'catalog', id: /[^\/]+(?=\.json|\.html|$|\/)/ do
concerns :exportable
end
resources :bookmarks, id: /[^\/]+(?=\.json|\.html|$|\/)/ do
concerns :exportable
collection do
delete 'clear'
end
end
end
end
tl; dr: scope为我的所有路由添加前缀,除了mount. 为什么,以及如何解决?