8000 [Translator] Set sources when extracting strings from php files by Stadly · Pull Request #31249 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translator] Set sources when extracting strings from php files #31249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function extract($resource, MessageCatalogue $catalog)
{
$files = $this->extractFiles($resource);
foreach ($files as $file) {
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);

gc_mem_caches();
}
Expand Down Expand Up @@ -197,9 +197,15 @@ private function getValue(\Iterator $tokenIterator)
*
* @param array $tokens
* @param MessageCatalogue $catalog
* @param string $filename
*/
protected function parseTokens($tokens, MessageCatalogue $catalog)
protected function parseTokens($tokens, MessageCatalogue $catalog/*, string $filename*/)
{
if (\func_num_args() < 3 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
@trigger_error(sprintf('The "%s()" method will have a new "string $filename" argument in version 5.0, not defining it is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
}
$filename = 2 < \func_num_args() ? \func_get_arg(2) : '';

$tokenIterator = new \ArrayIterator($tokens);

for ($key = 0; $key < $tokenIterator->count(); ++$key) {
Expand Down Expand Up @@ -236,6 +242,10 @@ protected function parseTokens($tokens, MessageCatalogue $catalog)

if ($message) {
$catalog->set($message, $this->prefix.$message, $domain);
$metadata = $catalog->getMetadata($message, $domain) ?? [];
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
$metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
$catalog->setMetadata($message, $metadata, $domain);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function testExtraction($resource)
$actualCatalogue = $catalogue->all();

$this->assertEquals($expectedCatalogue, $actualCatalogue);

$filename = __DIR__.'/../fixtures/extractor/translation.html.php';
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('single-quoted key'));
$this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
}

public function resourcesProvider()
Expand Down
0