Skip to content

Commit

Permalink
Correct generation of <target> nodes with translate NO attribute (#95)
Browse files Browse the repository at this point in the history
* fix not translatable <target>

* wip

* Tests all green

* Xliff 2.0

* Refactoring
  • Loading branch information
mauretto78 authored Oct 3, 2024
1 parent f74067c commit a562ef4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/XliffReplacer/AbstractXliffReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ protected function setInBuffer( string $name ) {
if ( in_array( $name, $this->nodesToBuffer ) ) {
$this->bufferIsActive = true;
}

// We need bufferIsActive for <target> nodes with currentTransUnitIsTranslatable = 'NO'
// because in the other case, the target can be chunked into pieces by xml_set_character_data_handler()
// and this can potentially lead to a wrong string rebuild by postProcAndFlush function if the internal placeholders are split
if($name === 'target' and $this->currentTransUnitIsTranslatable === 'no'){
$this->bufferIsActive = true;
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/XliffReplacer/Xliff12.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ protected function tagClose( $parser, string $name ) {
*/
if ( !$this->isEmpty ) {

if ( !$this->inTarget ) {
// write closing tag if is not a target
// EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO'
if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) {
$tag = "</$name>";
}

Expand All @@ -120,6 +122,12 @@ protected function tagClose( $parser, string $name ) {
// actually there may be more than one segment to that ID if there are two mrk of the same source segment
$tag = $this->rebuildTarget();

} elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) {

// These are target nodes with currentTransUnitIsTranslatable = 'NO'
$this->bufferIsActive = false;
$tag = $this->CDATABuffer . "</$name>";
$this->CDATABuffer = "";
}

$this->targetWasWritten = true;
Expand Down
10 changes: 9 additions & 1 deletion src/XliffReplacer/Xliff20.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ protected function tagClose( $parser, string $name ) {
*/
if ( !$this->isEmpty ) {

if ( !$this->inTarget ) {
// write closing tag if is not a target
// EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO'
if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) {
$tag = "</$name>";
}

Expand Down Expand Up @@ -195,6 +197,12 @@ protected function tagClose( $parser, string $name ) {
//append translation
$tag = "<target>$translation</target>";

} elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) {

// These are target nodes with currentTransUnitIsTranslatable = 'NO'
$this->bufferIsActive = false;
$tag = $this->CDATABuffer . "</$name>";
$this->CDATABuffer = "";
}

// signal we are leaving a target
Expand Down
43 changes: 43 additions & 0 deletions tests/XliffReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,49 @@ public function should_replace_12_units_with_entities() {

$this->assertEquals( "<mrk mid=\"0\" mtype=\"seg\">Ciao''</mrk> ", $output[ 'files' ][ 1 ][ 'trans-units' ][ 1 ][ 'target' ][ 'raw-content' ] );
}

/**
* @test
*/
public function other_tests_replacing_12_units_with_entities() {

$data = $this->getData( [
[
'sid' => '1',
'segment' => 'Hello&apos;&apos; ',
'internal_id' => '3142672',
'mrk_id' => '0',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'با دقت به کد زیر نگاه کنید. با کلیک روی «اجرا»، این برنامه کدام طراحی را انجام می‌دهد؟',
'status' => 'APPROVED',
'error' => '',
'eq_word_count' => '1.34',
'raw_word_count' => '2.00',
'source_page' => NULL,
'r2' => NULL,
'data_ref_map' => NULL,
],
] );

$inputFile = __DIR__ . '/../tests/files/entities.xliff';
$outputFile = __DIR__ . '/../tests/files/output/entities.xliff';

( new XliffParser() )->replaceTranslation( $inputFile, $data[ 'data' ], $data[ 'transUnits' ], 'it-it', $outputFile, false );
$output = ( new XliffParser() )->xliffToArray( file_get_contents( $outputFile ) );

$expected = '&lt;table&gt;
&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;&lt;img src="https://images.code.org/cfc3f8206438a60afe3be9afe7fc0a22-image-1489118742610.10.15.png" width="100px" style="mix-blend-mode: multiply;"/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;&lt;img src="https://images.code.org/975b027684d2f5411b960bf82987663e-image-1489119999013.11.13.png" width="100px" style="mix-blend-mode: multiply;"/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;td&gt;&lt;img src="https://images.code.org/635ac54ed7cb2e2d24eb341b3ec4eecb-image-1489120024059.12.00.png" width="80px" style="mix-blend-mode: multiply; clip: rect(0px,0px,0px,40px);"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;br/&gt;&lt;br/&gt;
';

$this->assertEquals( $expected, $output[ 'files' ][ 1 ][ 'trans-units' ][ 1 ][ 'target' ][ 'raw-content' ] );
}
}

class DummyXliffReplacerCallbackWhichReturnFalse implements XliffReplacerCallbackInterface {
Expand Down
36 changes: 36 additions & 0 deletions tests/files/entities.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file id="179174" original="/course_content/2022/coursee-2022.json" source-language="en-US" target-language="fa" datatype="plaintext" project-id="26074" tool-id="crowdin">
<header>
<tool tool-id="crowdin" tool-name="Crowdin" tool-version="1.1"/>
</header>
<body>
<trans-unit id="3142670" resname="&quot;https://studio.code.org/s/coursee-2022/lessons/7/levels/7&quot;.sublevels.courseD_artist_nestedLoops9_predict12022.contained levels.0.dsls.markdown" translate="no" approved="yes">
<source>&lt;table&gt;
&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/cfc3f8206438a60afe3be9afe7fc0a22-image-1489118742610.10.15.png&quot; width=&quot;100px&quot; style=&quot;mix-blend-mode: multiply;&quot;/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/975b027684d2f5411b960bf82987663e-image-1489119999013.11.13.png&quot; width=&quot;100px&quot; style=&quot;mix-blend-mode: multiply;&quot;/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/635ac54ed7cb2e2d24eb341b3ec4eecb-image-1489120024059.12.00.png&quot; width=&quot;80px&quot; style=&quot;mix-blend-mode: multiply; clip: rect(0px,0px,0px,40px);&quot;/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;br/&gt;&lt;br/&gt;

</source>
<target state="final">&lt;table&gt;
&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/cfc3f8206438a60afe3be9afe7fc0a22-image-1489118742610.10.15.png&quot; width=&quot;100px&quot; style=&quot;mix-blend-mode: multiply;&quot;/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/975b027684d2f5411b960bf82987663e-image-1489119999013.11.13.png&quot; width=&quot;100px&quot; style=&quot;mix-blend-mode: multiply;&quot;/&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;td&gt;&lt;img src=&quot;https://images.code.org/635ac54ed7cb2e2d24eb341b3ec4eecb-image-1489120024059.12.00.png&quot; width=&quot;80px&quot; style=&quot;mix-blend-mode: multiply; clip: rect(0px,0px,0px,40px);&quot;/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;br/&gt;&lt;br/&gt;

</target>
<context-group purpose="information">
<context context-type="source">https://studio.code.org/s/coursee-2022/lessons/7/levels/7 -&gt; sublevels -&gt; courseD_artist_nestedLoops9_predict12022 -&gt; contained levels -&gt; 0 -&gt; dsls -&gt; markdown</context>
</context-group>
</trans-unit>
<trans-unit id="3142672" resname="&quot;https://studio.code.org/s/coursee-2022/lessons/7/levels/7&quot;.sublevels.courseD_artist_nestedLoops9_predict12022.contained levels.0.dsls.questions.0.text" approved="yes">
<source>Take a good look at the code below. Which drawing will this program make when you click &quot;Run&quot;?</source>
<target state="final">با دقت به کد زیر نگاه کنید. با کلیک روی «اجرا»، این برنامه کدام طراحی را انجام می‌دهد؟</target>
<context-group purpose="information">
<context context-type="source">https://studio.code.org/s/coursee-2022/lessons/7/levels/7 -&gt; sublevels -&gt; courseD_artist_nestedLoops9_predict12022 -&gt; contained levels -&gt; 0 -&gt; dsls -&gt; questions -&gt; 0 -&gt; text</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit a562ef4

Please sign in to comment.