我是 rails 新手(使用 3.2.1),我正在关注rails guides上的 i18n-guide 。
我在这部分遇到问题:
您可能希望 URL 看起来像这样:www.example.com/en/books(加载英语语言环境)和 www.example.com/nl/books(加载荷兰语言环境)。这可以通过上面的“覆盖 default_url_options”策略来实现:您只需以这种方式使用 path_prefix 选项设置路由
但是当我在视图中使用 <%= products_path %> 时,它会返回:/products?locale=en 并且我希望它返回/nl/products
当我在浏览器中输入 url (fe localhost:3000/nl/products) 时,页面会显示正确的语言环境。
我错过了什么?
应用控制器:
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options = {})
{ :locale => I18n.locale }
end
end
路线:
scope "/:locale" do
resources :products
end
match '/:locale' => 'products#index'
耙路线:
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
GET /:locale/products(.:format) products#index
POST /:locale/products(.:format) products#create
GET /:locale/products/new(.:format) products#new
GET /:locale/products/:id/edit(.:format) products#edit
GET /:locale/products/:id(.:format) products#show
PUT /:locale/products/:id(.:format) products#update
DELETE /:locale/products/:id(.:format) products#destroy
/:locale(.:format) products#index
root / products#index