FIX: Ensure JsLocaleHelper to not output deprecated translations (#28037)

* FIX: Ensure JsLocaleHelper to obly outputs up-to-date translations

The old implementation forgot to filter out deprecated
translations, causing these translations to incorrectly override the new
locale in the frontend.

This commit fills in the forgotten where clause, filtering only the
up-to-date part.

Related meta topic: https://meta.discourse.org/t/outdated-translation-replacement-causing-missing-translation/314352
This commit is contained in:
锦心 2024-07-29 15:21:25 +08:00 committed by GitHub
parent 3126c50baa
commit 319075e4dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 2 deletions

View File

@ -19,9 +19,9 @@ module Jobs
end
end
TranslationOverride.where(id: deprecated_ids).update_all(status: "deprecated")
TranslationOverride.where(id: outdated_ids).update_all(status: "outdated")
TranslationOverride.where(id: invalid_ids).update_all(status: "invalid_interpolation_keys")
TranslationOverride.where(id: deprecated_ids).update_all(status: "deprecated")
end
end
end

View File

@ -49,10 +49,14 @@ class TranslationOverride < ActiveRecord::Base
attribute :status, :integer
enum status: { up_to_date: 0, outdated: 1, invalid_interpolation_keys: 2, deprecated: 3 }
scope :mf_locales, ->(locale) { where(locale: locale).where("translation_key LIKE '%_MF'") }
scope :mf_locales,
->(locale) do
where(locale: locale).where.not(status: "deprecated").where("translation_key LIKE '%_MF'")
end
scope :client_locales,
->(locale) do
where(locale: locale)
.where.not(status: "deprecated")
.where("translation_key LIKE 'js.%' OR translation_key LIKE 'admin_js.%'")
.where.not("translation_key LIKE '%_MF'")
end

View File

@ -2,6 +2,7 @@
class I18nInterpolationKeysFinder
def self.find(text)
return [] unless text.is_a? String
pattern = Regexp.union([*I18n.config.interpolation_patterns, /\{\{(\w+)\}\}/])
keys = text.scan(pattern)
keys.flatten!

View File

@ -260,4 +260,25 @@ RSpec.describe JsLocaleHelper do
end
end
end
describe ".output_client_overrides" do
it "should not output deprecated translation overrides" do
Fabricate(
:translation_override,
locale: "en",
translation_key: "js.user.preferences.title",
value: "SHOULD_SHOW",
)
Fabricate(
:translation_override,
locale: "en",
translation_key: "js.user.preferences",
value: "SHOULD_NOT_SHOW",
status: "deprecated",
)
expect(described_class.output_client_overrides("en").include? "SHOULD_SHOW").to eq(true)
expect(described_class.output_client_overrides("en").include? "SHOULD_NOT_SHOW").to eq(false)
end
end
end