Skip to content

DEV: Also translate excerpts #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
< 3.5.0.beta5-dev: 5c44f829ef82ded3416b0cddc521e9e6d62ed534
< 3.5.0.beta4-dev: 14ca3c07efa0a80712a4cbb8ca455c32a727adec
< 3.5.0.beta2-dev: 5f24835801fdc7cb98e1bcf42d2ab2e49e609921
< 3.5.0.beta1-dev: 7d411e458bdd449f8aead2bc07cedeb00b856798
Expand Down
3 changes: 3 additions & 0 deletions app/services/discourse_translator/topic_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ def self.translate(topic, target_locale = I18n.locale)
translator = DiscourseTranslator::Provider::TranslatorProvider.get
translated_title = translator.translate_topic!(topic, target_locale_sym)

translated_excerpt = translator.translate_text!(topic.excerpt, target_locale_sym)

localization =
TopicLocalization.find_or_initialize_by(topic_id: topic.id, locale: target_locale_sym.to_s)

localization.title = translated_title
localization.fancy_title = Topic.fancy_title(translated_title)
localization.excerpt = translated_excerpt
localization.localizer_user_id = Discourse.system_user.id
localization.save!
localization
Expand Down
16 changes: 15 additions & 1 deletion spec/services/topic_translator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

describe DiscourseTranslator::TopicTranslator do
describe ".translate" do
fab!(:topic) { Fabricate(:topic, title: "this is a cat topic :)") }
fab!(:topic) do
Fabricate(
:topic,
title: "this is a cat topic :)",
excerpt: "cats are great. how many do you have?",
)
end
let(:translator) { mock }
let(:translated_title) { "これは猫の話題です :)" }
let(:translated_excerpt) { "猫は素晴らしいですね。何匹飼っていますか?" }
let(:fancy_title) { "これは猫の話題です :slight_smile:" }
let(:target_locale) { "ja" }

before do
DiscourseTranslator::Provider::TranslatorProvider.stubs(:get).returns(translator)
translator.stubs(:translate_topic!).with(topic, :ja).returns(translated_title)
translator.stubs(:translate_text!).with(topic.excerpt, :ja).returns(translated_excerpt)
end

it "returns nil if topic is blank" do
Expand All @@ -30,12 +38,14 @@

it "translates with topic and locale" do
translator.expects(:translate_topic!).with(topic, :ja).returns(translated_title)
translator.expects(:translate_text!).with(topic.excerpt, :ja).returns(translated_title)

described_class.translate(topic, "ja")
end

it "normalizes dashes to underscores and symbol type for locale" do
translator.expects(:translate_topic!).with(topic, :zh_CN).returns("这是一个猫主题 :)")
translator.expects(:translate_text!).with(topic.excerpt, :zh_CN).returns("这是一个猫主题 :)")

described_class.translate(topic, "zh-CN")
end
Expand All @@ -48,6 +58,7 @@
topic_id: topic.id,
locale: target_locale,
title: translated_title,
excerpt: translated_excerpt,
fancy_title: fancy_title,
localizer_user_id: Discourse.system_user.id,
)
Expand All @@ -61,17 +72,20 @@
topic:,
locale: "ja",
title: "old title",
excerpt: "old excerpt",
fancy_title: "old_fancy_title",
)
expect {
expect(described_class.translate(topic, "ja")).to have_attributes(
id: localization.id,
title: translated_title,
fancy_title: fancy_title,
excerpt: translated_excerpt,
)
expect(localization.reload).to have_attributes(
title: translated_title,
fancy_title: fancy_title,
excerpt: translated_excerpt,
)
}.to_not change { TopicLocalization.count }
end
Expand Down
Loading