Skip to content

Commit

Permalink
Add support for XLIFF placeholders
Browse files Browse the repository at this point in the history
This should allow to translate units containing placeholders such
as the one used by Angular i18n.

PoXliff will not support placehoders, because it probably doesn't
make sense to support XLIFF specific placeholders in a format that
actually embed PO placeholders.

Fixes WeblateOrg#490
Fixes WeblateOrg#1535

Signed-off-by: Adrien Crivelli <adrien.crivelli@gmail.com>
  • Loading branch information
PowerKiKi committed Nov 29, 2018
1 parent a4ee2e4 commit 12252a9
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
41 changes: 40 additions & 1 deletion weblate/formats/ttkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -728,6 +766,7 @@ class PoXliffFormat(XliffFormat):
format_id = 'poxliff'
autoload = ('.poxliff',)
loader = PoXliffFile
unit_class = PoXliffUnit


class PropertiesBaseFormat(TTKitFormat):
Expand Down
47 changes: 47 additions & 0 deletions weblate/trans/tests/test_xliff_placeholders.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions weblate/trans/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 12252a9

Please sign in to comment.