Reputation: 7
I'm new to rails, and I already learned how to use locales to change my website language, but I'd like to put a selectbox on the rightside of my navbar that could pass a variable/session to change the whole website language. Would that be possible ? I'm using windows with Ruby: 1.9.3, Rails:3.2.2
This is my navbar on "layouts/application.html.erb" so far:
<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>
Remembering I used the following scope for my url, so it is something like >"Localhost:3000/en/issues/"
scope "/:locale" do
get "/timeline/index", to: "timeline#index"
resources :projects
resources :issues
end
And this are my routes:
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
Upvotes: 0
Views: 1025
Reputation: 10207
You do not necessarily have to use a select box for this.
This is the solution that I came up with:
config.i18n.available_locales = [:en, :de, :es]
config.i18n.default_locale = :en
class ApplicationController
around_action :set_locale
private
def set_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
end
module LanguageHelper
LANGUAGES = {
:en => "English",
:de => "Deutsch",
:es => "Español"
}
def language_selector
active_locale = I18n.locale
inactive_locales = I18n.available_locales - [active_locale]
content_tag :li do
elements = [
link_to(LANGUAGES[active_locale], nil, :class => "dropdown")
]
elements << content_tag(:ul) do
links = []
inactive_locales.each do |locale|
link = link_to LANGUAGES[locale], params.permit(:locale).merge(:locale => locale)
links << content_tag(:li, link)
end
links.join.html_safe
end
elements.join.html_safe
end
end
end
Now simply put language_selector
where you need it, for example in the upper right corner of your page. The markup it generates is this (it might need some CSS styling and / or Javascript functionality):
<li>
<a class="dropdown" href="/de">Deutsch</a>
<ul>
<li><a href="/en">English</a></li>
<li><a href="/es">Español</a></li>
</ul>
</li>
Enjoy!
Upvotes: 0
Reputation: 2328
yes this is possible
In your view
<li><%= select_tag(:locale, options_for_select([['Portuguese', 'pt'], ['English', 'en']])) %></li>
In your ApplicationController
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
Upvotes: 1