new export template (#3082)

This commit is contained in:
Kevin Papst
2022-01-22 13:20:22 +01:00
committed by GitHub
parent 3543e0a1b5
commit 1a0608f49e
8 changed files with 134 additions and 60 deletions

View File

@@ -12,9 +12,7 @@ namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use App\Twig\LocaleFormatExtensions;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use App\Twig\TwigRendererTrait;
use Twig\Environment;
/**
@@ -22,6 +20,8 @@ use Twig\Environment;
*/
abstract class AbstractTwigRenderer implements RendererInterface
{
use TwigRendererTrait;
/**
* @var Environment
*/
@@ -34,39 +34,16 @@ abstract class AbstractTwigRenderer implements RendererInterface
protected function renderTwigTemplate(InvoiceDocument $document, InvoiceModel $model): string
{
$previousLocale = $this->changeTwigLocale($this->twig, $model->getTemplate()->getLanguage());
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
$language = $model->getTemplate()->getLanguage();
$formatLocale = $model->getFormatter()->getLocale();
$template = '@invoice/' . basename($document->getFilename());
$options = [
// model should not be used in the future, but we can likely not remove it
'model' => $model,
// new since 1.16.7 - templates should only use the pre-generated values
'invoice' => $model->toArray(),
]);
];
$this->changeTwigLocale($this->twig, $previousLocale);
return $content;
}
private function changeTwigLocale(Environment $twig, ?string $locale = null): ?string
{
// for invoices that don't have a language configured (using request locale)
if (null === $locale) {
return null;
}
/** @var TranslationExtension $extension */
$extension = $twig->getExtension(TranslationExtension::class);
/** @var LocaleAwareInterface $translator */
$translator = $extension->getTranslator();
$previousLocale = $translator->getLocale();
$translator->setLocale($locale);
/** @var LocaleFormatExtensions $extension */
$extension = $twig->getExtension(LocaleFormatExtensions::class);
$extension->setLocale($locale);
return $previousLocale;
return $this->renderTwigTemplateWithLanguage($this->twig, $template, $options, $language, $formatLocale);
}
}