XLIFF 2.0: Writing CDATA

5 views
Skip to first unread message

Masha

unread,
Oct 18, 2022, 6:12:32 AM10/18/22
to okapi-users
Hi there! 

I am trying to write CDATA for XLIFF 2.0 using okapi-lib-xliff2:1.44.0, but it ends up being escaped in output .xlf. I need it specifically for note and meta elements, but it does nto work for source and target as well. This makes me wonder if there is anything I am missing to configure the writer or pass any parameter. This is my code snippet:

<code>
try (XLIFFWriter writer = new XLIFFWriter()) {
      writer.setUseIndentation(true);
      writer.create(
          new File("cdata.xlf"),
          Locale.US.toString(),
          Locale.FRANCE.toString());

      StartFileData fileElementAttribute = new StartFileData(null);
      String originalFile = "with_cdata.xlf";
      fileElementAttribute.setId("1");
      fileElementAttribute.setOriginal(originalFile);
      writer.writeStartFile(fileElementAttribute);

      Unit unit = new Unit("1");

      ExtAttributes additionalAttributes = new ExtAttributes();
      additionalAttributes.setAttribute(new ExtAttribute(QName.valueOf("xml:space"), "preserve"));
      unit.setExtAttributes(additionalAttributes);

      String segmentId = "test-key-1";
      unit.setName(segmentId);
      unit.setCanResegment(false);

      Segment segment = unit.appendSegment();
      segment.setCanResegment(false);
      segment.setSource(new CDATAEncoder("UTF-8", "\\n").encode("<b>Hello<\\b>", EncoderContext.TEXT));
      segment.setTarget(new CDATAEncoder("UTF-8", "\\n").encode("<b>Bonjour<\\b>", EncoderContext.TEXT));

      Note originalComment = new Note();
      originalComment.setCategory("engineer-comment");
      originalComment.setText(new CDATAEncoder("UTF-8", "\\n").encode("This is translation for <b>Hello<\\b>", EncoderContext.TEXT));
      unit.addNote(originalComment);

      Metadata unitMetadata = new Metadata();
      MetaGroup metaGroup = new MetaGroup();
      metaGroup.setCategory("unitMetadata");
      Meta meta = new Meta("key-1");
      meta.setData(new CDATAEncoder("UTF-8", "\\n").encode("This is translation for <b>Hello<\\b>", EncoderContext.TEXT));
      metaGroup.add(meta);
      unitMetadata.addGroup(metaGroup);

      unit.setMetadata(unitMetadata);

      writer.writeUnit(unit);
    }

</code>

Output is as follows:
<code>
<?xml version="1.0"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en_US" trgLang="fr_FR">
 <file id="1" original="with_cdata.xlf">
  <unit id="1" canResegment="no" name="test-key-1" xml:space="preserve">
   <mda:metadata xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0">
   <mda:metaGroup category="unitMetadata">
   <mda:meta type="key-1">&lt;![CDATA[This is translation for &lt;b>Hello&lt;\b>]]></mda:meta>
   </mda:metaGroup>
</mda:metadata>
   <notes>
    <note category="engineer-comment">&lt;![CDATA[This is translation for &lt;b>Hello&lt;\b>]]></note>
   </notes>
   <segment>
    <source>&lt;![CDATA[&lt;b&gt;Hello&lt;\b&gt;]]&gt;</source>
    <target>&lt;![CDATA[&lt;b&gt;Bonjour&lt;\b&gt;]]&gt;</target>
   </segment>
  </unit>
 </file>
</xliff></code>

Expected output:
<code>
<?xml version="1.0"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en_US" trgLang="fr_FR">
 <file id="1" original="with_cdata.xlf">
  <unit id="1" canResegment="no" name="test-key-1" xml:space="preserve">
   <mda:metadata xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0">
   <mda:metaGroup category="unitMetadata">
   <mda:meta type="key-1"><![CDATA[This is translation for <b>Hello<\b>]]></mda:meta>
   </mda:metaGroup>
</mda:metadata>
   <notes>
    <note category="engineer-comment"><![CDATA[This is translation for <b>Hello<\b>]]></note>
   </notes>
   <segment>
    <source><![CDATA[<b>Hello<\b>]]></source>
    <target><![CDATA[<b>Bonjour<\b>]]></target>
   </segment>
  </unit>
 </file>
</xliff></code>

Thanks a lot for the help! 

Reply all
Reply to author
Forward
0 new messages