简体   繁体   English

带有语言环境的Ruby on Rails路由

[英]Ruby on Rails Routes with Locales

I am using this guide: http://edgeguides.rubyonrails.org/i18n.html 我正在使用本指南: http//edgeguides.rubyonrails.org/i18n.html

What I would like: 我想要的是什么:

/about goes to pages#about with the defaulted locale of en . /about使用en的默认语言环境转到pages#about #out。

/en/about goes to pages#about with the locale of en . /en/about使用en的语言环境转到pages#about

/es/about goes to pages#about with the locale of es . /es/about转到pages#about用的语言环境es

What I get: 我得到了什么:

/about goes to root_path with the locale of about . /about转到root_path ,其语言环境为about

/en/about goes to pages#about with the locale of en . /en/about使用en的语言环境转到pages#about

/es/about goes to pages#about with the locale of es . /es/about转到pages#about用的语言环境es

Here is some code: 这是一些代码:

# config/routes.rb
match '/:locale' => 'pages#news'

scope "(:locale)", :locale => /en|es/ do
  match '/abcd' => 'pages#abcd'
  match '/plan' => 'pages#plan'
  match '/about' => 'pages#about'
  match '/history' => 'pages#history'
  match '/projects' => 'pages#projects'
  match '/donate' => 'pages#donate'
  match '/opportunities' => 'pages#opportunities'
  match '/board' => 'pages#board'
end

root :to => "pages#news"

# app/controller/application_controller.rb
before_filter :set_locale

def set_locale
  # if params[:locale] is nil then I18n.default_locale will be used
  I18n.locale = params[:locale]
end

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

If I am reading the guide correctly, section 2.5 says that I should be able to access /about and have it load the default locale. 如果我正确阅读指南,第2.5节说我应该能够访问/about并加载默认语言环境。

From 2.5: 从2.5开始:

# config/routes.rb
scope "(:locale)", :locale => /en|nl/ do
  resources :books
end

With this approach you will not get a Routing Error when accessing your resources such as http://localhost:3001/books without a locale. 使用此方法,在访问资源(例如http:// localhost:3001 / books而不使用区域设置)时,您将不会收到路由错误。 This is useful for when you want to use the default locale when one is not specified. 当您想要在未指定默认语言环境时使用默认语言环境时,这非常有用。

The first line in your routes.rb is a catch-all route routes.rb中的第一行是一个包罗万象的路线

match '/:locale' => 'pages#news'

It should be the last line in the file, right after the root route. 它应该是文件中的最后一行,就在根路径之后。

The other way is: 另一种方式是:

Possible_locales = /en|es/

match '/:locale' => 'pages#news', :locale => Possible_locales

scope "(:locale)", :locale => Possible_locales do
   ...
end

No need to worry about routes order. 无需担心路线顺序。

This blogpost explains it actually in great detail (Rails 4): 这篇博文实际上非常详细地解释了它(Rails 4):

Just what I was looking for when nothing seems to work 正是我在寻找什么似乎没有任何作用

http://dhampik.com/blog/rails-routes-tricks-with-locales http://dhampik.com/blog/rails-routes-tricks-with-locales

  scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do
    resources :posts    
    root to: "main#index"
  end

  root to: redirect("/#{I18n.default_locale}", status: 302), as: :redirected_root

  get "/*path", to: redirect("/#{I18n.default_locale}/%{path}", status: 302), constraints: {path: /(?!(#{I18n.available_locales.join("|")})\/).*/}, format: false

Redirects to default lang from root and does a lot of other things as well. 从root重定向到默认lang并执行许多其他操作。

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

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