Skip to content

Instantly share code, notes, and snippets.

@jiripudil
Last active March 22, 2020 12:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiripudil/57ad8ed153cc577f20b28e56a7614a1c to your computer and use it in GitHub Desktop.
Save jiripudil/57ad8ed153cc577f20b28e56a7614a1c to your computer and use it in GitHub Desktop.
Symfony/Translation keys extraction from Nette and Latte
<?php
declare(strict_types=1);
namespace App\Infrastructure\Localization\Extractor;
use Latte\Engine;
use Nette\Application\UI\ITemplateFactory;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Utils\Finder;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\Extractor\PhpExtractor;
use Symfony\Component\Translation\MessageCatalogue;
final class LatteExtractor implements ExtractorInterface
{
private Engine $latte;
private PhpExtractor $compiledLatteExtractor;
private string $prefix;
public function __construct(ITemplateFactory $templateFactory)
{
$template = $templateFactory->createTemplate();
\assert($template instanceof Template);
$this->latte = $template->getLatte();
$this->compiledLatteExtractor = new class extends PhpExtractor {
protected $sequences = [
[
'$this',
'->',
'filters',
'->',
'translate',
')',
'(',
PhpExtractor::MESSAGE_TOKEN,
],
[
'call_user_func',
'(',
'$this',
'->',
'filters',
'->',
'translate',
',',
PhpExtractor::MESSAGE_TOKEN,
],
[
'$this',
'->',
'filters',
'->',
'filterContent',
'(',
'"translate"',
',',
'$_fi',
',',
PhpExtractor::MESSAGE_TOKEN,
],
];
};
}
public function extract($resource, MessageCatalogue $catalogue)
{
foreach (Finder::findFiles('*.latte')->from($resource) as $file) {
$this->extractFile($file, $catalogue);
}
}
private function extractFile(\SplFileInfo $file, MessageCatalogue $catalogue): void
{
$filePath = $file->getPathname();
$compiledTemplateFile = $this->latte->getCacheFile($filePath);
$this->latte->warmupCache($filePath);
$this->compiledLatteExtractor->extract($compiledTemplateFile, $catalogue);
}
public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}
}
<?php
declare(strict_types=1);
namespace App\Infrastructure\Localization\Extractor;
use Symfony\Component\Translation\Extractor\PhpExtractor;
final class NetteTranslatorExtractor extends PhpExtractor
{
public function __construct()
{
$this->sequences[] = [
'->',
'translate',
'(',
PhpExtractor::MESSAGE_TOKEN,
];
}
}
<?php
declare(strict_types=1);
$extractor = new Symfony\Component\Translation\Extractor\ChainExtractor();
$extractor->addExtractor('latte', new App\Infrastructure\Localization\Extractor\LatteExtractor());
$extractor->addExtractor('php', new App\Infrastructure\Localization\Extractor\NetteTranslatorExtractor());
$sourceDirectory = __DIR__;
$extractedKeys = new MessageCatalogue(NULL);
$extractor->extract($sourceDirectory, $extractedKeys);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment