Skip to content

Commit 75ee60f

Browse files
authored
Highlight placeables in the Translation Memory Tab (mozilla#4061)
* Fixes mozilla#3445 Highlight placeables in the Translation Memory Tab * Running make ruff * Fixing conflict from search and placeable highlight * Make the background of the placeable more darker * Fixing placeable and search highlight conflict
1 parent be3dcc7 commit 75ee60f

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

pontoon/base/templatetags/helpers.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from django.utils.http import url_has_allowed_host_and_scheme
2525
from django.utils.safestring import mark_safe
2626

27+
from pontoon.base.placeables import get_placeables
2728
from pontoon.base.simple_preview import get_simple_preview
2829

2930

@@ -86,6 +87,25 @@ def full_static(path):
8687
return urljoin(settings.SITE_URL, static(path))
8788

8889

90+
@library.filter
91+
def highlight_placeables(text):
92+
if not text:
93+
return text
94+
95+
placeables = get_placeables(text)
96+
97+
escaped = str(text) if isinstance(text, markupsafe.Markup) else html.escape(text)
98+
99+
for p in sorted(placeables, key=len, reverse=True):
100+
escaped_p = html.escape(p)
101+
escaped = escaped.replace(
102+
escaped_p,
103+
f'<mark class="placeable" data-match="{escaped_p}">{escaped_p}</mark>',
104+
)
105+
106+
return markupsafe.Markup(escaped)
107+
108+
89109
@library.filter
90110
def to_json(value):
91111
return json.dumps(value, cls=DjangoJSONEncoder)
@@ -303,7 +323,11 @@ def highlight_matches(
303323
if not search_query:
304324
return text
305325

306-
escaped_text = escape(text)
326+
if isinstance(text, markupsafe.Markup):
327+
escaped_text = str(text)
328+
else:
329+
escaped_text = escape(text)
330+
307331
flags = 0 if match_case_enabled else re.IGNORECASE
308332

309333
if advanced:

pontoon/teams/static/css/translation_memory.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@
123123
border-radius: 3px;
124124
}
125125

126+
mark.placeable {
127+
background: var(--dark-grey-2);
128+
border: 1px solid var(--light-grey-1);
129+
border-radius: 2px;
130+
color: var(--light-grey-6);
131+
font-style: normal;
132+
font-weight: normal;
133+
margin: 0 1px;
134+
}
135+
126136
.empty {
127137
border: 1px solid var(--main-border-1);
128138
border-radius: 3px;

pontoon/teams/templates/teams/widgets/translation_memory_entries.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
{% for entry in tm_entries %}
1414
<tr data-ids="{{ entry.ids }}">
1515
<td class="source text">
16-
{{ linkify(entry.source|highlight_matches(search_query), entry.entity_ids) }}
16+
{{ linkify(entry.source|highlight_placeables|highlight_matches(search_query), entry.entity_ids) }}
1717
</td>
1818
<td class="target text">
1919
<div class="content-wrapper">
20-
{{ linkify(entry.target|highlight_matches(search_query), entry.entity_ids) }}
20+
{{ linkify(entry.target|highlight_placeables|highlight_matches(search_query), entry.entity_ids) }}
2121
</div>
2222
<textarea>{{ entry.target }}</textarea>
2323
</td>

0 commit comments

Comments
 (0)