From 1d88ca44f00dd661801150522caf6f9e60f596f7 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Tue, 22 Jul 2025 10:09:03 +0800 Subject: [PATCH] DEV: Use core's locale normalizer (#1509) We've moved the class to core here discourse/discourse#33702 so following up a deletion in this repo. --- .discourse-compatibility | 1 + app/jobs/regular/detect_translate_post.rb | 2 +- app/jobs/regular/detect_translate_topic.rb | 2 +- app/jobs/regular/localize_categories.rb | 2 +- lib/translation/locale_normalizer.rb | 50 ------------------- .../lib/translation/locale_normalizer_spec.rb | 42 ---------------- 6 files changed, 4 insertions(+), 95 deletions(-) delete mode 100644 lib/translation/locale_normalizer.rb delete mode 100644 spec/lib/translation/locale_normalizer_spec.rb diff --git a/.discourse-compatibility b/.discourse-compatibility index 6bde56a3..95b78d58 100644 --- a/.discourse-compatibility +++ b/.discourse-compatibility @@ -1,3 +1,4 @@ +< 3.5.0.beta8-dev: 5d80a34589d63e381d80273828072badc18955b1 < 3.5.0.beta7-dev: cd14b0c0bee0cf63c59b64b6f7213e31a37f11a7 < 3.5.0.beta6-dev: 3e74eea1e5e3143888d67a8d8a11206df214dc24 < 3.5.0.beta3-dev: 09a68414804a1447f52e5d60691ba59742cda9ec diff --git a/app/jobs/regular/detect_translate_post.rb b/app/jobs/regular/detect_translate_post.rb index b739989b..84b93a0a 100644 --- a/app/jobs/regular/detect_translate_post.rb +++ b/app/jobs/regular/detect_translate_post.rb @@ -40,7 +40,7 @@ module Jobs return if locales.blank? locales.each do |locale| - next if DiscourseAi::Translation::LocaleNormalizer.is_same?(locale, detected_locale) + next if LocaleNormalizer.is_same?(locale, detected_locale) regionless_locale = locale.split("_").first next if post.post_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists? diff --git a/app/jobs/regular/detect_translate_topic.rb b/app/jobs/regular/detect_translate_topic.rb index ae6972b8..e5cc1543 100644 --- a/app/jobs/regular/detect_translate_topic.rb +++ b/app/jobs/regular/detect_translate_topic.rb @@ -38,7 +38,7 @@ module Jobs return if locales.blank? locales.each do |locale| - next if DiscourseAi::Translation::LocaleNormalizer.is_same?(locale, detected_locale) + next if LocaleNormalizer.is_same?(locale, detected_locale) regionless_locale = locale.split("_").first next if topic.topic_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists? diff --git a/app/jobs/regular/localize_categories.rb b/app/jobs/regular/localize_categories.rb index 8c01c6bf..45476a58 100644 --- a/app/jobs/regular/localize_categories.rb +++ b/app/jobs/regular/localize_categories.rb @@ -29,7 +29,7 @@ module Jobs missing_locales = locales - existing_locales - [category.locale] missing_locales.each do |locale| break if remaining_limit <= 0 - next if DiscourseAi::Translation::LocaleNormalizer.is_same?(locale, category.locale) + next if LocaleNormalizer.is_same?(locale, category.locale) begin DiscourseAi::Translation::CategoryLocalizer.localize(category, locale) diff --git a/lib/translation/locale_normalizer.rb b/lib/translation/locale_normalizer.rb deleted file mode 100644 index d9540dc5..00000000 --- a/lib/translation/locale_normalizer.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -module DiscourseAi - module Translation - class LocaleNormalizer - # Normalizes locale string, matching the list of I18n.locales where possible - # @param locale [String,Symbol] the locale to normalize - # @return [String] the normalized locale - def self.normalize_to_i18n(locale) - return nil if locale.blank? - locale = locale.to_s.gsub("-", "_") - - i18n_pairs.each { |downcased, value| return value if locale.downcase == downcased } - - locale - end - - def self.is_same?(locale1, locale2) - return true if locale1 == locale2 - locale1 = locale1.gsub("-", "_").downcase - locale2 = locale2.gsub("-", "_").downcase - locale1.split("_").first == locale2.split("_").first - end - - private - - def self.i18n_pairs - # they should look like this for the input to match against: - # { - # "lowercased" => "actual", - # "en" => "en", - # "zh_cn" => "zh_CN", - # "zh" => "zh_CN", - # } - @locale_map ||= - I18n - .available_locales - .reduce({}) do |output, sym| - locale = sym.to_s - output[locale.downcase] = locale - if locale.include?("_") - short = locale.split("_").first - output[short] = locale if output[short].blank? - end - output - end - end - end - end -end diff --git a/spec/lib/translation/locale_normalizer_spec.rb b/spec/lib/translation/locale_normalizer_spec.rb deleted file mode 100644 index 772ec89f..00000000 --- a/spec/lib/translation/locale_normalizer_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -describe DiscourseAi::Translation::LocaleNormalizer do - describe ".normalize_to_i18n" do - it "matches input locales to i18n locales" do - expect(described_class.normalize_to_i18n("en-GB")).to eq("en_GB") - expect(described_class.normalize_to_i18n("en")).to eq("en") - expect(described_class.normalize_to_i18n("zh")).to eq("zh_CN") - expect(described_class.normalize_to_i18n("tr")).to eq("tr_TR") - end - - it "converts dashes to underscores" do - expect(described_class.normalize_to_i18n("a-b")).to eq("a_b") - end - end - - describe "#is_same?" do - it "returns true for the same locale" do - expect(described_class.is_same?("en", "en")).to be true - end - - it "returns true for locales with different cases" do - expect(described_class.is_same?("en", "EN")).to be true - end - - it "returns true for locales with different separators" do - expect(described_class.is_same?("en-US", "en_US")).to be true - end - - it "returns false for different locales" do - expect(described_class.is_same?("en", "ja")).to be false - end - - it "returns true for locales with the same base language" do - expect(described_class.is_same?("zh-CN", "zh_TW")).to be true - end - - it "returns false for completely different locales" do - expect(described_class.is_same?("en", "ja")).to be false - end - end -end