Skip to content

Commit

Permalink
Translate: Highlight placeables in translation editor
Browse files Browse the repository at this point in the history
Using CodeMirror overlay to highlight placeables from the source string.

Fixes WeblateOrg#3504
  • Loading branch information
nijel committed Nov 20, 2020
1 parent 18ffae3 commit 9a26161
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Not yet released.
* Added addon to remove blank strings from translation files.
* The CodeMirror editor is now used for translations.
* Syntax highlighting in translation editor for XML, HTML, Markdown and reStructuredText.
* Highlight placeables in translation editor.

Weblate 4.3.2
-------------
Expand Down
12 changes: 11 additions & 1 deletion weblate/static/editor/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ WLT.Editor = (function () {
if (textarea.CodeMirror) {
return;
}
console.log("Mode", textarea.getAttribute("data-mode"));
let codemirror = CodeMirror.weblateEditor(
textarea,
textarea.getAttribute("data-mode")
Expand All @@ -95,6 +94,17 @@ WLT.Editor = (function () {
stream.skipTo(" ") || stream.skipToEnd();
},
});
let placeables = textarea.getAttribute("data-placeables");
if (placeables) {
codemirror.addOverlay({
token: function (stream) {
if (stream.match(placeables)) {
return "placeable";
}
stream.next();
},
});
}

codemirror.on("change", () => {
WLT.Utils.markTranslated($(textarea).closest("form"));
Expand Down
3 changes: 3 additions & 0 deletions weblate/static/style-bootstrap.css
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,12 @@ div.CodeMirror.cm-s-default {
font-family: inherit;
}

.cm-placeable,
.hlcheck {
border: 1px dotted #aeb7cf;
background-color: #e0eaf1;
}
.hlcheck {
display: inline-block;
cursor: pointer;
}
Expand Down
4 changes: 4 additions & 0 deletions weblate/trans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import copy
import json
import re
from datetime import date, datetime, timedelta
from typing import Dict, List

Expand All @@ -45,6 +46,7 @@

from weblate.auth.models import User
from weblate.checks.models import CHECKS
from weblate.checks.utils import highlight_string
from weblate.formats.models import EXPORTERS, FILE_FORMATS
from weblate.lang.models import Language
from weblate.machinery import MACHINE_TRANSLATION_SERVICES
Expand Down Expand Up @@ -288,6 +290,7 @@ def render(self, name, value, attrs=None, renderer=None, **kwargs):
lang = unit.translation.language
plural = unit.translation.plural
tabindex = self.attrs["tabindex"]
placeables = [hl[2] for hl in highlight_string(unit.source_string, unit)]

# Need to add extra class
attrs["class"] = "translation-editor form-control"
Expand All @@ -297,6 +300,7 @@ def render(self, name, value, attrs=None, renderer=None, **kwargs):
attrs["rows"] = 3
attrs["maxlength"] = unit.get_max_length()
attrs["data-mode"] = unit.edit_mode
attrs["data-placeables"] = "|".join(re.escape(pl) for pl in placeables)
if unit.readonly:
attrs["readonly"] = 1

Expand Down

0 comments on commit 9a26161

Please sign in to comment.