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.
This commit is contained in:
parent
6059b6e111
commit
1d88ca44f0
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
||||
|
|
|
@ -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?
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue