gpt4 book ai didi

ruby-on-rails-3 - Rails 国际化 (I18n) : locale in the url

转载 作者:行者123 更新时间:2023-12-04 07:09:11 25 4
gpt4 key购买 nike

我是 Rails 新手(使用 3.2.1),我正在关注 rails guides 上的 i18n-guide .

我在这部分遇到问题:

You probably want URLs to look like this: www.example.com/en/books (which loads the English locale) and www.example.com/nl/books (which loads the Netherlands locale). This is achievable with the “over-riding default_url_options” strategy from above: you just have to set up your routes with path_prefix option in this way



但是当我在 View 中使用 <%= products_path %> 时,它会返回: /products?locale=en
我希望它返回 /nl/产品

当我在浏览器中输入 url(例如 localhost:3000/nl/products)时,页面会显示正确的语言环境。

我错过了什么?

应用 Controller :
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

最佳答案

使用 path_prefix 怎么样?选项改为

def default_url_options(options = {})
{ :path_prefix => I18n.locale }
end

关于ruby-on-rails-3 - Rails 国际化 (I18n) : locale in the url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com