Repository: WeblateOrg/weblate

Issue:
Support for xliff generic placeholder - <x />
Xliff entry with variables in it looks something like this:
```
<trans-unit id="6644865400824058724" datatype="html">
    <source>Shot by player <x id="PLAYER"/></source>
</trans-unit>
```
### Actual behaviour
Weblate UI does not seem to handle [\<x />](http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#x) tags - it just omits it from source field.
[example screenshot](https://ibb.co/crFgTQ)

### Expected behaviour
\<x /> placeholders should be displayed in translation source input field, so that translators would be aware of them. There should also be a way to enter these placeholders in translation target input field.



Discussion:
@nijel, since this is a blocker for me, I'd be willing to submit a PR for it, but I have no experience in Weblate. Could you point me in the right direction, on how to contribute in general, and what should be done in what part of code for this specific feature ?
This is a duplicate of #490 (or the other way around). One of them should probably be closed.

PS: To answer my own previous question, see https://github.com/WeblateOrg/weblate/issues/490#issuecomment-54791842
Good point, it indeed is (though it doesn't specifically mention placeholders).

Duplicate of #490

Resolving patch:
diff --git a/weblate/formats/ttkit.py b/weblate/formats/ttkit.py
--- a/weblate/formats/ttkit.py
+++ b/weblate/formats/ttkit.py
@@ -45,7 +45,9 @@
 
 from weblate.formats.base import TranslationUnit, TranslationFormat
 
-from weblate.trans.util import get_string, join_plural
+from weblate.trans.util import (
+    get_string, join_plural, xliff_string_to_rich, rich_to_xliff_string
+)
 
 from weblate.utils.errors import report_error
 
@@ -415,6 +417,29 @@ class XliffUnit(TTKitUnit):
         'new', 'needs-translation', 'needs-adaptation', 'needs-l10n'
     ))
 
+    @cached_property
+    def source(self):
+        """Return source string from a ttkit unit."""
+
+        if self.template is not None:
+            return rich_to_xliff_string(self.template.rich_target)
+        return rich_to_xliff_string(self.unit.rich_source)
+
+    @cached_property
+    def target(self):
+        """Return target string from a ttkit unit."""
+        if self.unit is None:
+            return ''
+
+        if self.unit.target is None:
+            return ''
+
+        return rich_to_xliff_string(self.unit.rich_target)
+
+    def set_target(self, target):
+        """Set translation unit target."""
+        self.unit.rich_target = xliff_string_to_rich(target)
+
     @cached_property
     def xliff_node(self):
         return self.unit.getlanguageNode(lang=None, index=1)
@@ -501,6 +526,19 @@ def mark_approved(self, value):
         if self.xliff_state:
             self.xliff_node.set('state', 'final' if value else 'translated')
 
+class PoXliffUnit(XliffUnit):
+
+    @cached_property
+    def source(self):
+        return TTKitUnit.source.__get__(self)
+
+    @cached_property
+    def target(self):
+        return TTKitUnit.target.__get__(self)
+
+    def set_target(self, target):
+        TTKitUnit.set_target(self, target)
+
 
 class MonolingualIDUnit(TTKitUnit):
     @cached_property
@@ -728,6 +766,7 @@ class PoXliffFormat(XliffFormat):
     format_id = 'poxliff'
     autoload = ('.poxliff',)
     loader = PoXliffFile
+    unit_class = PoXliffUnit
 
 
 class PropertiesBaseFormat(TTKitFormat):
diff --git a/weblate/trans/util.py b/weblate/trans/util.py
--- a/weblate/trans/util.py
+++ b/weblate/trans/util.py
@@ -33,6 +33,8 @@
 from django.utils.encoding import force_text
 from django.utils.http import is_safe_url
 from django.utils.translation import ugettext as _, ugettext_lazy
+from translate.storage.placeables.lisa import parse_xliff, strelem_to_xml
+from lxml import etree
 
 try:
     import pyuca  # pylint: disable=import-error
@@ -284,3 +286,31 @@ def redirect_next(next_url, fallback):
             not next_url.startswith('/')):
         return redirect(fallback)
     return HttpResponseRedirect(next_url)
+
+
+def xliff_string_to_rich(string):
+    """XLIFF string to StringElement
+
+    Transform a string containing XLIFF placeholders as XML
+    into a rich content (StringElement)
+    """
+
+    return [parse_xliff(string)]
+
+
+def rich_to_xliff_string(string_elements):
+    """StringElement to XLIFF string
+
+    Transform rich content (StringElement) into
+    a string with placeholder kept as XML
+    """
+
+    result = ''
+    for string_element in string_elements:
+        xml = etree.Element(u'e')
+        strelem_to_xml(xml, string_element)
+        string_xml = etree.tostring(xml, encoding="unicode")
+        string_without_wrapping_element = string_xml[3:][:-4]
+        result += string_without_wrapping_element
+
+    return result


Test patch:
diff --git a/weblate/trans/tests/test_xliff_placeholders.py b/weblate/trans/tests/test_xliff_placeholders.py
new file mode 100644
--- /dev/null
+++ b/weblate/trans/tests/test_xliff_placeholders.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © 2012 - 2018 Michal Čihař <michal@cihar.com>
+#
+# This file is part of Weblate <https://weblate.org/>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+#
+
+"""
+Tests for XLIFF rich string.
+"""
+
+from django.test import TestCase
+
+from weblate.trans.util import xliff_string_to_rich, rich_to_xliff_string
+from translate.storage.placeables.strelem import StringElem
+
+
+class XliffPlaceholdersTest(TestCase):
+
+    def test_bidirectional_xliff_string(self):
+        cases = [
+            'foo <x id="INTERPOLATION" equiv-text="{{ angularExpression }}"/> bar',
+            '',
+            'hello world',
+            'hello <p>world</p>'
+        ]
+
+        for string in cases:
+            rich = xliff_string_to_rich(string)
+            self.assertTrue(isinstance(rich, list))
+            self.assertTrue(isinstance(rich[0], StringElem))
+
+            final_string = rich_to_xliff_string(rich)
+            self.assertEqual(string, final_string)