我是Rails的新手,我已经学会了如何使用locale来改变我的网站语言,但是我想在导航栏的右侧放一个选择框,它可以传递一个变量/session来改变整个网站语言。有可能吗?我在Ruby中使用Windows:1.9.3,Rails:3.2.2
这是我到目前为止在“layouts/application.html.erb”上的导航栏:
<div class="upmenu">
<ul align="left">
<li><a href="/issues"><%=t "nav.issues.list" %></a></li>
<li><a href="/issues/new"><%=t "nav.issues.new" %></a></li>
<li><a href="/timeline/index"><%=t "nav.timeline" %></a></li>
<li><a href="/projects"><%=t "nav.projects" %></a></li>
<%= yield :nav %>
</ul>
<ul id="language">
<li><%= select_tag(:lang, options_for_select([['Portuguese', 'pt'], ['English', 'en']])) %></li>
</ul>
</div>
字符串
记得我使用了下面的作用域作为我的url,所以它类似于>“Localhost:3000/en/issues/”
scope "/:locale" do
get "/timeline/index", to: "timeline#index"
resources :projects
resources :issues
end
型
这是我的路线:
timeline_index GET /:locale/timeline/index(.:format) timeline#index
projects GET /:locale/projects(.:format) projects#index
POST /:locale/projects(.:format) projects#create
new_project GET /:locale/projects/new(.:format) projects#new
edit_project GET /:locale/projects/:id/edit(.:format) projects#edit
project GET /:locale/projects/:id(.:format) projects#show
PUT /:locale/projects/:id(.:format) projects#update
DELETE /:locale/project
DELETE /:locale/projects/:id(.:format) projects#destroy
.:format) issues#index
issues GET /:locale/issues(.:format) issues#index
POST /:locale/issues(.:format) issues#create
PUT /:locale/projects/:id(.:format) projects#update
DELETE /:locale/projects/:id(.:format) projects#destroy
issues GET /:locale/issues(.:format) issues#index
POST /:locale/issues(.:format) issues#create
new_issue GET /:locale/issues/new(.:format) issues#new
edit_issue GET /:locale/issues/:id/edit(.:format) issues#edit
issue GET /:locale/issues/:id(.:format) issues#show
PUT /:locale/issues/:id(.:format) issues#update
DELETE /:locale/issues/:id(.:format) issues#destroy
型
2条答案
按热度按时间pengsaosao1#
是的,这是可能的。
在你看来
字符串
在ApplicationController中
型
l2osamch2#
您不必为此使用选择框。
这是我想出的解决方案:
config/application.rb
字符串
app/controllers/application_controller.rb
型
app/helpers/language_helpers.rb
型
现在只需将
language_selector
放在你需要的地方,例如在页面的右上角。它生成的标记是这样的(它可能需要一些CSS样式和/或JavaScript功能):型
好好享受吧!