Skip to content

Commit 2039a68

Browse files
committed
FIX: Update localisation instead of category
1 parent 926197c commit 2039a68

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

app/services/discourse_translator/category_translator.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ def self.translate(category, target_locale = I18n.locale)
1616
translated_name = translator.translate_text!(category.name, target_locale_sym)
1717
translated_description = translator.translate_text!(category.description, target_locale_sym)
1818

19-
category.update!(name: translated_name, description: translated_description)
19+
localization =
20+
CategoryLocalization.find_or_initialize_by(
21+
category_id: category.id,
22+
locale: target_locale_sym.to_s,
23+
)
24+
25+
localization.name = translated_name
26+
localization.description = translated_description
27+
localization.save!
28+
localization
2029
end
2130
end
2231
end

spec/services/category_translator_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,35 @@
2121
.with(category.description, target_locale)
2222
.returns("C'est une catégorie de test")
2323

24+
res = DiscourseTranslator::CategoryTranslator.translate(category, target_locale)
25+
26+
expect(res.name).to eq("Catégorie de Test")
27+
expect(res.description).to eq("C'est une catégorie de test")
28+
end
29+
30+
it "translates the category name and description" do
31+
localized =
32+
Fabricate(
33+
:category_localization,
34+
category: category,
35+
locale: target_locale,
36+
name: "X",
37+
description: "Y",
38+
)
39+
translator
40+
.expects(:translate_text!)
41+
.with(category.name, target_locale)
42+
.returns("Catégorie de Test")
43+
translator
44+
.expects(:translate_text!)
45+
.with(category.description, target_locale)
46+
.returns("C'est une catégorie de test")
47+
2448
DiscourseTranslator::CategoryTranslator.translate(category, target_locale)
2549

26-
expect(category.name).to eq("Catégorie de Test")
27-
expect(category.description).to eq("C'est une catégorie de test")
50+
localized.reload
51+
expect(localized.name).to eq("Catégorie de Test")
52+
expect(localized.description).to eq("C'est une catégorie de test")
2853
end
2954

3055
it "handles locale format standardization" do

0 commit comments

Comments
 (0)