Skip to content

Instantly share code, notes, and snippets.

@Gappa
Created March 10, 2020 09:12
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 Gappa/24d38b943f6c8ab3a02cb244ccb63f6e to your computer and use it in GitHub Desktop.
Save Gappa/24d38b943f6c8ab3a02cb244ccb63f6e to your computer and use it in GitHub Desktop.
Symfony Messages Extractor & Nette
<?php
declare(strict_types=1);
namespace Symfony\Component\Translation\Dumper;
use Nette\Neon\Neon;
use Symfony\Component\Translation\Dumper\FileDumper;
use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Util\ArrayConverter;
class NeonFileDumper extends FileDumper
{
/** @var string */
private $extension;
public function __construct(string $extension = 'neon')
{
$this->extension = $extension;
}
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
if (!class_exists('Nette\Neon\Neon')) {
throw new LogicException('Dumping translations in the NEON format requires the Nette Neon package.');
}
$data = $messages->all($domain);
if (isset($options['as_tree']) && $options['as_tree']) {
$data = ArrayConverter::expandToTree($data);
}
return Neon::encode($data, 1);
}
/**
* {@inheritdoc}
*/
protected function getExtension()
{
return $this->extension;
}
}
<?php
declare(strict_types=1);
// Removed dependency on Symfony Finder, now uses Nette Finder
// https://gist.github.com/jiripudil/57ad8ed153cc577f20b28e56a7614a1c#file-nettetranslatorextractor-php
namespace Symfony\Component\Translation\Extractor;
use Nette\Utils\Finder;
use Symfony\Component\Translation\Extractor\PhpExtractor;
final class NetteTranslatorExtractor extends PhpExtractor
{
public function __construct()
{
$this->sequences[] = [
'->',
'translate',
'(',
PhpExtractor::MESSAGE_TOKEN,
];
}
protected function extractFromDirectory($directory)
{
return Finder::findFiles('*.php')->from($directory);
}
}
<?php
declare(strict_types=1);
use Symfony\Component\Translation\Extractor\LatteExtractor;
use Symfony\Component\Translation\ExtractorNetteTranslatorExtractor;
use Symfony\Component\Translation\Dumper\NeonFileDumper;
use Nette\Application\UI\ITemplateFactory;
use Symfony\Component\Translation\Catalogue\TargetOperation;
use Symfony\Component\Translation\Extractor\ChainExtractor;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;
$extractor = new ChainExtractor();
$extractor->addExtractor('latte', new LatteExtractor($this->templateFactory)); // ITemplateFactory from DI
$extractor->addExtractor('php', new NetteTranslatorExtractor());
$extractedCatalogue = new MessageCatalogue('new'); // locale
$extractor->extract($sourceDir, $extractedCatalogue);
$currentCatalogue = new MessageCatalogue('new');
$operation = new TargetOperation($currentCatalogue, $extractedCatalogue);
$writer = new TranslationWriter;
$writer->addDumper('neon', new NeonFileDumper());
$writer->write($operation->getResult(), 'neon', [
'path' => $this->tempDir,
// 'as_tree' => true, // Otherwise the structure is flat and repeated on each line
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment