diff --git a/app/helpers/i18n_helper.rb b/app/helpers/i18n_helper.rb index 527429b530..827260404e 100644 --- a/app/helpers/i18n_helper.rb +++ b/app/helpers/i18n_helper.rb @@ -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 diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 5007134bd6..d631387fa4 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -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 diff --git a/db/migrate/20170512115519_add_locale_to_spree_users.rb b/db/migrate/20170512115519_add_locale_to_spree_users.rb new file mode 100644 index 0000000000..f694bff1f7 --- /dev/null +++ b/db/migrate/20170512115519_add_locale_to_spree_users.rb @@ -0,0 +1,5 @@ +class AddLocaleToSpreeUsers < ActiveRecord::Migration + def change + add_column :spree_users, :locale, :string, limit: 5 + end +end diff --git a/db/schema.rb b/db/schema.rb index b62bd45c04..5c70c8e6f9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/features/admin/multilingual_spec.rb b/spec/features/admin/multilingual_spec.rb index bfad7c39a6..0f98e8bbde 100644 --- a/spec/features/admin/multilingual_spec.rb +++ b/spec/features/admin/multilingual_spec.rb @@ -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 diff --git a/spec/features/consumer/multilingual_spec.rb b/spec/features/consumer/multilingual_spec.rb index 59858875e2..4f163dee01 100644 --- a/spec/features/consumer/multilingual_spec.rb +++ b/spec/features/consumer/multilingual_spec.rb @@ -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 diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 1bd22b010c..8d9c66c148 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -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