Skip to content
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

Keep space between marks #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/XliffReplacer/AbstractXliffReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ abstract class AbstractXliffReplacer {
'eq_word_count' => 0,
];

protected $mrkTagsMap = [];

/**
* AbstractXliffReplacer constructor.
*
Expand Down Expand Up @@ -296,13 +298,46 @@ protected function characterData( $parser, string $data ): void {
* postprocess escaped data and write to disk
*
* @param resource $fp
* @param string $data
* @param bool $treatAsCDATA
* @param string $data
* @param bool $treatAsCDATA
* @param bool $parseMarks
*/
protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) {
protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false, $parseMarks = false ) {
//postprocess string
$data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data );
$data = str_replace( ' ', ' ', $data );

// extract <mrk> map only for <seg-source> tag
if($parseMarks){
// check if there are spaces between <mrk> tags
preg_match_all('/<mrk \b[^>]*>(.*?)<\/mrk>(\s+)/', $data, $spacesBetweenMrkCheck);

if(!empty($spacesBetweenMrkCheck[0])){

// $spacesBetweenMrkCheck[0] // holds the complete tags
// $spacesBetweenMrkCheck[1] // holds the text
// $spacesBetweenMrkCheck[2] // holds the spaces

foreach ($spacesBetweenMrkCheck[0] as $index => $mrk){

if($this instanceof Xliff20){
preg_match('/id="(\d+)"/', $mrk, $markMatch);
} else {
preg_match('/mid="(\d+)"/', $mrk, $markMatch);
}

if(isset($markMatch[1])){

if(!isset($this->mrkTagsMap[$this->currentTransUnitId])){
$this->mrkTagsMap[$this->currentTransUnitId] = [];
}

$this->mrkTagsMap[$this->currentTransUnitId][$markMatch[1]] = $spacesBetweenMrkCheck[2][$index];
}
}
}
}

if ( !$treatAsCDATA ) {
//unix2dos
$data = str_replace( "\r\n", "\r", $data );
Expand Down
17 changes: 15 additions & 2 deletions src/XliffReplacer/Xliff12.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function tagClose( $parser, string $name ) {
$this->CDATABuffer = "";

//flush to the pointer
$this->postProcAndFlush( $this->outputFP, $tag );
$this->postProcAndFlush( $this->outputFP, $tag, false, $name === 'seg-source' );

} elseif ( $name === $this->tuTagName ) {

Expand Down Expand Up @@ -179,7 +179,7 @@ protected function tagClose( $parser, string $name ) {
$this->CDATABuffer = "";

//flush to the pointer
$this->postProcAndFlush( $this->outputFP, $tag );
$this->postProcAndFlush( $this->outputFP, $tag, false, $name === 'seg-source' );

} else {
//ok, nothing to be done; reset flag for next coming tag
Expand Down Expand Up @@ -228,10 +228,23 @@ protected function prepareTranslation( array $seg, string $transUnitTranslation
return $transUnitTranslation;
}

/**
* @param array $seg
* @param string $translation
* @return string
*/
protected function rebuildMarks( array $seg, string $translation ): string {

if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
$translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>";

// check if there is a trailing space in the map of this trans unit
if(
isset($this->mrkTagsMap[$this->currentTransUnitId]) and
isset($this->mrkTagsMap[$this->currentTransUnitId][$seg[ 'mrk_id' ]])
){
$translation .= $this->mrkTagsMap[$this->currentTransUnitId][$seg[ 'mrk_id' ]];
}
}

return $translation;
Expand Down
4 changes: 2 additions & 2 deletions src/XliffReplacer/Xliff20.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected function tagClose( $parser, string $name ) {
$this->CDATABuffer = "";

//flush to the pointer
$this->postProcAndFlush( $this->outputFP, $tag );
$this->postProcAndFlush( $this->outputFP, $tag, false, $name === 'source' );

} elseif ( 'segment' === $name ) {

Expand Down Expand Up @@ -278,7 +278,7 @@ protected function tagClose( $parser, string $name ) {
$this->CDATABuffer = "";

//flush to the pointer
$this->postProcAndFlush( $this->outputFP, $tag );
$this->postProcAndFlush( $this->outputFP, $tag, false, $name === 'source' );

} else {
//ok, nothing to be done; reset flag for next coming tag
Expand Down
73 changes: 67 additions & 6 deletions tests/XliffReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,13 @@ public function should_replace_12_units_with_empty_segments_with_the_correct_sta
$this->assertEquals( $status, $output[ 'files' ][ 1 ][ 'trans-units' ][ 3 ][ 'target' ][ 'attr' ][ 'state' ] );
}

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

}

/**
* @test
*/
Expand All @@ -1218,14 +1225,68 @@ public function should_replace_12_units_with_entities() {
$data = $this->getData( [
[
'sid' => '1',
'segment' => 'Hello&apos;&apos; ',
'internal_id' => '2973331',
'segment' => 'No more digging. If it’s top of mind, you’ll find it at the top of your inbox.',
'internal_id' => '2975931',
'mrk_id' => '0',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'Ciao&apos;&apos; ',
'translation' => 'This is the translation',
'status' => 'APPROVED',
'error' => '',
'eq_word_count' => '1.34',
'raw_word_count' => '2.00',
'source_page' => NULL,
'r2' => NULL,
'data_ref_map' => NULL,
],
[
'sid' => '1',
'segment' => 'No more digging. If it’s top of mind, you’ll find it at the top of your inbox.',
'internal_id' => '2975931',
'mrk_id' => '1',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'This is the second part of the translation',
'status' => 'APPROVED',
'error' => '',
'eq_word_count' => '1.34',
'raw_word_count' => '2.00',
'source_page' => NULL,
'r2' => NULL,
'data_ref_map' => NULL,
],
[
'sid' => '2',
'segment' => 'Your messages are all here with a new look and feel. See how your mailbox uses AI to save you time.',
'internal_id' => '2976344',
'mrk_id' => '0',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'This is the translation',
'status' => 'APPROVED',
'error' => '',
'eq_word_count' => '1.34',
'raw_word_count' => '2.00',
'source_page' => NULL,
'r2' => NULL,
'data_ref_map' => NULL,
],
[
'sid' => '2',
'segment' => 'Your messages are all here with a new look and feel. See how your mailbox uses AI to save you time.',
'internal_id' => '2976344',
'mrk_id' => '1',
'prev_tags' => '',
'succ_tags' => '',
'mrk_prev_tags' => NULL,
'mrk_succ_tags' => NULL,
'translation' => 'This is the second part of the translation',
'status' => 'APPROVED',
'error' => '',
'eq_word_count' => '1.34',
Expand All @@ -1236,13 +1297,13 @@ public function should_replace_12_units_with_entities() {
],
] );

$inputFile = __DIR__ . '/../tests/files/with-entities.xliff';
$outputFile = __DIR__ . '/../tests/files/output/with-entities.xliff';
$inputFile = __DIR__ . '/../tests/files/mrk-with-space.xliff';
$outputFile = __DIR__ . '/../tests/files/output/mrk-with-space.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' ] );
$this->assertEquals( "<mrk mid=\"0\" mtype=\"seg\">This is the translation</mrk> <mrk mid=\"1\" mtype=\"seg\">This is the second part of the translation</mrk>", $output[ 'files' ][ 1 ][ 'trans-units' ][ 1 ][ 'target' ][ 'raw-content' ] );
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/files/mrk-with-space.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:okp="okapi-framework:xliff-extensions" xmlns:yahoo="http://localizr.yahooapis.com/v1/localizrBase.xsd" version="1.2">
<file datatype="x-textunit_dto" original="unknown" source-language="en" target-language="fr-FR">
<header>
<count-group name="total">
<count count-type="total" unit="trans-unit">2</count>
<count count-type="total" unit="word">40</count>
<count count-type="total" unit="x-fuzzy-100">null</count>
<count count-type="total" unit="x-fuzzy-95">null</count>
<count count-type="total" unit="x-fuzzy-85">null</count>
<count count-type="total" unit="x-fuzzy-75">null</count>
<count count-type="total" unit="x-fuzzy-new">40</count>
</count-group>
</header>
<body>
<trans-unit id="2975931" resname="AYrjpi" xml:space="preserve">
<source xml:lang="en">No more digging. If it’s top of mind, you’ll find it at the top of your inbox.</source>
<seg-source><mrk mid="0" mtype="seg">No more digging.</mrk> <mrk mid="1" mtype="seg">If it’s top of mind, you’ll find it at the top of your inbox.</mrk></seg-source>
<target state="translated" state-qualifier="x-validation" xml:lang="fr-FR"><mrk mid="0" mtype="seg">No more digging.</mrk> <mrk mid="1" mtype="seg">If it’s top of mind, you’ll find it at the top of your inbox.</mrk> </target>
<note annotates="target">Reported in https://ouryahoo.atlassian.net/browse/MDF-3156, missing a space before/after a period.</note><note>Description for onboarding ai features modal priority step</note><note annotates="source">No more digging. If it’s top of mind, you’ll find it at the top of your inbox.</note>


<context-group purpose="location">
<context context-type="url"/>
</context-group>
</trans-unit>
<trans-unit id="2976344" resname="bNtCPN" xml:space="preserve">
<source xml:lang="en">Your messages are all here with a new look and feel. See how your mailbox uses AI to save you time.</source>
<seg-source><mrk mid="0" mtype="seg">Your messages are all here with a new look and feel.</mrk> <mrk mid="1" mtype="seg">See how your mailbox uses AI to save you time.</mrk></seg-source>
<target state="translated" state-qualifier="x-validation" xml:lang="fr-FR"><mrk mid="0" mtype="seg">Your messages are all here with a new look and feel.</mrk> <mrk mid="1" mtype="seg">See how your mailbox uses AI to save you time.</mrk> </target>
<note annotates="target">Reported in https://ouryahoo.atlassian.net/browse/MDF-3156, missing a space before/after a period.</note><note>Onboarding AI features card body</note><note annotates="source">Your messages are all here with a new look and feel. See how your mailbox uses AI to save you time.</note>


<context-group purpose="location">
<context context-type="url"/>
</context-group>
</trans-unit>
</body>
</file>
</xliff>