0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/spec/lib/localization_attributes_replacer_spec.rb
Kris ffcd045019
FIX: only show first paragraph of localized category descriptions (#41511)
Reported here:
https://meta.discourse.org/t/category-description-truncates-to-first-paragraph-for-primary-base-language-but-not-for-localizations-panel-languages/406801

In a number of places we only show the first paragraph of a category
description by default, but when someone manually adds a category
description translation we show the full content. This results in
translated content being much longer in some places where only a single
paragraph is expected (/categories pages, category headers, etc).

This change cooks the translated description and applies the same single
paragraph rule where relevant.

`Category.first_paragraph_description` has been extracted to be used in
both the default and translated descriptions so the behavior remains
consistent.
2026-07-10 16:59:40 -04:00

137 lines
5.3 KiB
Ruby
Vendored

# frozen_string_literal: true
describe LocalizationAttributesReplacer do
describe ".replace_category_attributes" do
fab!(:category) { Fabricate(:category, locale: "en") }
fab!(:subcategory) { Fabricate(:category, parent_category: category, locale: "en") }
fab!(:ja_subcategory) do
Fabricate(:category_localization, category: subcategory, locale: "ja", name: "猫犬")
end
fab!(:ja_category) { Fabricate(:category_localization, category:, locale: "ja", name: "猫犬") }
it "replaces category and subcategory attributes with localized value" do
LocalizationAttributesReplacer.replace_category_attributes(subcategory, "ja")
expect(subcategory.name).to eq(ja_subcategory.name)
expect(subcategory.description).to eq(ja_subcategory.description)
expect(subcategory.parent_category.name).to eq(ja_category.name)
expect(subcategory.parent_category.description).to eq(ja_category.description)
end
it "only uses the first paragraph of a localized description" do
ja_category.update!(description: "最初の段落\n\n二番目の段落")
LocalizationAttributesReplacer.replace_category_attributes(category, "ja")
expect(category.description).to eq("最初の段落")
end
it "keeps the untranslated description when the localized description has no paragraph" do
ja_category.update!(description: "- 一つ\n- 二つ")
original_description = category.description
LocalizationAttributesReplacer.replace_category_attributes(category, "ja")
expect(category.description).to eq(original_description)
end
it "does not change the name if the locale is the same" do
LocalizationAttributesReplacer.replace_category_attributes(category, "en")
expect(category.name).to eq(category.name)
end
it "does not change attributes if no localization exists for the given locale" do
LocalizationAttributesReplacer.replace_category_attributes(category, "fr")
expect(category.name).to eq(category.name)
end
end
describe ".replace_topic_attributes" do
fab!(:topic) { Fabricate(:topic, locale: "en") }
fab!(:ja_localization) do
Fabricate(:topic_localization, topic:, locale: "ja", title: "猫犬", excerpt: "柴犬は猫のような犬です。")
end
it "replaces the title, fancy_title, and excerpt with localized values" do
LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
expect(topic.title).to eq(ja_localization.title)
expect(topic.fancy_title).to eq(ja_localization.fancy_title)
expect(topic.excerpt).to eq(ja_localization.excerpt)
end
it "does not write localized fancy_title to the database when fancy_title is null" do
topic.update_column(:fancy_title, nil)
topic.reload
LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
expect(topic.title).to eq(ja_localization.title)
expect(topic.read_attribute(:fancy_title)).to eq(ja_localization.fancy_title)
localized_fancy_title = Topic.fancy_title(ja_localization.title)
expect(Topic.where(id: topic.id).pick(:fancy_title)).not_to eq(localized_fancy_title)
end
it "generates fancy_title from localized title when localization fancy_title is blank" do
ja_localization.update_column(:fancy_title, "")
topic.update_column(:fancy_title, nil)
topic.reload
LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
expect(topic.title).to eq(ja_localization.title)
expect(topic.read_attribute(:fancy_title)).to eq(Topic.fancy_title(ja_localization.title))
localized_fancy_title = Topic.fancy_title(ja_localization.title)
expect(Topic.where(id: topic.id).pick(:fancy_title)).not_to eq(localized_fancy_title)
end
it "does not change the title or excerpt if the locale is the same" do
LocalizationAttributesReplacer.replace_topic_attributes(topic, "en")
expect(topic.title).to eq(topic.title)
expect(topic.excerpt).to eq(topic.excerpt)
end
it "does not change attributes if no localization exists for the given locale" do
LocalizationAttributesReplacer.replace_topic_attributes(topic, "fr")
expect(topic.title).to eq(topic.title)
expect(topic.excerpt).to eq(topic.excerpt)
end
it "does not error out if topic does not exist" do
expect {
LocalizationAttributesReplacer.replace_topic_attributes(nil, "ja")
}.not_to raise_error
end
end
describe ".replace_post_attributes" do
fab!(:post) { Fabricate(:post, locale: "en") }
fab!(:ja_localization) do
Fabricate(:post_localization, post:, locale: "ja", cooked: "猫犬は柴犬のような猫です。")
end
it "replaces the cooked content with localized values" do
LocalizationAttributesReplacer.replace_post_attributes(post, "ja")
expect(post.cooked).to eq(ja_localization.cooked)
end
it "does not change the cooked content if the locale is the same" do
LocalizationAttributesReplacer.replace_post_attributes(post, "en")
expect(post.cooked).to eq(post.cooked)
end
it "does not change attributes if no localization exists for the given locale" do
LocalizationAttributesReplacer.replace_post_attributes(post, "fr")
expect(post.cooked).to eq(post.cooked)
end
end
end