From 7f663b874fc7c7faa8a8476ba9397abfdb1e14c7 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 24 May 2023 20:42:27 +0200 Subject: [PATCH] Respect available locales set in application If the application has set Rails.application.config.i18n.available_locales we use them for Alchemy::I81n.available_locales and only scan the locale files if not. --- lib/alchemy/i18n.rb | 10 ++++++---- spec/libraries/i18n_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/alchemy/i18n.rb b/lib/alchemy/i18n.rb index 6b4aaa7945..5e97abf841 100644 --- a/lib/alchemy/i18n.rb +++ b/lib/alchemy/i18n.rb @@ -54,10 +54,12 @@ def translate(msg, **options) end def available_locales - @@available_locales ||= nil - @@available_locales || translation_files.collect { |f| - f.match(LOCALE_FILE_PATTERN)[1].to_sym - }.uniq.sort + Rails.application.config.i18n.available_locales || begin + @@available_locales ||= nil + @@available_locales || translation_files.collect { |f| + f.match(LOCALE_FILE_PATTERN)[1].to_sym + }.uniq.sort + end end def available_locales=(locales) diff --git a/spec/libraries/i18n_spec.rb b/spec/libraries/i18n_spec.rb index 44bde5af8f..fca2d1fde0 100644 --- a/spec/libraries/i18n_spec.rb +++ b/spec/libraries/i18n_spec.rb @@ -16,6 +16,13 @@ module Alchemy it { is_expected.to be_a Array } it { is_expected.to include(:en) } + context "when locales are already set in application available_locales" do + it "returns them" do + expect(Rails.application.config.i18n).to receive(:available_locales) { [:de, :it] } + is_expected.to match_array([:de, :it]) + end + end + context "when locales are already set in @@available_locales" do before { I18n.class_variable_set(:@@available_locales, [:kl, :jp]) } it { is_expected.to match_array([:kl, :jp]) }