diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1835bc5..a342123 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -38,7 +38,11 @@ # Sets FastGettext locale for every request made def set_gettext_locale - FastGettext.locale = session[:locale] || FastGettext.default_locale + FastGettext.locale = LocaleFormatter.new(current_locale, format: :fast_gettext).to_s + end + + def current_locale + session[:locale] || FastGettext.default_locale end def store_location diff --git a/app/controllers/session_locales_controller.rb b/app/controllers/session_locales_controller.rb index 9004d6c..d748b1e 100644 --- a/app/controllers/session_locales_controller.rb +++ b/app/controllers/session_locales_controller.rb @@ -3,10 +3,18 @@ class SessionLocalesController < ApplicationController def update - if FastGettext.default_available_locales.include?(params[:locale]) - session[:locale] = params[:locale] - end + session[:locale] = params[:locale] if available_locales.include?(param_locale) redirect_to(:back) end + private + + def available_locales + LocaleSet.new(FastGettext.default_available_locales).for(:fast_gettext) + end + + def param_locale + LocaleFormatter.new(params[:locale], format: :fast_gettext).to_s + end + end diff --git a/config/initializers/fast_gettext.rb b/config/initializers/fast_gettext.rb index 3e28740..5f6ba3d 100644 --- a/config/initializers/fast_gettext.rb +++ b/config/initializers/fast_gettext.rb @@ -17,7 +17,8 @@ end def available_locales - Rails.application.config.i18n.available_locales = LocaleSet.new(["en-GB", "en"]) + Rails.application.config.i18n.available_locales = + LocaleSet.new(["en-GB", "en"]).for(:i18n).to_a end end @@ -28,8 +29,8 @@ report_warning: false, ) -I18n.available_locales += available_locales.for(:i18n) -FastGettext.default_available_locales = available_locales.for(:fast_gettext) +I18n.available_locales += available_locales.for(:i18n).to_a +FastGettext.default_available_locales = available_locales.for(:fast_gettext).to_a FastGettext.default_text_domain = "app" diff --git a/lib/locale_set.rb b/lib/locale_set.rb index 3c14d8f..4ff5cf5 100644 --- a/lib/locale_set.rb +++ b/lib/locale_set.rb @@ -6,8 +6,8 @@ # Examples: # # @locale_set = LocaleSet.new(["en_GB", "en", "fr", "de", :ch_TW]) -# @locale_set.for(:i18n) # => ['en-GB', 'en', 'fr', 'de', 'ch-TW'] -# @locale_set.for(:fast_gettext) # => ['en_GB', 'en', 'fr', 'de', 'ch_TW'] +# @locale_set.for(:i18n) # => +# @locale_set.for(:fast_gettext) # => # class LocaleSet < Set @@ -18,9 +18,9 @@ # Returns Array def for(framework) if framework.to_sym == :i18n - map { |l| LocaleFormatter.new(l, format: :i18n).to_s } + self.class.new(map { |l| LocaleFormatter.new(l, format: :i18n).to_s }) else - map { |l| LocaleFormatter.new(l, format: :fast_gettext).to_s } + self.class.new(map { |l| LocaleFormatter.new(l, format: :fast_gettext).to_s }) end end diff --git a/spec/features/locales_spec.rb b/spec/features/locales_spec.rb index 2ef4c5b..6beb506 100644 --- a/spec/features/locales_spec.rb +++ b/spec/features/locales_spec.rb @@ -14,14 +14,21 @@ default_language: false, name: "German", abbreviation: "de" + ).first_or_create, + + Language.where( + default_language: false, + name: "Portugese", + abbreviation: "pt-BR" ).first_or_create + ] } let!(:user) { create(:user, language: languages.first) } before do - locale_set = LocaleSet.new(languages.map(&:abbreviation)) + locale_set = LocaleSet.new(%w[en-GB de pt-BR]) I18n.available_locales = locale_set.for(:i18n) FastGettext.default_available_locales = locale_set.for(:fast_gettext) I18n.locale = locale_set.for(:i18n).first @@ -36,14 +43,33 @@ FastGettext.default_locale = AVAILABLE_TEST_LOCALES.for(:fast_gettext).first end - scenario "user changes their locale" do - click_link "Language" - expect(current_path).to eql(plans_path) - expect(page).not_to have_text("Erstelle Plan") + context "when new locale has no region" do - click_link languages.last.name - expect(current_path).to eql(plans_path) - expect(page).to have_text("Erstelle Plan") + scenario "user changes their locale" do + create_plan_text = "Erstelle Plan" + click_link "Language" + expect(current_path).to eql(plans_path) + expect(page).not_to have_text(create_plan_text) + + click_link "German" + expect(current_path).to eql(plans_path) + expect(page).to have_text(create_plan_text) + end + end + context "when new locale has region" do + + scenario "user changes their locale" do + create_plan_text = "Criar plano" + click_link "Language" + expect(current_path).to eql(plans_path) + expect(page).not_to have_text(create_plan_text) + + click_link "Portugese" + expect(current_path).to eql(plans_path) + expect(page).to have_text(create_plan_text) + end + + end end diff --git a/spec/lib/locale_set_spec.rb b/spec/lib/locale_set_spec.rb index 946c936..28a2dd1 100644 --- a/spec/lib/locale_set_spec.rb +++ b/spec/lib/locale_set_spec.rb @@ -24,7 +24,7 @@ let!(:format) { :i18n } it "returns each item in i18n format" do - expect(subject).to eql(['en-GB', 'en-US', 'es', 'fr']) + expect(subject).to eql(['en-GB', 'en-US', 'es', 'fr'].to_set) end end @@ -34,7 +34,7 @@ let!(:format) { :fast_gettext } it "returns each item in fast_gettext format" do - expect(subject).to eql(['en_GB', 'en_US', 'es', 'fr']) + expect(subject).to eql(['en_GB', 'en_US', 'es', 'fr'].to_set) end end