Skip to content
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 docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Weblate 2026.12

* Added a choice to :ref:`connect an existing GitHub App installation to another workspace <code-hosting-github-app-connect>`.
* Allow five minutes to assign strings to newly uploaded :ref:`screenshots` before raising an unused screenshot alert.
* The :ref:`screenshot picker <add-existing-screenshot>` prioritizes screenshots with recognized image text matching the current source string.
* Show the last translator and approver in the :ref:`translation editor <source-context>`.
* Keep the original editor search when merging a translation from the :ref:`translation context <source-context>`.

Expand Down
5 changes: 4 additions & 1 deletion docs/user/translating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ To link a screenshot that has already been uploaded, open the string in the
translation editor:

1. In the :guilabel:`Screenshots` panel, choose :guilabel:`Add existing screenshot`.
2. Browse the thumbnails, or enter a query in :guilabel:`Search screenshots` and
2. Browse the thumbnails. Screenshots with the current source phrase in their
recognized image text appear first and show an :guilabel:`Image text match`
badge. To filter by screenshot name or other :ref:`screenshot search fields
<search-screenshots>`, enter a query in :guilabel:`Search screenshots` and
choose :guilabel:`Search`. Use :guilabel:`Previous` and :guilabel:`Next` to
browse additional results.
3. Select a screenshot by clicking its card. The card is highlighted and marked
Expand Down
72 changes: 72 additions & 0 deletions weblate/screenshots/migrations/0005_screenshot_ocr_pass_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("screenshots", "0004_screenshot_backup_author_claim"),
]

operations = [
migrations.RunSQL(
# Store a bounded prefix of complete lines for each pass.
sql="""
CREATE FUNCTION weblate_ocr_pass_text(
cache jsonb, pass_index integer
) RETURNS text
LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE
RETURN (
SELECT btrim(regexp_replace(
coalesce(string_agg(line, ' ' ORDER BY line_number), ''),
'[[:space:]]+', ' ', 'g'
))
FROM (
SELECT value #>> '{}' AS line, line_number,
sum(octet_length(value #>> '{}') + 1)
OVER (ORDER BY line_number) AS total_bytes
FROM jsonb_path_query(
cache -> 'passes' -> pass_index,
'$.lines[*].text ? (@.type() == "string")'
) WITH ORDINALITY AS recognized(value, line_number)
) AS lines
WHERE total_bytes <= 32768
);
""",
reverse_sql="DROP FUNCTION weblate_ocr_pass_text(jsonb, integer);",
),
migrations.AddField(
model_name="screenshotocr",
name="first_pass_text",
field=models.GeneratedField(
expression=models.Func(
"data",
models.Value(0),
function="weblate_ocr_pass_text",
output_field=models.TextField(),
),
output_field=models.TextField(),
db_persist=True,
null=True,
),
),
migrations.AddField(
model_name="screenshotocr",
name="second_pass_text",
field=models.GeneratedField(
expression=models.Func(
"data",
models.Value(1),
function="weblate_ocr_pass_text",
output_field=models.TextField(),
),
output_field=models.TextField(),
db_persist=True,
null=True,
),
),
]
23 changes: 23 additions & 0 deletions weblate/screenshots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,29 @@ class ScreenshotOCR(models.Model):
requested_at = models.DateTimeField(null=True)
error = models.CharField(max_length=16, blank=True)

first_pass_text = models.GeneratedField(
expression=models.Func(
"data",
models.Value(0),
function="weblate_ocr_pass_text",
output_field=models.TextField(),
),
output_field=models.TextField(),
db_persist=True,
null=True,
)
second_pass_text = models.GeneratedField(
expression=models.Func(
"data",
models.Value(1),
function="weblate_ocr_pass_text",
output_field=models.TextField(),
),
output_field=models.TextField(),
db_persist=True,
null=True,
)

def __str__(self) -> str:
return f"Text recognition for screenshot {self.screenshot_id}"

Expand Down
204 changes: 203 additions & 1 deletion weblate/screenshots/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from weblate.screenshots.ocr import (
OCR_LINE_LIMIT,
OCR_TEXT_LENGTH_LIMIT,
OCR_WORDS_PER_LINE_LIMIT,
TESSERACT_DOWNLOAD_ATTEMPTS,
TESSERACT_DOWNLOAD_TIMEOUT,
OCRLine,
Expand Down Expand Up @@ -2859,12 +2860,211 @@ def setUp(self) -> None:
name="Target preview", translation=self.unit.translation
)

def cache_text(self, screenshot: Screenshot, *lines: str, error: str = "") -> None:
ScreenshotOCR.objects.create(
screenshot=screenshot,
data={"passes": [{"lines": [{"text": line} for line in lines]}]},
error=error,
)

def test_ocr_matches_before_other_screenshots(self) -> None:
self.cache_text(self.source_screenshot, "Hello")
self.cache_text(self.target_screenshot, "HELLO,", "world!")
pending = Screenshot.objects.create(
name="Pending preview", translation=self.unit.translation
)
ScreenshotOCR.objects.create(screenshot=pending)
failed = Screenshot.objects.create(
name="Failed preview", translation=self.unit.translation
)
self.cache_text(failed, "Hello, world!", error="failed")

response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[self.target_screenshot, failed, pending, self.source_screenshot],
)
document = html.fromstring(response.content)
self.assertEqual(
document.xpath('//label[.//span[text()="Image text match"]]/@for'),
[f"screenshot-choice-{self.target_screenshot.pk}"],
)
self.assertFalse(document.xpath('//input[@name="screenshot"][@checked]'))

response = self.client.get(self.url, {"q": "Source"})
self.assertEqual(list(response.context["page_obj"]), [self.source_screenshot])
self.assertNotContains(response, "Image text match")

def test_ocr_match_order_across_pages(self) -> None:
self.source_screenshot.name = self.target_screenshot.name
self.source_screenshot.save(update_fields=["name"])
for screenshot in (self.source_screenshot, self.target_screenshot):
self.cache_text(screenshot, "Hello, world!")

params = {"page": 1}
# Warm page-level caches before comparing query counts.
self.client.get(self.url, params)
with CaptureQueriesContext(connection) as small_list:
self.assertEqual(self.client.get(self.url, params).status_code, 200)

unmatched = Screenshot.objects.bulk_create(
Screenshot(name=f"Earlier {index:02}", translation=self.unit.translation)
for index in range(47)
)
with CaptureQueriesContext(connection) as full_page:
response = self.client.get(self.url, params)
self.assertEqual(len(full_page), len(small_list))
self.assertEqual(
list(response.context["page_obj"]),
[self.source_screenshot, self.target_screenshot, *unmatched[:46]],
)
response = self.client.get(self.url, {"page": 2})
self.assertEqual(list(response.context["page_obj"]), unmatched[46:])

def test_ocr_match_requires_an_ordered_phrase_in_one_pass(self) -> None:
Unit.objects.filter(pk=self.unit.pk).update(source="Hello world")
self.cache_text(self.source_screenshot, "Hello draft", "Discard world")
self.cache_text(self.target_screenshot, "world Hello")
split = Screenshot.objects.create(
name="Split between passes", translation=self.unit.translation
)
ScreenshotOCR.objects.create(
screenshot=split,
data={
"passes": [
{"lines": [{"text": "Hello"}]},
{"lines": [{"text": "world"}]},
]
},
)
matching = Screenshot.objects.create(
name="Wrapped phrase on second pass", translation=self.unit.translation
)
ScreenshotOCR.objects.create(
screenshot=matching,
data={
"passes": [
{"lines": [{"text": "world Hello"}]},
{"lines": [{"text": "Hello"}, {"text": "world"}]},
]
},
)

response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[matching, self.source_screenshot, split, self.target_screenshot],
)
self.assertContains(response, "Image text match", count=1)

def test_ocr_match_with_large_cache(self) -> None:
# Exercise a cache near the worker's line and word limits.
lines = [
{
"text": " ".join(
f"prefix{number:05d}aaaaaaaaaa-suffix{number:05d}bbbbbbbbbb"
for number in range(start, start + OCR_WORDS_PER_LINE_LIMIT)
)
}
for start in range(
0,
2 * OCR_LINE_LIMIT * OCR_WORDS_PER_LINE_LIMIT,
OCR_WORDS_PER_LINE_LIMIT,
)
]
ScreenshotOCR.objects.create(
screenshot=self.target_screenshot,
data={
"passes": [
{"lines": [{"text": "Hello, world!"}, *lines[1:OCR_LINE_LIMIT]]},
{
"lines": [
*lines[OCR_LINE_LIMIT:-1],
{"text": "Outside byte budget"},
]
},
]
},
)
response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[self.target_screenshot, self.source_screenshot],
)
self.assertContains(response, "Image text match", count=1)

Unit.objects.filter(pk=self.unit.pk).update(source="Outside byte budget")
self.assertNotContains(self.client.get(self.url), "Image text match")

def test_ocr_matches_text_without_word_spaces(self) -> None:
Component.objects.filter(pk=self.component.pk).update(
source_language=Language.objects.get(code="zh_Hans")
)
Unit.objects.filter(pk=self.unit.pk).update(source="保存设置")
self.cache_text(self.source_screenshot, "点击保存设置继续")
self.cache_text(self.target_screenshot, "点击保存", "设置继续")

response = self.client.get(self.url)
self.assertContains(response, "Image text match", count=2)

def test_ocr_match_keeps_symbols_and_word_boundaries(self) -> None:
Unit.objects.filter(pk=self.unit.pk).update(source="C++")
self.cache_text(self.source_screenshot, "C#, C++17 and XC++")
self.cache_text(self.target_screenshot, "Learn C#")
self.assertNotContains(self.client.get(self.url), "Image text match")
ScreenshotOCR.objects.filter(screenshot=self.target_screenshot).update(
data={
"passes": [
{"lines": [{"text": "Learn"}, {"text": "c++ for beginners"}]}
]
}
)

response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[self.target_screenshot, self.source_screenshot],
)
self.assertContains(response, "Image text match", count=1)
response = self.client.get(self.url, {"q": "Source"})
self.assertNotContains(response, "Image text match")

def test_ocr_match_without_searchable_source(self) -> None:
self.cache_text(self.target_screenshot, "Hello world")
Unit.objects.filter(pk=self.unit.pk).update(source="?!")
response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[self.source_screenshot, self.target_screenshot],
)
self.assertNotContains(response, "Image text match")

def test_ocr_match_skips_oversized_source(self) -> None:
# The first 512 characters appear in OCR but are only part of the source.
prefix = "Hello " * 85 + "wo"
Unit.objects.filter(pk=self.unit.pk).update(source=f"{prefix}rld")
self.cache_text(self.target_screenshot, prefix)
response = self.client.get(self.url)
self.assertEqual(
list(response.context["page_obj"]),
[self.source_screenshot, self.target_screenshot],
)
self.assertNotContains(response, "Image text match")

def test_candidates(self) -> None:
other = self.component.translation_set.exclude(
pk__in=[self.unit.translation_id, self.unit.source_unit.translation_id]
).first()
assert other is not None
Screenshot.objects.create(name="Other language preview", translation=other)
other_screenshot = Screenshot.objects.create(
name="Other language preview", translation=other
)
for screenshot in (
other_screenshot,
self.source_screenshot,
self.target_screenshot,
):
self.cache_text(screenshot, "Hello, world!")
response = self.client.get(self.url)
self.assertContains(response, "Source preview")
self.assertContains(response, "Target preview")
Expand Down Expand Up @@ -2959,6 +3159,7 @@ def test_other_component(self) -> None:
Translation.objects.bulk_create([translation])
screenshot = Screenshot(name="Other component preview", translation=translation)
Screenshot.objects.bulk_create([screenshot])
self.cache_text(screenshot, "Hello, world!")
response = self.client.get(self.url)
self.assertNotContains(response, "Other component preview")
response = self.client.post(self.url, {"screenshot": screenshot.pk})
Expand All @@ -2984,6 +3185,7 @@ def test_permissions(self) -> None:

def test_language_scope(self) -> None:
self.make_language_scoped_user("cs", "screenshot.edit")
self.cache_text(self.source_screenshot, "Hello, world!")

response = self.client.get(self.url)
self.assertContains(response, "Target preview")
Expand Down
Loading
Loading