Skip to content

Commit 14808a5

Browse files
committed
implement php-format flag #12
1 parent 63d5164 commit 14808a5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/PhpScanner.php

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Gettext\Scanner;
55

66
use Gettext\Translations;
7+
use Gettext\Translation;
78

89
/**
910
* Class to scan PHP files and get gettext translations
@@ -38,4 +39,29 @@ public function getFunctionsScanner(): FunctionsScannerInterface
3839
{
3940
return new PhpFunctionsScanner(array_keys($this->functions));
4041
}
42+
43+
protected function saveTranslation(
44+
?string $domain,
45+
?string $context,
46+
string $original,
47+
string $plural = null
48+
): ?Translation {
49+
$translation = parent::saveTranslation($domain, $context, $original, $plural);
50+
51+
if (!$translation) {
52+
return null;
53+
}
54+
55+
$original = $translation->getOriginal();
56+
57+
//Check if it includes a sprintf
58+
if (strpos($original, "%") !== false) {
59+
// %[argnum$][flags][width][.precision]specifier
60+
if (preg_match('/%(\d+\$)?([\-\+\s0]|\'.)?(\d+)?(\.\d+)?[bcdeEfFgGhHosuxX]/', $original)) {
61+
$translation->getFlags()->add("php-format");
62+
}
63+
}
64+
65+
return $translation;
66+
}
4167
}

tests/PhpScannerTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testPhpCodeScanner()
4848
$this->assertSame([$file => [75]], $translation->getReferences()->toArray());
4949
$this->assertCount(1, $translation->getExtractedComments());
5050
$this->assertSame(['i18n Tagged comment on the line before'], $translation->getExtractedComments()->toArray());
51+
$this->assertSame(['php-format'], $translation->getFlags()->toArray());
5152
}
5253

5354
public function testInvalidFunction()

0 commit comments

Comments
 (0)