2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/services/locale_normalizer_spec.rb
Natalie Tay 83670fb01e
FIX: Show localization for regionless locale if they exist (#33702)
Currently if a post is written in Japanese (`ja`) and has been
translated to English (`en`), a British (`en_GB`) user will see the
Japanese post.

WIth this PR we normalize user locales so the best match would show
(`en` first, then `en_GB`, or `pt` and `pt_BR`).

Note: `LocaleNormalizer` is from discourse-ai and will be removed there.
2025-07-21 15:45:14 +08:00

42 lines
1.3 KiB
Ruby

# frozen_string_literal: true
describe 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