Skip to content

Commit 1a4044e

Browse files
author
Oscar Otero
committed
CodeScanner::extractCommentsStartingWith() fixes
1 parent edf4579 commit 1a4044e

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases)
99

10+
## [5.2.0] - Unreleased
11+
### Added
12+
- New function `CodeScanner::extractCommentsStartingWith()` to extract comments from the code.
13+
1014
## [5.1.0] - 2019-11-11
1115
### Added
1216
- New function `CodeScanner::ignoreInvalidFunctions()` to ignore invalid functions instead throw an exception
@@ -37,4 +41,5 @@ Previous releases are documented in [github releases](https://github.com/oscarot
3741
- The library is easier to extend
3842
- Translation id can be independent of the context + original values, in order to be more compatible with Xliff format.
3943

44+
[5.2.0]: https://github.com/php-gettext/Gettext/compare/v5.1.0...HEAD
4045
[5.1.0]: https://github.com/php-gettext/Gettext/compare/v5.0.0...v5.1.0

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ $phpScanner = new PhpScanner(
167167
Translations::create('domain3')
168168
);
169169

170+
//Set a default domain, so any translations with no domain specified, will be added to that domain
171+
$phpScanner->setDefaultDomain('domain1');
172+
173+
//Extract all comments starting with 'i18n:' and 'Translators:'
174+
$phpScanner->extractCommentsStartingWith('i18n:', 'Translators:');
175+
170176
//Scan files
171177
foreach (glob('*.php') as $file) {
172178
$phpScanner->scanFile($file);

src/Scanner/CodeScanner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function ignoreInvalidFunctions($ignore = true): self
6262
return $this;
6363
}
6464

65-
public function addCommentsStartingWith(string ...$prefixes): self
65+
public function extractCommentsStartingWith(string ...$prefixes): self
6666
{
6767
$this->commentsPrefixes = $prefixes;
6868

@@ -201,9 +201,9 @@ protected function dnpgettext(ParsedFunction $function): ?Translation
201201
);
202202
}
203203

204-
protected function addComments(ParsedFunction $function, Translation $translation): Translation
204+
protected function addComments(ParsedFunction $function, ?Translation $translation): ?Translation
205205
{
206-
if (empty($this->commentsPrefixes)) {
206+
if (empty($this->commentsPrefixes) || empty($translation)) {
207207
return $translation;
208208
}
209209

@@ -255,7 +255,7 @@ protected function checkFunction(ParsedFunction $function, int $minLength): bool
255255
protected function checkComment(string $comment): bool
256256
{
257257
foreach ($this->commentsPrefixes as $prefix) {
258-
if (strpos($comment, $prefix) === 0) {
258+
if ($prefix === '' || strpos($comment, $prefix) === 0) {
259259
return true;
260260
}
261261
}

0 commit comments

Comments
 (0)