discourse-ai/spec/lib/translation/locale_normalizer_spec.rb
Natalie Tay d54cd1f602
DEV: Normalize locales that are similar (e.g. en and en_GB) so they do not get translated (#1495)
This commit
- normalizes locales like en_GB and variants to en. With this, the feature will not translate en_GB posts to en (or similarly pt_BR to pt_PT)
- consolidates whether the feature is enabled in `DiscourseAi::Translation.enabled?`
- similarly for backfill in  `DiscourseAi::Translation.backfill_enabled?`
  - turns off backfill if `ai_translation_backfill_max_age_days` is 0 to keep true to what it says. Set it to a high number to backfill everything
2025-07-09 22:21:51 +08:00

43 lines
1.4 KiB
Ruby

# 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