Skip to content

Commit

Permalink
Merge pull request #31 from matecat/lokalise-tags
Browse files Browse the repository at this point in the history
Added SQUARE_SPRINTF type/filter
  • Loading branch information
mauretto78 committed Jul 11, 2023
2 parents 25c5f17 + 9dd8d9b commit ad6f491
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Enum/CTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class CTypeEnum {
const DOUBLE_SQUARE_BRACKETS = 'x-double-square-brackets';
const DOLLAR_CURLY_BRACKETS = 'x-dollar-curly-brackets';
const DOUBLE_UNDERSCORE = 'x-double-underscore';
const SQUARE_SPRINTF = 'x-square-sprintf';
}
42 changes: 42 additions & 0 deletions src/Filters/SquareSprintf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Matecat\SubFiltering\Filters;

use Matecat\SubFiltering\Commons\AbstractHandler;
use Matecat\SubFiltering\Enum\CTypeEnum;

class SquareSprintf extends AbstractHandler {
/**
* @inheritDoc
*/
public function transform( $segment ) {

$tags = [
'\[%s\]',
'\[%1$s\]',
'\[%s:name\]',
'\[%i\]',
'\[%1$i\]',
'\[%i:name\]',
'\[%f\]',
'\[%.2f\]',
'\[%1$.2f\]',
'\[%.2f:name\]',
];

$regex = '/'.implode("|", $tags).'/';

preg_match_all( $regex, $segment, $html, PREG_SET_ORDER );
foreach ( $html as $pos => $percentSnailVariable ) {

$segment = preg_replace(
'/' . preg_quote( $percentSnailVariable[ 0 ], '/' ) . '/',
'<ph id="__mtc_' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::SQUARE_SPRINTF . '" equiv-text="base64:' . base64_encode( $percentSnailVariable[ 0 ] ) . '"/>',
$segment,
1
);
}

return $segment;
}
}
2 changes: 2 additions & 0 deletions src/MateCatFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Matecat\SubFiltering\Filters\SpacesToNBSPForView;
use Matecat\SubFiltering\Filters\SplitPlaceholder;
use Matecat\SubFiltering\Filters\SprintfToPH;
use Matecat\SubFiltering\Filters\SquareSprintf;
use Matecat\SubFiltering\Filters\StandardPHToMateCatCustomPH;
use Matecat\SubFiltering\Filters\SubFilteredPhToHtml;
use Matecat\SubFiltering\Filters\TwigToPh;
Expand Down Expand Up @@ -167,6 +168,7 @@ public function fromLayer0ToLayer1( $segment ) {
$channel->addLast( new PercentSnail() );
$channel->addLast( new PercentNumberSnail() );
$channel->addLast( new Percentages() );
$channel->addLast( new SquareSprintf() );
$channel->addLast( new SprintfToPH() );
$channel->addLast( new RestoreXliffTagsContent() );
$channel->addLast( new RestorePlaceHoldersToXLIFFLtGt() );
Expand Down
3 changes: 3 additions & 0 deletions src/MyMemoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Matecat\SubFiltering\Filters\Snails;
use Matecat\SubFiltering\Filters\SplitPlaceholder;
use Matecat\SubFiltering\Filters\SprintfToPH;
use Matecat\SubFiltering\Filters\SquareSprintf;
use Matecat\SubFiltering\Filters\StandardPHToMateCatCustomPH;
use Matecat\SubFiltering\Filters\SubFilteredPhToHtml;
use Matecat\SubFiltering\Filters\TwigToPh;
Expand Down Expand Up @@ -76,9 +77,11 @@ public function fromLayer0ToLayer1( $segment, $cid = null ) {
$channel->addLast( new DoubleSquareBrackets() );
//$channel->addLast( new DoubleUnderscore() );
$channel->addLast( new DollarCurlyBrackets() );

$channel->addLast( new PercentSnail() );
$channel->addLast( new PercentNumberSnail() );
$channel->addLast( new Percentages() );
$channel->addLast( new SquareSprintf() );
$channel->addLast( new SprintfToPH() );
$channel->addLast( new RestoreXliffTagsContent() );
$channel->addLast( new RestorePlaceHoldersToXLIFFLtGt() );
Expand Down
10 changes: 10 additions & 0 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,14 @@ public function testWithDollarCurlyBrackets() {
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
}

public function testWithSquareSprintf() {
$filter = $this->getFilterInstance();

$db_segment = 'This string contains [%s]';
$segment_from_UI = 'This string contains <ph id="mtc_1" ctype="' . CTypeEnum::SQUARE_SPRINTF . '" equiv-text="base64:WyVzXQ=="/>';

$this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) );
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
}

}
9 changes: 9 additions & 0 deletions tests/MyMemorySubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,13 @@ public function testWithDollarCurlyBrackets()
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
}

public function testWithSquareSprintf() {
$filter = $this->getFilterInstance();

$db_segment = 'This string contains [%s]';
$segment_from_UI = 'This string contains <ph id="mtc_1" ctype="' . CTypeEnum::SQUARE_SPRINTF . '" equiv-text="base64:WyVzXQ=="/>';

$this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) );
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
}
}

0 comments on commit ad6f491

Please sign in to comment.