diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b64f11bb4..6779d223a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,21 @@ class ApplicationController < ActionController::Base self.responder = ApplicationResponder respond_to :html + before_action :set_locale + # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. 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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 499265f5a..31314a357 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -10,10 +10,16 @@ module ApplicationHelper end def home_page? - request.fullpath == '/' + # Using path because fullpath yields false negatives since it contains + # parameters too + request.path == '/' end def header_css home_page? ? '' : 'results' end + + def available_locales_to_switch + I18n.available_locales - [I18n.locale] + end end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4353e164a..b76b2735e 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -4,6 +4,14 @@
<%= t("layouts.header.external_link_transparency") %> | <%= t("layouts.header.external_link_opendata") %> + | + + [ + <% available_locales_to_switch.each do |locale| %> + <%= link_to(locale, params.merge(locale: locale), id: "locale-link-#{locale}") %> + <% end %> + ] +
@@ -42,4 +50,4 @@ <% end %> - \ No newline at end of file + diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb new file mode 100644 index 000000000..9aa77cd8b --- /dev/null +++ b/config/initializers/i18n.rb @@ -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}')] diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb new file mode 100644 index 000000000..9e4a64681 --- /dev/null +++ b/spec/features/localization_spec.rb @@ -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