Skip to content

Commit

Permalink
Sdlxliff entities (#93)
Browse files Browse the repository at this point in the history
* Sdlxliff entities

* Stop adding <count-group> in sdlxliff files
  • Loading branch information
mauretto78 authored Sep 5, 2024
1 parent 4e66a6b commit 80b17ae
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/XliffReplacer/Xliff12.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected function rebuildMarks( array $seg, string $translation ): string {
*
* @return string
*/
private function createTargetTag( string $translation, string $stateProp ): string {
protected function createTargetTag( string $translation, string $stateProp ): string {
$targetLang = ' xml:lang="' . $this->targetLang . '"';
$tag = "<target $targetLang $stateProp>$translation</target>";
$tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts[ 'raw_word_count' ] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts[ 'eq_word_count' ] . '</count></count-group>';
Expand Down
25 changes: 25 additions & 0 deletions src/XliffReplacer/XliffSdl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@

class XliffSdl extends Xliff12 {

/**
* @var array
*/
protected array $nodesToBuffer = [
'source',
'seg-source',
'note',
'context-group',
'tag-defs',
'cxt-defs'
];

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -100,4 +112,17 @@ protected function rebuildMarks( array $seg, string $translation ): string {

}

/**
* This function creates a <target>
*
* @param string $translation
* @param string $stateProp
*
* @return string
*/
protected function createTargetTag( string $translation, string $stateProp ): string {
$targetLang = ' xml:lang="' . $this->targetLang . '"';

return "<target $targetLang $stateProp>$translation</target>";
}
}
35 changes: 35 additions & 0 deletions tests/XliffReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,41 @@ public function should_replace_12_units_with_empty_segments_with_the_correct_sta
$this->assertEquals( $status, $output[ 'files' ][ 1 ][ 'trans-units' ][ 2 ][ 'target' ][ 'attr' ][ 'state' ] );
$this->assertEquals( $status, $output[ 'files' ][ 1 ][ 'trans-units' ][ 3 ][ 'target' ][ 'attr' ][ 'state' ] );
}

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

$data = $this->getData( [
[
'sid' => '1',
'segment' => 'Hello&apos;&apos; ',
'internal_id' => '2973331',
'mrk_id' => '0',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'Ciao&apos;&apos; ',
'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/with-entities.xliff';
$outputFile = __DIR__ . '/../tests/files/output/with-entities.xliff';

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

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

class DummyXliffReplacerCallbackWhichReturnFalse implements XliffReplacerCallbackInterface {
Expand Down
61 changes: 61 additions & 0 deletions tests/files/with-entities.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:sdl="http://sdl.com/FileTypes/SdlXliff/1.0" xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2" sdl:version="1.0">
<file datatype="x-textunit_dto" original="unknown" source-language="en" target-language="fr-FR">
<header>
<file-info
xmlns="http://sdl.com/FileTypes/SdlXliff/1.0">
<value key="SDL:FileId">2bc03c8e-0d02-49e7-8788-f3e94f511c00</value>
<value key="SDL:CreationDate">06/04/2024 15:25:58</value>
<value key="SDL:OriginalFilePath">C:\Users\StefanieVeldhuis\AppData\Local\Temp\SDL_Wizard_Project_cb33f86e-d120-4cb5-af9f-9ca3532362fd\en-GB\M4_output.json</value>
<value key="SDL:OriginalEncoding">utf-8</value>
<value key="EscapeUnicode">False</value>
<value key="SetPrettyPrinting">True</value>
<value key="Utf8Bom">DontAddIfNotPresent</value>
<value key="SDL_SUBCONTENT_STREAM">true</value>
<value key="ParagraphTextDirections"></value>
<sniff-info>
<detected-encoding detection-level="Likely" encoding="utf-8"/>
<detected-source-lang detection-level="Guess" lang="en-GB"/>
</sniff-info>
</file-info>
<sdl:filetype-info>
<sdl:filetype-id>JSON v 1.0.0.0</sdl:filetype-id>
</sdl:filetype-info>
<cxt-defs
xmlns="http://sdl.com/FileTypes/SdlXliff/1.0">
<cxt-def id="1" type="StructureParagraphUnit">
<props>
<value key="StructureParagraphPath">[239].Text</value>
</props>
</cxt-def>
</cxt-defs>
<tag-defs
xmlns="http://sdl.com/FileTypes/SdlXliff/1.0">
<tag id="4407">
<st>#SDL_SUBCONTENT_BOUNDARY#</st>
</tag>
<tag id="4409">
<st>#SDL_SUBCONTENT_BOUNDARY#</st>
</tag>
<tag id="4410">
<st>#SDL-SUBCONTENT-MARKER#</st>
</tag>
<tag id="4411">
<st>&apos;&apos;I cannot compensate you, as it is Colgate-Palmolive policy not to compensate Customers for margin loss. Our Customers are free to sell Colgate-Palmolive products at any price they choose. Our Customers are responsible for their own profitability.&apos;&apos; </st>
</tag>
</tag-defs>
</header>
<body>
<trans-unit id="2973331" resname="creators.PAGE_TITLE" xml:space="preserve">
<source xml:lang="en">Hello&apos;&apos; </source>
<seg-source>
<mrk mid="0" mtype="seg">Hello&apos;&apos; </mrk>
</seg-source>
<target state="new" xml:lang="it-IT">
<mrk mid="0" mtype="seg">Ciao&apos;&apos; </mrk>
</target>
<note>Silly note</note>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 80b17ae

Please sign in to comment.