Skip to content

Commit

Permalink
Xliff translation state support
Browse files Browse the repository at this point in the history
  • Loading branch information
mauretto78 committed Oct 6, 2023
1 parent 1432ddb commit cdb1181
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/XliffParser/XliffParserV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniqueness
$segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ];
if ( isset( $targetRawContent ) && !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) {
$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $targetRawContent );
$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ]['attr'] = $this->extractTagAttributes($childNode);
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/XliffReplacer/XliffSAXTranslationReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ protected function tagOpen( $parser, $name, $attr ) {
$tag .= "$k=\"$this->targetLang\" ";
} else {
$pos = 0;
if ( $this->currentTransUnitId ) {
if ( $this->currentTransUnitId and isset($this->transUnits[ $this->currentTransUnitId ])) {
$pos = current( $this->transUnits[ $this->currentTransUnitId ] );
}

if ( $name === $this->tuTagName ) {
if ( $name === $this->tuTagName and isset($this->segments[ $pos ]) and isset($this->segments[ $pos ][ 'sid' ]) ) {

$sid = $this->segments[ $pos ][ 'sid' ];

Expand All @@ -209,6 +209,8 @@ protected function tagOpen( $parser, $name, $attr ) {

} elseif ( 'segment' === $name && $this->xliffVersion === 2 ) { // add state to segment in Xliff v2
list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $this->segments[ $pos ], $stateProp, $lastMrkState );
} elseif ( 'target' === $name && $this->xliffVersion === 1 ) { // add state to target in Xliff v1
list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $this->segments[ $pos ], $stateProp, $lastMrkState );
}

//normal tag flux, put attributes in it
Expand Down Expand Up @@ -629,8 +631,9 @@ protected function prepareTranslation( $seg, $transUnitTranslation = "" ) {
if ( is_null( $seg [ 'translation' ] ) || $seg [ 'translation' ] == '' ) {
$translation = $segment;
} else {
if ( $this->callback ) {
if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap ) ) {
if ( $this->callback instanceof XliffReplacerCallbackInterface ) {
$error = (isset($seg['error'])) ? $seg['error'] : null;
if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) {
$translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||';
}
}
Expand Down Expand Up @@ -760,7 +763,13 @@ private function setTransUnitState( $seg, $state_prop, $lastMrkState ) {
case TranslationStatus::STATUS_FIXED:
case TranslationStatus::STATUS_APPROVED:
if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) {
$state_prop = ( $this->xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"";

if( isset($seg[ 'r2' ]) and $seg[ 'r2' ] == 1 ){
$state_prop = "state=\"final\"";
} else {
$state_prop = ( $this->xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"";
}

$lastMrkState = TranslationStatus::STATUS_APPROVED;
}
break;
Expand All @@ -781,16 +790,19 @@ private function setTransUnitState( $seg, $state_prop, $lastMrkState ) {
break;

case TranslationStatus::STATUS_NEW:
if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) {
if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_NEW ) {
$state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
$lastMrkState = TranslationStatus::STATUS_NEW;
}
break;

case TranslationStatus::STATUS_DRAFT:
$state_prop = "state=\"needs-translation\"";
$lastMrkState = TranslationStatus::STATUS_DRAFT;
if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) {
$state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
$lastMrkState = TranslationStatus::STATUS_DRAFT;
}
break;

default:
// this is the case when a segment is not showed in cattool, so the row in
// segment_translations does not exists and
Expand Down
40 changes: 37 additions & 3 deletions tests/XliffParserV1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,24 @@ public function can_parse_xliff_v1_tu_with_extenal_tags_in_seg_source_and_target

$segSource = $parsed[ 'files' ][ 3 ]['trans-units'][ 1 ][ 'seg-source' ][ 0 ];
$segTarget = $parsed[ 'files' ][ 3 ]['trans-units'][ 1 ][ 'seg-target' ][ 0 ];
$expected = [
$expected1 = [
'mid' => 0,
'ext-prec-tags' => '<g id="1">',
'raw-content' => 'An English string with g tags',
'ext-succ-tags' => '</g>',
];
$expected2 = [
'mid' => 0,
'ext-prec-tags' => '<g id="1">',
'raw-content' => 'An English string with g tags',
'ext-succ-tags' => '</g>',
'attr' => [
'xml:lang' => 'fr-fr'
]
];

$this->assertEquals($expected, $segSource);
$this->assertEquals($expected, $segTarget);
$this->assertEquals($expected1, $segSource);
$this->assertEquals($expected2, $segTarget);
}

/**
Expand Down Expand Up @@ -604,4 +613,29 @@ public function can_extract_custom_file_attributes()
$this->assertEquals($parsed['files'][3]['trans-units'][1]['notes'][1]['raw-content'], 'page1.txt');
$this->assertEquals($parsed['files'][3]['trans-units'][1]['notes'][2]['raw-content'], 'questa è una nota1');
}

/**
* @test
*/
public function can_parse_segment_state_attribute()
{
$states = [
'new',
'needs-translation',
'needs-adaptation',
'needs-l10n',
'needs-review-translation',
'needs-review-adaptation',
'needs-review-l10n',
'translated',
'signed-off',
'final',
];

$parsed = (new XliffParser())->xliffToArray($this->getTestFile('xliff12-with-segment-state.xliff'));

for($i = 1; $i <= count($parsed['files'][1]['trans-units']); $i++){
$this->assertEquals($parsed['files'][1]['trans-units'][$i]['seg-target'][0]['attr']['state'], $states[$i-1]);
}
}
}
Loading

0 comments on commit cdb1181

Please sign in to comment.