Skip to content

Commit

Permalink
implement php-format flag #12
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Apr 1, 2021
1 parent 63d5164 commit 14808a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/PhpScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Gettext\Scanner;

use Gettext\Translations;
use Gettext\Translation;

/**
* Class to scan PHP files and get gettext translations
Expand Down Expand Up @@ -38,4 +39,29 @@ public function getFunctionsScanner(): FunctionsScannerInterface
{
return new PhpFunctionsScanner(array_keys($this->functions));
}

protected function saveTranslation(
?string $domain,
?string $context,
string $original,
string $plural = null
): ?Translation {
$translation = parent::saveTranslation($domain, $context, $original, $plural);

if (!$translation) {
return null;
}

$original = $translation->getOriginal();

//Check if it includes a sprintf
if (strpos($original, "%") !== false) {
// %[argnum$][flags][width][.precision]specifier
if (preg_match('/%(\d+\$)?([\-\+\s0]|\'.)?(\d+)?(\.\d+)?[bcdeEfFgGhHosuxX]/', $original)) {
$translation->getFlags()->add("php-format");
}
}

return $translation;
}
}
1 change: 1 addition & 0 deletions tests/PhpScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testPhpCodeScanner()
$this->assertSame([$file => [75]], $translation->getReferences()->toArray());
$this->assertCount(1, $translation->getExtractedComments());
$this->assertSame(['i18n Tagged comment on the line before'], $translation->getExtractedComments()->toArray());
$this->assertSame(['php-format'], $translation->getFlags()->toArray());
}

public function testInvalidFunction()
Expand Down

0 comments on commit 14808a5

Please sign in to comment.