Add backend to be able to switch language by param and persist in cookies and user db

This commit is contained in:
Julius Pabrinkis
2017-05-12 16:42:47 +01:00
committed by Maikel Linke
parent 72d264fab9
commit 1fe20eba45
7 changed files with 76 additions and 9 deletions

View File

@@ -2,6 +2,16 @@ module I18nHelper
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
# update spree_current_user locale if logged in and set cookie locale if params
if params[:locale] && Rails.application.config.i18n.available_locales.include?(params[:locale])
spree_current_user.update_attributes!(locale: params[:locale]) if spree_current_user
cookies[:locale] = params[:locale]
end
if spree_current_user && spree_current_user.locale.nil? && cookies[:locale]
spree_current_user.update_attributes!(locale: params[:locale])
end
I18n.locale = spree_current_user.andand.locale || cookies[:locale] || I18n.default_locale
end
end

View File

@@ -17,7 +17,7 @@ Spree.user_class.class_eval do
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit, :bill_address_attributes, :ship_address_attributes
attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit, :locale, :bill_address_attributes, :ship_address_attributes
after_create :send_signup_confirmation
validate :limit_owned_enterprises

View File

@@ -0,0 +1,5 @@
class AddLocaleToSpreeUsers < ActiveRecord::Migration
def change
add_column :spree_users, :locale, :string, limit: 5
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20170413083148) do
ActiveRecord::Schema.define(:version => 20170512115519) do
create_table "account_invoices", :force => true do |t|
t.integer "user_id", :null => false
@@ -998,6 +998,7 @@ ActiveRecord::Schema.define(:version => 20170413083148) do
t.datetime "reset_password_sent_at"
t.string "api_key", :limit => 40
t.integer "enterprise_limit", :default => 1, :null => false
t.string "locale", :limit => 5
end
add_index "spree_users", ["email"], :name => "email_idx_unique", :unique => true

View File

@@ -3,9 +3,13 @@ require 'spec_helper'
feature 'Multilingual', js: true do
include AuthenticationWorkflow
include WebHelper
let(:admin_role) { Spree::Role.find_or_create_by_name!('admin') }
let(:admin_user) { create(:user) }
background do
login_to_admin_section
admin_user.spree_roles << admin_role
quick_login_as admin_user
visit spree.admin_path
end
it 'has two locales available' do
@@ -18,11 +22,14 @@ feature 'Multilingual', js: true do
expect(get_i18n_locale).to eq 'en'
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises'
expect(page).to have_content 'My Enterprises'
expect(admin_user.locale).to be_nil
visit spree.admin_path(locale: 'es')
expect(get_i18n_locale).to eq 'es'
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'Mis Organizaciones'
expect(page).to have_content 'Mis Organizaciones'
admin_user.reload
expect(admin_user.locale).to eq 'es'
end
it 'fallbacks to default_locale' do
@@ -34,6 +41,7 @@ feature 'Multilingual', js: true do
visit spree.admin_path(locale: 'it')
expect(get_i18n_locale).to eq 'it'
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises'
expect(admin_user.locale).to be_nil
# This still is italian until we change enforce_available_locales to `true`
expect(page).to have_content 'Le Mie Aziende'
end

View File

@@ -1,6 +1,7 @@
require 'spec_helper'
feature 'Multilingual', js: true do
include AuthenticationWorkflow
include WebHelper
it 'has two locales available' do
@@ -9,24 +10,62 @@ feature 'Multilingual', js: true do
expect(Rails.application.config.i18n[:available_locales]).to eq ['en', 'es']
end
it '18n-js fallsback to default language' do # in backend it doesn't until we change enforce_available_locales to `true`
visit root_path
set_i18n_locale('it')
expect(get_i18n_translation('label_shops')).to eq 'Shops'
end
it 'can switch language by params' do
visit root_path
expect(get_i18n_locale).to eq 'en'
expect(get_i18n_translation('label_shops')).to eq 'Shops'
expect(page.driver.browser.cookies['locale']).to be_nil
expect(page).to have_content 'Interested in getting on the Open Food Network?'
expect(page).to have_content 'SHOPS'
visit root_path(locale: 'es')
expect(get_i18n_locale).to eq 'es'
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
expect(page).to have_content 'TIENDAS'
# I18n-js fallsback to 'en'
# it is not in the list of available of available_locales
visit root_path(locale: 'it')
expect(get_i18n_locale).to eq 'it'
expect(get_i18n_translation('label_shops')).to eq 'Shops'
# This still is italian until we change enforce_available_locales to `true`
expect(page).to have_content 'NEGOZI'
expect(get_i18n_locale).to eq 'es'
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
expect(page).to have_content 'TIENDAS'
end
context 'with user' do
let(:user) { create(:user) }
it 'updates user locale from cookie if it is empty' do
visit root_path(locale: 'es')
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
expect(user.locale).to be_nil
quick_login_as user
visit root_path
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
end
it 'updates user locale and stays in cookie after logout' do
quick_login_as user
visit root_path(locale: 'es')
user.reload
expect(user.locale).to eq 'es'
logout
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
expect(page).to have_content 'TIENDAS'
end
end
end

View File

@@ -113,6 +113,10 @@ module WebHelper
DirtyFormDialog.new(page)
end
def set_i18n_locale(locale = 'en')
page.evaluate_script("I18n.locale = '#{locale}'")
end
def get_i18n_locale
page.evaluate_script("I18n.locale;")
end