diff options
author | Akihiko Odaki (@fn_aki@pawoo.net) <akihiko.odaki.4i@stu.hosei.ac.jp> | 2017-06-10 16:44:02 +0900 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-06-10 09:44:02 +0200 |
commit | 4919b89ab83f8eee43b89ff4786eae46bf2fa30e (patch) | |
tree | f779dac8d3f736ecd6696dac249729aaef3918f7 | |
parent | 2925372ff44347fa7066c380a5d51dd35f80682f (diff) |
Improve default language decision and spec (#3351)
* Improve default language decision This change allows to takes account of accepted language determined by the user agent even if the custom default locale of the instance is configured. * Cover Localized more * Fix code style
-rw-r--r-- | app/controllers/concerns/localized.rb | 22 | ||||
-rw-r--r-- | spec/controllers/concerns/localized_spec.rb | 88 |
2 files changed, 105 insertions, 5 deletions
diff --git a/app/controllers/concerns/localized.rb b/app/controllers/concerns/localized.rb index 5501b6793..a9ea60f7d 100644 --- a/app/controllers/concerns/localized.rb +++ b/app/controllers/concerns/localized.rb @@ -17,12 +17,24 @@ module Localized end def default_locale - ENV.fetch('DEFAULT_LOCALE') do - user_supplied_locale || I18n.default_locale - end + request_locale || env_locale || I18n.default_locale end - def user_supplied_locale - http_accept_language.language_region_compatible_from(I18n.available_locales) + def env_locale + ENV['DEFAULT_LOCALE'] + end + + def request_locale + preferred_locale || compatible_locale + end + + def preferred_locale + http_accept_language.preferred_language_from([env_locale]) || + http_accept_language.preferred_language_from(I18n.available_locales) + end + + def compatible_locale + http_accept_language.compatible_language_from([env_locale]) || + http_accept_language.compatible_language_from(I18n.available_locales) end end diff --git a/spec/controllers/concerns/localized_spec.rb b/spec/controllers/concerns/localized_spec.rb new file mode 100644 index 000000000..18fe508e4 --- /dev/null +++ b/spec/controllers/concerns/localized_spec.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ApplicationController, type: :controller do + controller do + include Localized + + def success + head 200 + end + end + + before do + routes.draw { get 'success' => 'anonymous#success' } + end + + shared_examples 'default locale' do + context 'when DEFAULT_LOCALE environment variable is set' do + around do |example| + ClimateControl.modify 'DEFAULT_LOCALE' => 'ca', &example.method(:run) + end + + it 'sets language specified by ENV if preferred' do + request.headers['Accept-Language'] = 'ca, fa' + get 'success' + expect(I18n.locale).to eq :ca + end + + it 'sets available and preferred language if language specified by ENV is not preferred' do + request.headers['Accept-Language'] = 'ca-ES, fa' + get 'success' + expect(I18n.locale).to eq :fa + end + + it 'sets language specified by ENV if it is compatible and none of available languages are preferred' do + request.headers['Accept-Language'] = 'ca-ES, fa-IR' + get 'success' + expect(I18n.locale).to eq :ca + end + + it 'sets available and compatible langauge if language specified by ENV is not compatible none of available languages are preferred' do + request.headers['Accept-Language'] = 'fa-IR' + get 'success' + expect(I18n.locale).to eq :fa + end + + it 'sets language specified by ENV if none of available languages are compatible' do + request.headers['Accept-Language'] = '' + get 'success' + expect(I18n.locale).to eq :ca + end + end + + context 'when DEFAULT_LOCALE environment variable is not set' do + it 'sets default locale if none of available languages are compatible' do + request.headers['Accept-Language'] = '' + get 'success' + expect(I18n.locale).to eq :en + end + end + end + + context 'user with valid locale has signed in' do + it "sets user's locale" do + user = Fabricate(:user, locale: :ca) + + sign_in(user) + get 'success' + + expect(I18n.locale).to eq :ca + end + end + + context 'user with invalid locale has signed in' do + before do + user = Fabricate.build(:user, locale: :invalid) + user.save!(validate: false) + sign_in(user) + end + + include_examples 'default locale' + end + + context 'user has not signed in' do + include_examples 'default locale' + end +end |