matecat /
xliff-parser
| 1 | <?php |
|||||||||||
| 2 | /** |
|||||||||||
| 3 | * Created by PhpStorm. |
|||||||||||
| 4 | * @author hashashiyyin [email protected] / [email protected] |
|||||||||||
| 5 | * Date: 02/08/24 |
|||||||||||
| 6 | * Time: 11:45 |
|||||||||||
| 7 | * |
|||||||||||
| 8 | */ |
|||||||||||
| 9 | ||||||||||||
| 10 | namespace Matecat\XliffParser\XliffReplacer; |
|||||||||||
| 11 | ||||||||||||
| 12 | use Matecat\XliffParser\Utils\Strings; |
|||||||||||
| 13 | ||||||||||||
| 14 | class Xliff12 extends AbstractXliffReplacer { |
|||||||||||
| 15 | ||||||||||||
| 16 | /** |
|||||||||||
| 17 | * @var array |
|||||||||||
| 18 | */ |
|||||||||||
| 19 | protected array $nodesToBuffer = [ |
|||||||||||
| 20 | 'source', |
|||||||||||
| 21 | 'seg-source', |
|||||||||||
| 22 | 'note', |
|||||||||||
| 23 | 'context-group' |
|||||||||||
| 24 | ]; |
|||||||||||
| 25 | ||||||||||||
| 26 | /** |
|||||||||||
| 27 | * @var string |
|||||||||||
| 28 | */ |
|||||||||||
| 29 | protected string $tuTagName = 'trans-unit'; |
|||||||||||
| 30 | ||||||||||||
| 31 | /** |
|||||||||||
| 32 | * @var string |
|||||||||||
| 33 | */ |
|||||||||||
| 34 | protected string $alternativeMatchesTag = 'alt-trans'; |
|||||||||||
| 35 | ||||||||||||
| 36 | /** |
|||||||||||
| 37 | * @var string |
|||||||||||
| 38 | */ |
|||||||||||
| 39 | protected string $namespace = "mtc"; // Custom namespace |
|||||||||||
| 40 | ||||||||||||
| 41 | /** |
|||||||||||
| 42 | * @inheritDoc |
|||||||||||
| 43 | */ |
|||||||||||
| 44 | protected function tagOpen( $parser, string $name, array $attr ) { |
|||||||||||
| 45 | ||||||||||||
| 46 | $this->handleOpenUnit( $name, $attr ); |
|||||||||||
| 47 | ||||||||||||
| 48 | $this->trySetAltTrans( $name );; |
|||||||||||
| 49 | $this->checkSetInTarget( $name ); |
|||||||||||
| 50 | ||||||||||||
| 51 | // open buffer |
|||||||||||
| 52 | $this->setInBuffer( $name ); |
|||||||||||
| 53 | ||||||||||||
| 54 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
|||||||||||
| 55 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
|||||||||||
| 56 | if ( !$this->inTarget ) { |
|||||||||||
| 57 | ||||||||||||
| 58 | $tag = ''; |
|||||||||||
| 59 | ||||||||||||
| 60 | // construct tag |
|||||||||||
| 61 | $tag .= "<$name "; |
|||||||||||
| 62 | ||||||||||||
| 63 | foreach ( $attr as $k => $v ) { |
|||||||||||
| 64 | ||||||||||||
| 65 | //if tag name is file, we must replace the target-language attribute |
|||||||||||
| 66 | if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
|||||||||||
| 67 | //replace Target language with job language provided from constructor |
|||||||||||
| 68 | $tag .= "$k=\"$this->targetLang\" "; |
|||||||||||
| 69 | } else { |
|||||||||||
| 70 | $tag .= "$k=\"$v\" "; |
|||||||||||
| 71 | } |
|||||||||||
| 72 | ||||||||||||
| 73 | } |
|||||||||||
| 74 | ||||||||||||
| 75 | $seg = $this->getCurrentSegment(); |
|||||||||||
| 76 | ||||||||||||
| 77 | if ( $name === $this->tuTagName && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
|||||||||||
| 78 | ||||||||||||
| 79 | // add `help-id` to xliff v.1* |
|||||||||||
| 80 | if ( strpos( $tag, 'help-id' ) === false ) { |
|||||||||||
| 81 | if ( !empty( $seg[ 'sid' ] ) ) { |
|||||||||||
| 82 | $tag .= "help-id=\"{$seg[ 'sid' ]}\" "; |
|||||||||||
| 83 | } |
|||||||||||
| 84 | } |
|||||||||||
| 85 | ||||||||||||
| 86 | } |
|||||||||||
| 87 | ||||||||||||
| 88 | $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
|||||||||||
| 89 | ||||||||||||
| 90 | $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|||||||||||
| 91 | ||||||||||||
| 92 | } |
|||||||||||
| 93 | ||||||||||||
| 94 | } |
|||||||||||
| 95 | ||||||||||||
| 96 | ||||||||||||
| 97 | /** |
|||||||||||
| 98 | * @inheritDoc |
|||||||||||
| 99 | */ |
|||||||||||
| 100 | protected function tagClose( $parser, string $name ) { |
|||||||||||
| 101 | $tag = ''; |
|||||||||||
| 102 | ||||||||||||
| 103 | /** |
|||||||||||
| 104 | * if is a tag within <target> or |
|||||||||||
| 105 | * if it is an empty tag, do not add closing tag because we have already closed it in |
|||||||||||
| 106 | * |
|||||||||||
| 107 | * self::tagOpen method |
|||||||||||
| 108 | */ |
|||||||||||
| 109 | if ( !$this->isEmpty ) { |
|||||||||||
| 110 | ||||||||||||
| 111 | // write closing tag if is not a target |
|||||||||||
| 112 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
|||||||||||
| 113 | if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
|||||||||||
|
0 ignored issues
–
show
|
||||||||||||
| 114 | $tag = "</$name>"; |
|||||||||||
| 115 | } |
|||||||||||
| 116 | ||||||||||||
| 117 | if ( 'target' == $name && !$this->inAltTrans ) { |
|||||||||||
| 118 | ||||||||||||
| 119 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|||||||||||
| 120 | ||||||||||||
| 121 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
|||||||||||
| 122 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
|||||||||||
| 123 | $tag = $this->rebuildTarget(); |
|||||||||||
| 124 | ||||||||||||
| 125 | } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
|||||||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
and instead of && is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. Loading history...
|
||||||||||||
| 126 | ||||||||||||
| 127 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
|||||||||||
| 128 | $this->bufferIsActive = false; |
|||||||||||
| 129 | $tag = $this->CDATABuffer . "</$name>"; |
|||||||||||
| 130 | $this->CDATABuffer = ""; |
|||||||||||
| 131 | } |
|||||||||||
| 132 | ||||||||||||
| 133 | $this->targetWasWritten = true; |
|||||||||||
| 134 | // signal we are leaving a target |
|||||||||||
| 135 | $this->inTarget = false; |
|||||||||||
| 136 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
|||||||||||
| 137 | ||||||||||||
| 138 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
|||||||||||
| 139 | ||||||||||||
| 140 | $this->bufferIsActive = false; |
|||||||||||
| 141 | $tag = $this->CDATABuffer . "</$name>"; |
|||||||||||
| 142 | $this->CDATABuffer = ""; |
|||||||||||
| 143 | ||||||||||||
| 144 | //flush to the pointer |
|||||||||||
| 145 | $this->postProcAndFlush( $this->outputFP, $tag ); |
|||||||||||
| 146 | ||||||||||||
| 147 | } elseif ( $name === $this->tuTagName ) { |
|||||||||||
| 148 | ||||||||||||
| 149 | $tag = ""; |
|||||||||||
| 150 | ||||||||||||
| 151 | // handling </trans-unit> closure |
|||||||||||
| 152 | if ( !$this->targetWasWritten ) { |
|||||||||||
| 153 | ||||||||||||
| 154 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|||||||||||
| 155 | $tag = $this->rebuildTarget(); |
|||||||||||
| 156 | } else { |
|||||||||||
| 157 | $tag = $this->createTargetTag( "", "" ); |
|||||||||||
| 158 | } |
|||||||||||
| 159 | ||||||||||||
| 160 | } |
|||||||||||
| 161 | ||||||||||||
| 162 | $tag .= "</$this->tuTagName>"; |
|||||||||||
| 163 | $this->targetWasWritten = false; |
|||||||||||
| 164 | $this->postProcAndFlush( $this->outputFP, $tag ); |
|||||||||||
| 165 | ||||||||||||
| 166 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|||||||||||
| 167 | $this->CDATABuffer .= "</$name>"; |
|||||||||||
| 168 | // Do NOT Flush |
|||||||||||
| 169 | } else { //generic tag closure do Nothing |
|||||||||||
| 170 | // flush to pointer |
|||||||||||
| 171 | $this->postProcAndFlush( $this->outputFP, $tag ); |
|||||||||||
| 172 | } |
|||||||||||
| 173 | ||||||||||||
| 174 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
|||||||||||
| 175 | ||||||||||||
| 176 | $this->isEmpty = false; |
|||||||||||
| 177 | $this->bufferIsActive = false; |
|||||||||||
| 178 | $tag = $this->CDATABuffer; |
|||||||||||
| 179 | $this->CDATABuffer = ""; |
|||||||||||
| 180 | ||||||||||||
| 181 | //flush to the pointer |
|||||||||||
| 182 | $this->postProcAndFlush( $this->outputFP, $tag ); |
|||||||||||
| 183 | ||||||||||||
| 184 | } else { |
|||||||||||
| 185 | //ok, nothing to be done; reset flag for next coming tag |
|||||||||||
| 186 | $this->isEmpty = false; |
|||||||||||
| 187 | } |
|||||||||||
| 188 | ||||||||||||
| 189 | // try to signal that we are leaving a target |
|||||||||||
| 190 | $this->tryUnsetAltTrans( $name ); |
|||||||||||
| 191 | ||||||||||||
| 192 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
|||||||||||
| 193 | if ( $this->tuTagName === $name ) { |
|||||||||||
| 194 | $this->currentTransUnitIsTranslatable = null; |
|||||||||||
| 195 | $this->inTU = false; |
|||||||||||
| 196 | $this->hasWrittenCounts = false; |
|||||||||||
| 197 | ||||||||||||
| 198 | $this->resetCounts(); |
|||||||||||
| 199 | } |
|||||||||||
| 200 | } |
|||||||||||
| 201 | ||||||||||||
| 202 | /** |
|||||||||||
| 203 | * prepare segment tagging for xliff insertion |
|||||||||||
| 204 | * |
|||||||||||
| 205 | * @param array $seg |
|||||||||||
| 206 | * @param string $transUnitTranslation |
|||||||||||
| 207 | * |
|||||||||||
| 208 | * @return string |
|||||||||||
| 209 | */ |
|||||||||||
| 210 | protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
|||||||||||
| 211 | ||||||||||||
| 212 | $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|||||||||||
| 213 | $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|||||||||||
| 214 | ||||||||||||
| 215 | if ( $seg [ 'translation' ] == '' ) { |
|||||||||||
| 216 | $translation = $segment; |
|||||||||||
| 217 | } else { |
|||||||||||
| 218 | if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|||||||||||
| 219 | $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
|||||||||||
| 220 | if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
|||||||||||
| 221 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
|||||||||||
| 222 | } |
|||||||||||
| 223 | } |
|||||||||||
| 224 | } |
|||||||||||
| 225 | ||||||||||||
| 226 | $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
|||||||||||
| 227 | ||||||||||||
| 228 | return $transUnitTranslation; |
|||||||||||
| 229 | } |
|||||||||||
| 230 | ||||||||||||
| 231 | protected function rebuildMarks( array $seg, string $translation ): string { |
|||||||||||
| 232 | ||||||||||||
| 233 | if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|||||||||||
| 234 | $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
|||||||||||
| 235 | } |
|||||||||||
| 236 | ||||||||||||
| 237 | return $translation; |
|||||||||||
| 238 | ||||||||||||
| 239 | } |
|||||||||||
| 240 | ||||||||||||
| 241 | /** |
|||||||||||
| 242 | * This function creates a <target> |
|||||||||||
| 243 | * |
|||||||||||
| 244 | * @param string $translation |
|||||||||||
| 245 | * @param string $stateProp |
|||||||||||
| 246 | * |
|||||||||||
| 247 | * @return string |
|||||||||||
| 248 | */ |
|||||||||||
| 249 | protected function createTargetTag( string $translation, string $stateProp ): string { |
|||||||||||
| 250 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
|||||||||||
| 251 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
|||||||||||
| 252 | $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>'; |
|||||||||||
| 253 | ||||||||||||
| 254 | return $tag; |
|||||||||||
| 255 | ||||||||||||
| 256 | } |
|||||||||||
| 257 | ||||||||||||
| 258 | protected function rebuildTarget(): string { |
|||||||||||
| 259 | ||||||||||||
| 260 | // init translation and state |
|||||||||||
| 261 | $translation = ''; |
|||||||||||
| 262 | $lastMrkState = null; |
|||||||||||
| 263 | $stateProp = ''; |
|||||||||||
| 264 | ||||||||||||
| 265 | // we must reset the lastMrkId found because this is a new segment. |
|||||||||||
| 266 | $lastMrkId = -1; |
|||||||||||
| 267 | ||||||||||||
| 268 | foreach ( $this->lastTransUnit as $pos => $seg ) { |
|||||||||||
| 269 | ||||||||||||
| 270 | /* |
|||||||||||
| 271 | * This routine works to respect the positional orders of markers. |
|||||||||||
| 272 | * In every cycle we check if the mrk of the segment is below or equal the last one. |
|||||||||||
| 273 | * When this is true, means that the mrk id belongs to the next segment with the same internal_id |
|||||||||||
| 274 | * so we MUST stop to apply markers and translations |
|||||||||||
| 275 | * and stop to add eq_word_count |
|||||||||||
| 276 | * |
|||||||||||
| 277 | * Begin: |
|||||||||||
| 278 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
|||||||||||
| 279 | * If it is null leave it NULL |
|||||||||||
| 280 | */ |
|||||||||||
| 281 | if ( (int)$seg[ "mrk_id" ] < 0 && $seg[ "mrk_id" ] !== null ) { |
|||||||||||
| 282 | $seg[ "mrk_id" ] = 0; |
|||||||||||
| 283 | } |
|||||||||||
| 284 | ||||||||||||
| 285 | /* |
|||||||||||
| 286 | * WARNING: |
|||||||||||
| 287 | * For those seg-source that doesn't have a mrk ( having a mrk id === null ) |
|||||||||||
| 288 | * ( null <= -1 ) === true |
|||||||||||
| 289 | * so, cast to int |
|||||||||||
| 290 | */ |
|||||||||||
| 291 | if ( (int)$seg[ "mrk_id" ] <= $lastMrkId ) { |
|||||||||||
| 292 | break; |
|||||||||||
| 293 | } |
|||||||||||
| 294 | ||||||||||||
| 295 | // update counts |
|||||||||||
| 296 | if ( !empty( $seg ) ) { |
|||||||||||
| 297 | $this->updateSegmentCounts( $seg ); |
|||||||||||
| 298 | } |
|||||||||||
| 299 | ||||||||||||
| 300 | // delete translations so the prepareSegment |
|||||||||||
| 301 | // will put source content in target tag |
|||||||||||
| 302 | if ( $this->sourceInTarget ) { |
|||||||||||
| 303 | $seg[ 'translation' ] = ''; |
|||||||||||
| 304 | $this->resetCounts(); |
|||||||||||
| 305 | } |
|||||||||||
| 306 | ||||||||||||
| 307 | // append $translation |
|||||||||||
| 308 | $translation = $this->prepareTranslation( $seg, $translation ); |
|||||||||||
| 309 | ||||||||||||
| 310 | $lastMrkId = $seg[ "mrk_id" ]; |
|||||||||||
| 311 | ||||||||||||
| 312 | [ $stateProp, $lastMrkState ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ], $lastMrkState ); |
|||||||||||
| 313 | ||||||||||||
| 314 | } |
|||||||||||
| 315 | ||||||||||||
| 316 | //append translation |
|||||||||||
| 317 | return $this->createTargetTag( $translation, $stateProp ); |
|||||||||||
| 318 | ||||||||||||
| 319 | } |
|||||||||||
| 320 | ||||||||||||
| 321 | protected function getCurrentSegment(): array { |
|||||||||||
| 322 | if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|||||||||||
| 323 | return $this->segments[ $this->transUnits[ $this->currentTransUnitId ][ 0 ] ]; // TODO try to understand why here is needed to override the method and set 0 index hardcoded |
|||||||||||
| 324 | } |
|||||||||||
| 325 | ||||||||||||
| 326 | return []; |
|||||||||||
| 327 | } |
|||||||||||
| 328 | ||||||||||||
| 329 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.