FIX: Pluralized translation overrides didn't work for en_US

FIX: Pluralized translation overrides didn’t work for en_US

“en_US” doesn’t contain most of the translations, so it falls back to “en”. But that behavior stopped translation overrides to work for pluralized strings in “en_US”, because it relies on existing translations. This fixes it by looking up the existing translation in all fallback locales.

diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb
index 27da5e1..96971d9 100644
--- a/lib/i18n/backend/discourse_i18n.rb
+++ b/lib/i18n/backend/discourse_i18n.rb
@@ -82,20 +82,29 @@ module I18n
         overrides = options.dig(:overrides, locale)
 
         if overrides
-          if existing_translations && options[:count]
-            remapped_translations =
-              if existing_translations.is_a?(Hash)
-                Hash[existing_translations.map { |k, v| ["#{key}.#{k}", v] }]
-              elsif existing_translations.is_a?(String)
-                Hash[[[key, existing_translations]]]
+          if options[:count]
+            if !existing_translations
+              I18n.fallbacks[locale].drop(1).each do |fallback|
+                existing_translations = super(fallback, key, scope, options)
+                break if existing_translations.present?
               end
+            end
+
+            if existing_translations
+              remapped_translations =
+                if existing_translations.is_a?(Hash)
+                  Hash[existing_translations.map { |k, v| ["#{key}.#{k}", v] }]
+                elsif existing_translations.is_a?(String)
+                  Hash[[[key, existing_translations]]]
+                end
 
-            result = {}
+              result = {}
 
-            remapped_translations.merge(overrides).each do |k, v|
-              result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s)
+              remapped_translations.merge(overrides).each do |k, v|
+                result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s)
+              end
+              return result if result.size > 0
             end
-            return result if result.size > 0
           end
 
           return overrides[key] if overrides[key]
@@ -103,7 +112,6 @@ module I18n
 
         existing_translations
       end
-
     end
   end
 end
diff --git a/spec/components/freedom_patches/translate_accelerator_spec.rb b/spec/components/freedom_patches/translate_accelerator_spec.rb
index 9387564..930e4b3 100644
--- a/spec/components/freedom_patches/translate_accelerator_spec.rb
+++ b/spec/components/freedom_patches/translate_accelerator_spec.rb
@@ -172,6 +172,16 @@ describe "translate accelerator" do
       expect(I18n.t('items', count: 1)).to eq('one fish')
     end
 
+    it "supports one and other with fallback locale" do
+      override_translation('en_US', 'items.one', 'one fish')
+      override_translation('en_US', 'items.other', '%{count} fishies')
+
+      I18n.with_locale(:en_US) do
+        expect(I18n.t('items', count: 13)).to eq('13 fishies')
+        expect(I18n.t('items', count: 1)).to eq('one fish')
+      end
+    end
+
     it "supports one and other when only a single pluralization key is overridden" do
       override_translation('en', 'keys.magic.other', 'no magic keys')
       expect(I18n.t('keys.magic', count: 1)).to eq('one magic key')
diff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rb
index f4df683..e941468 100644
--- a/spec/integration/email_style_spec.rb
+++ b/spec/integration/email_style_spec.rb
@@ -94,14 +94,14 @@ describe EmailStyle do
       context 'translation override' do
         before do
           TranslationOverride.upsert!(
-            'en',
+            SiteSetting.default_locale,
             'user_notifications.signup.text_body_template',
             "CLICK THAT LINK: %{base_url}/u/activate-account/%{email_token}"
           )
         end
 
         after do
-          TranslationOverride.revert!('en', ['user_notifications.signup.text_body_template'])
+          TranslationOverride.revert!(SiteSetting.default_locale, ['user_notifications.signup.text_body_template'])
         end
 
         it "applies customizations when translation override exists" do

GitHub sha: ce1620f2

1 Like