Skip to content

Commit 6b6df5d

Browse files
authored
FIX: Update localisation instead of category (#283)
Fix - we were updating categories instead of the localizations.
1 parent 926197c commit 6b6df5d

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

app/services/discourse_translator/category_translator.rb

+10-1
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

+36-10
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,48 @@
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
31-
translator.expects(:translate_text!).with(category.name, :fr_CA).returns("Catégorie de Test")
56+
translator.expects(:translate_text!).with(category.name, :fr).returns("Catégorie de Test")
3257
translator
3358
.expects(:translate_text!)
34-
.with(category.description, :fr_CA)
59+
.with(category.description, :fr)
3560
.returns("C'est une catégorie de test")
3661

37-
DiscourseTranslator::CategoryTranslator.translate(category, "fr-CA")
62+
res = DiscourseTranslator::CategoryTranslator.translate(category, "fr")
3863

39-
expect(category.name).to eq("Catégorie de Test")
40-
expect(category.description).to eq("C'est une catégorie de test")
64+
expect(res.name).to eq("Catégorie de Test")
65+
expect(res.description).to eq("C'est une catégorie de test")
4166
end
4267

4368
it "returns nil if category is blank" do
@@ -56,10 +81,11 @@
5681
.with(category.description, :es)
5782
.returns("Esta es una categoría de prueba")
5883

59-
DiscourseTranslator::CategoryTranslator.translate(category)
84+
res = DiscourseTranslator::CategoryTranslator.translate(category)
6085

61-
expect(category.name).to eq("Categoría de Prueba")
62-
expect(category.description).to eq("Esta es una categoría de prueba")
86+
expect(res.name).to eq("Categoría de Prueba")
87+
expect(res.description).to eq("Esta es una categoría de prueba")
88+
expect(res.locale).to eq("es")
6389
end
6490
end
6591
end

0 commit comments

Comments
 (0)