-
-
Notifications
You must be signed in to change notification settings - Fork 955
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 2 blank lines, found 1 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -728,6 +766,7 @@ class PoXliffFormat(XliffFormat): | |
format_id = 'poxliff' | ||
autoload = ('.poxliff',) | ||
loader = PoXliffFile | ||
unit_class = PoXliffUnit | ||
|
||
|
||
class PropertiesBaseFormat(TTKitFormat): | ||
|
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) |
There was a problem hiding this comment.
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.