Skip to content

Commit fb8190b

Browse files
committed
FIX: Ensure untranslated post ids exclude those already translated to target locale
1 parent d7baa82 commit fb8190b

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

+17-28
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,29 @@ def execute(args = nil)
1515
def fetch_untranslated_model_ids(model, content_column, limit, target_locale)
1616
m = model.name.downcase
1717

18-
# Query selects every model (post/topic) *except* those who are **both**
19-
# already locale detected and translated
2018
DB.query_single(<<~SQL, target_locale: target_locale, limit: limit)
21-
SELECT * FROM
22-
(
23-
( -- every post / topic
24-
SELECT m.id
25-
FROM #{model.table_name} m
26-
#{limit_to_public_clause(model)}
27-
WHERE m.deleted_at IS NULL
28-
AND m.#{content_column} != ''
29-
AND m.user_id > 0
30-
#{max_age_clause}
31-
ORDER BY m.updated_at DESC
32-
)
33-
EXCEPT
34-
(
35-
( -- locale detected
36-
SELECT
37-
#{m}_id
38-
FROM
39-
discourse_translator_#{m}_locales
40-
WHERE
41-
detected_locale = :target_locale
19+
SELECT m.id
20+
FROM #{model.table_name} m
21+
#{limit_to_public_clause(model)}
22+
WHERE m.deleted_at IS NULL
23+
AND m.#{content_column} != ''
24+
AND m.user_id > 0
25+
#{max_age_clause}
26+
AND (
27+
m.id NOT IN (
28+
SELECT #{m}_id
29+
FROM discourse_translator_#{m}_locales
30+
WHERE detected_locale = :target_locale
4231
)
43-
INTERSECT
44-
( -- translated
32+
)
33+
AND (
34+
m.id NOT IN (
4535
SELECT #{m}_id
4636
FROM discourse_translator_#{m}_translations
47-
WHERE
48-
locale = :target_locale
37+
WHERE locale = :target_locale
4938
)
5039
)
51-
) AS subquery
40+
ORDER BY m.updated_at DESC
5241
LIMIT :limit
5342
SQL
5443
end

spec/jobs/automatic_translation_backfill_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,13 @@ def expect_google_translate(text)
251251

252252
expect(result).not_to include(post_1.id)
253253
end
254+
255+
it "does not return posts that already have the target locale translation, even if detected_locale is not the target" do
256+
post_7.set_detected_locale("en")
257+
post_7.set_translation("de", "hallo")
258+
259+
result = described_class.new.fetch_untranslated_model_ids(Post, "raw", 50, "de")
260+
expect(result).not_to include(post_7.id)
261+
end
254262
end
255263
end

0 commit comments

Comments
 (0)