Skip to content

Commit e1d0887

Browse files
authored
DEV: Also translate excerpts (#304)
Related: discourse/discourse#32839 We also want to translate excerpts when translating topics.
1 parent 5c44f82 commit e1d0887

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.discourse-compatibility

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
< 3.5.0.beta5-dev: 5c44f829ef82ded3416b0cddc521e9e6d62ed534
12
< 3.5.0.beta4-dev: 14ca3c07efa0a80712a4cbb8ca455c32a727adec
23
< 3.5.0.beta2-dev: 5f24835801fdc7cb98e1bcf42d2ab2e49e609921
34
< 3.5.0.beta1-dev: 7d411e458bdd449f8aead2bc07cedeb00b856798

app/services/discourse_translator/topic_translator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ def self.translate(topic, target_locale = I18n.locale)
1010
translator = DiscourseTranslator::Provider::TranslatorProvider.get
1111
translated_title = translator.translate_topic!(topic, target_locale_sym)
1212

13+
translated_excerpt = translator.translate_text!(topic.excerpt, target_locale_sym)
14+
1315
localization =
1416
TopicLocalization.find_or_initialize_by(topic_id: topic.id, locale: target_locale_sym.to_s)
1517

1618
localization.title = translated_title
1719
localization.fancy_title = Topic.fancy_title(translated_title)
20+
localization.excerpt = translated_excerpt
1821
localization.localizer_user_id = Discourse.system_user.id
1922
localization.save!
2023
localization

spec/services/topic_translator_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
describe DiscourseTranslator::TopicTranslator do
44
describe ".translate" do
5-
fab!(:topic) { Fabricate(:topic, title: "this is a cat topic :)") }
5+
fab!(:topic) do
6+
Fabricate(
7+
:topic,
8+
title: "this is a cat topic :)",
9+
excerpt: "cats are great. how many do you have?",
10+
)
11+
end
612
let(:translator) { mock }
713
let(:translated_title) { "これは猫の話題です :)" }
14+
let(:translated_excerpt) { "猫は素晴らしいですね。何匹飼っていますか?" }
815
let(:fancy_title) { "これは猫の話題です :slight_smile:" }
916
let(:target_locale) { "ja" }
1017

1118
before do
1219
DiscourseTranslator::Provider::TranslatorProvider.stubs(:get).returns(translator)
1320
translator.stubs(:translate_topic!).with(topic, :ja).returns(translated_title)
21+
translator.stubs(:translate_text!).with(topic.excerpt, :ja).returns(translated_excerpt)
1422
end
1523

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

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

3443
described_class.translate(topic, "ja")
3544
end
3645

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

4050
described_class.translate(topic, "zh-CN")
4151
end
@@ -48,6 +58,7 @@
4858
topic_id: topic.id,
4959
locale: target_locale,
5060
title: translated_title,
61+
excerpt: translated_excerpt,
5162
fancy_title: fancy_title,
5263
localizer_user_id: Discourse.system_user.id,
5364
)
@@ -61,17 +72,20 @@
6172
topic:,
6273
locale: "ja",
6374
title: "old title",
75+
excerpt: "old excerpt",
6476
fancy_title: "old_fancy_title",
6577
)
6678
expect {
6779
expect(described_class.translate(topic, "ja")).to have_attributes(
6880
id: localization.id,
6981
title: translated_title,
7082
fancy_title: fancy_title,
83+
excerpt: translated_excerpt,
7184
)
7285
expect(localization.reload).to have_attributes(
7386
title: translated_title,
7487
fancy_title: fancy_title,
88+
excerpt: translated_excerpt,
7589
)
7690
}.to_not change { TopicLocalization.count }
7791
end

0 commit comments

Comments
 (0)