Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for XLIFF placeholders #2364

Merged
merged 1 commit into from
Nov 30, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 2 blank lines, found 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure this is needed. PoXliff is also used natively to get some of the gettext features to xliff, so disabling xliff placeholders might be not intended here.


@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