Exemplo n.º 1
0
 def set_target(self, target):
     """Set translation unit target."""
     # Use source for monolingual files if target is not set
     if self.template is not None and not self.template.target:
         self.template.rich_source = xliff_string_to_rich(target)
     else:
         self.unit.rich_target = xliff_string_to_rich(target)
Exemplo n.º 2
0
 def build_unit(self, unit):
     output = super().build_unit(unit)
     try:
         converted_source = xliff_string_to_rich(unit.get_source_plurals())
         converted_target = xliff_string_to_rich(unit.get_target_plurals())
     except (XMLSyntaxError, TypeError):
         return output
     output.rich_source = converted_source
     output.set_rich_target(converted_target, self.language.code)
     return output
Exemplo n.º 3
0
 def build_unit(self, unit):
     try:
         converted_source = xliff_string_to_rich(unit.source)
         converted_target = xliff_string_to_rich(unit.target)
     except XMLSyntaxError:
         return super().build_unit(unit)
     output = self.storage.UnitClass("")
     output.rich_source = converted_source
     output.set_rich_target(converted_target, self.language.code)
     return output
Exemplo n.º 4
0
    def test_xliff_roundtrip_unknown(self):
        with open(TEST_MRK, "rb") as handle:
            source = handle.read()

        store = xlifffile.parsestring(source)
        string = rich_to_xliff_string(store.units[0].rich_source)
        self.assertEqual('T: <mrk mtype="protected">%s</mrk>', string)
        store.units[0].rich_source = xliff_string_to_rich(string)
        self.assertEqual(source, bytes(store))
Exemplo n.º 5
0
    def test_xliff_roundtrip(self):
        with open(TEST_X, "rb") as handle:
            source = handle.read()

        store = xlifffile.parsestring(source)
        string = rich_to_xliff_string(store.units[0].rich_source)
        self.assertEqual(
            'T: <x id="INTERPOLATION" equiv-text="{{ angular }}"/>', string)
        store.units[0].rich_source = xliff_string_to_rich(string)
        self.assertEqual(source, bytes(store))
Exemplo n.º 6
0
 def set_target(self, target):
     """Set translation unit target."""
     self._invalidate_target()
     try:
         converted = xliff_string_to_rich(target)
     except XMLSyntaxError:
         converted = [target]
     if self.template is not None:
         if self.parent.is_template:
             # Use source for monolingual files if editing template
             self.unit.rich_source = converted
             return
         if self.unit.source:
             # Update source if it is already set to keep it in sync
             try:
                 self.unit.rich_source = xliff_string_to_rich(
                     self.template.source)
             except XMLSyntaxError:
                 self.unit.rich_source = [self.template.source]
     self.unit.rich_target = converted
Exemplo n.º 7
0
 def set_target(self, target):
     """Set translation unit target."""
     try:
         converted = xliff_string_to_rich(target)
     except XMLSyntaxError:
         converted = target
     # Use source for monolingual files if target is not set
     if self.template is not None and not self.template.target:
         self.unit.rich_source = converted
     else:
         self.unit.rich_target = converted
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
 def set_target(self, target):
     """Set translation unit target."""
     try:
         converted = xliff_string_to_rich(target)
     except XMLSyntaxError:
         converted = target
     if self.template is not None:
         if self.parent.is_template:
             # Use source for monolingual files if editing template
             self.unit.rich_source = converted
             return
         if self.unit.source:
             # Update source if it is already set to keep it in sync
             self.unit.rich_source = self.template.source
     self.unit.rich_target = converted
Exemplo n.º 11
0
    def test_bidirectional_xliff_string(self):
        cases = [
            'foo <x id="INTERPOLATION" equiv-text="{{ angular }}"/> bar',
            "",
            "hello world",
            "hello <p>world</p>",
        ]

        for string in cases:
            rich = xliff_string_to_rich(string)
            self.assertIsInstance(rich, list)
            self.assertIsInstance(rich[0], StringElem)

            final_string = rich_to_xliff_string(rich)
            self.assertEqual(string, final_string)
Exemplo n.º 12
0
 def set_target(self, target):
     """Set translation unit target."""
     self._invalidate_target()
     try:
         converted = xliff_string_to_rich(target)
     except XMLSyntaxError:
         converted = [target]
     if self.template is not None:
         if self.parent.is_template:
             # Use source for monolingual files if editing template
             self.unit.rich_source = converted
         elif self.unit.source:
             # Update source to match current source
             self.unit.rich_source = self.template.rich_source
     # Always set target, even in monolingual template
     self.unit.rich_target = converted
Exemplo n.º 13
0
    def test_xliff_roundtrip_unknown(self):
        source = b'''<?xml version='1.0' encoding='UTF-8'?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
  <file datatype="xml" source-language="en-US" target-language="en-US" original="Translation Test">
    <body>
      <group id="body">
        <trans-unit id="1761676329" size-unit="char" translate="yes" xml:space="preserve">
          <source>T: <mrk mtype="protected">%s</mrk></source>
        </trans-unit>
      </group>
    </body>
  </file>
</xliff>
'''
        store = xlifffile.parsestring(source)
        string = rich_to_xliff_string(store.units[0].rich_source)
        self.assertEqual('T: <mrk mtype="protected">%s</mrk>', string)
        store.units[0].rich_source = xliff_string_to_rich(string)
        self.assertEqual(source, bytes(store))
Exemplo n.º 14
0
    def test_xliff_roundtrip_unknown(self):
        source = b'''<?xml version='1.0' encoding='UTF-8'?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
  <file datatype="xml" source-language="en-US" target-language="en-US" original="Translation Test">
    <body>
      <group id="body">
        <trans-unit id="1761676329" size-unit="char" translate="yes" xml:space="preserve">
          <source>T: <mrk mtype="protected">%s</mrk></source>
        </trans-unit>
      </group>
    </body>
  </file>
</xliff>
'''
        store = xlifffile.parsestring(source)
        string = rich_to_xliff_string(store.units[0].rich_source)
        self.assertEqual(
            'T: <mrk mtype="protected">%s</mrk>',
            string
        )
        store.units[0].rich_source = xliff_string_to_rich(string)
        self.assertEqual(source, bytes(store))
Exemplo n.º 15
0
 def set_target(self, target):
     """Set translation unit target."""
     self._invalidate_target()
     # Delete the empty target element
     if not target:
         xmlnode = self.unit.getlanguageNode(lang=None, index=1)
         if xmlnode is not None:
             xmlnode.getparent().remove(xmlnode)
         return
     try:
         converted = xliff_string_to_rich(target)
     except (XMLSyntaxError, KeyError):
         # KeyError happens on missing attribute
         converted = [target]
     if self.template is not None:
         if self.parent.is_template:
             # Use source for monolingual files if editing template
             self.unit.rich_source = converted
         elif self.unit.source:
             # Update source to match current source
             self.unit.rich_source = self.template.rich_source
     # Always set target, even in monolingual template
     self.unit.rich_target = converted
Exemplo n.º 16
0
 def set_target(self, target):
     """Set translation unit target."""
     self.unit.rich_target = xliff_string_to_rich(target)
Exemplo n.º 17
0
 def set_target(self, target):
     """Set translation unit target."""
     self.unit.rich_target = xliff_string_to_rich(target)