Allow locale to be set through the front-end
* Store the last used locale in the session * Allow home page to be identified in helpers even when containing parameters * Create initializer to explicitly set the available locales, the default and the load_path
This commit is contained in:
@@ -4,7 +4,21 @@ class ApplicationController < ActionController::Base
|
|||||||
self.responder = ApplicationResponder
|
self.responder = ApplicationResponder
|
||||||
respond_to :html
|
respond_to :html
|
||||||
|
|
||||||
|
before_action :set_locale
|
||||||
|
|
||||||
# Prevent CSRF attacks by raising an exception.
|
# Prevent CSRF attacks by raising an exception.
|
||||||
# For APIs, you may want to use :null_session instead.
|
# For APIs, you may want to use :null_session instead.
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_locale
|
||||||
|
if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
|
||||||
|
session[:locale] = params[:locale]
|
||||||
|
end
|
||||||
|
|
||||||
|
session[:locale] ||= I18n.default_locale
|
||||||
|
|
||||||
|
I18n.locale = session[:locale]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -10,10 +10,16 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def home_page?
|
def home_page?
|
||||||
request.fullpath == '/'
|
# Using path because fullpath yields false negatives since it contains
|
||||||
|
# parameters too
|
||||||
|
request.path == '/'
|
||||||
end
|
end
|
||||||
|
|
||||||
def header_css
|
def header_css
|
||||||
home_page? ? '' : 'results'
|
home_page? ? '' : 'results'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def available_locales_to_switch
|
||||||
|
I18n.available_locales - [I18n.locale]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,6 +4,14 @@
|
|||||||
<div class="small-12 column">
|
<div class="small-12 column">
|
||||||
<div class="right">
|
<div class="right">
|
||||||
<a href="#"><%= t("layouts.header.external_link_transparency") %></a> | <a href="#"><%= t("layouts.header.external_link_opendata") %></a>
|
<a href="#"><%= t("layouts.header.external_link_transparency") %></a> | <a href="#"><%= t("layouts.header.external_link_opendata") %></a>
|
||||||
|
|
|
||||||
|
<span id="locale-switcher">
|
||||||
|
[
|
||||||
|
<% available_locales_to_switch.each do |locale| %>
|
||||||
|
<%= link_to(locale, params.merge(locale: locale), id: "locale-link-#{locale}") %>
|
||||||
|
<% end %>
|
||||||
|
]
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
6
config/initializers/i18n.rb
Normal file
6
config/initializers/i18n.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
I18n.available_locales = [:en, :es]
|
||||||
|
|
||||||
|
I18n.default_locale = :es
|
||||||
|
|
||||||
|
# Add the new directories to the locales load path
|
||||||
|
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
|
||||||
27
spec/features/localization_spec.rb
Normal file
27
spec/features/localization_spec.rb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
feature 'Localization' do
|
||||||
|
|
||||||
|
scenario 'Wrong locale' do
|
||||||
|
visit root_path(locale: :es)
|
||||||
|
visit root_path(locale: :klingon)
|
||||||
|
|
||||||
|
expect(page).to have_text('Estamos abriendo Madrid')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'Changing locale' do
|
||||||
|
visit root_path(locale: :es)
|
||||||
|
locale_switcher = find('#locale-switcher')
|
||||||
|
|
||||||
|
expect(page).to have_text('Estamos abriendo Madrid')
|
||||||
|
expect(locale_switcher).to have_text('en')
|
||||||
|
expect(locale_switcher).to_not have_text('es')
|
||||||
|
|
||||||
|
find('#locale-link-en').click
|
||||||
|
locale_switcher = find('#locale-switcher')
|
||||||
|
|
||||||
|
expect(page).to have_text('We are opening Madrid')
|
||||||
|
expect(locale_switcher).to have_text('es')
|
||||||
|
expect(locale_switcher).to_not have_text('en')
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user