new export template (#3082)
This commit is contained in:
@@ -10,14 +10,14 @@
|
||||
namespace App\Export\Base;
|
||||
|
||||
use App\Activity\ActivityStatisticService;
|
||||
use App\Export\ExportItemInterface;
|
||||
use App\Invoice\InvoiceItemInterface;
|
||||
use App\Project\ProjectStatisticService;
|
||||
use App\Repository\Query\TimesheetQuery;
|
||||
|
||||
trait RendererTrait
|
||||
{
|
||||
/**
|
||||
* @param ExportItemInterface[] $exportItems
|
||||
* @param InvoiceItemInterface[] $exportItems
|
||||
* @return array
|
||||
*/
|
||||
protected function calculateSummary(array $exportItems)
|
||||
@@ -130,7 +130,7 @@ trait RendererTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExportItemInterface[] $exportItems
|
||||
* @param InvoiceItemInterface[] $exportItems
|
||||
* @param TimesheetQuery $query
|
||||
* @param ProjectStatisticService $projectStatisticService
|
||||
* @return array
|
||||
@@ -209,7 +209,7 @@ trait RendererTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExportItemInterface[] $exportItems
|
||||
* @param InvoiceItemInterface[] $exportItems
|
||||
* @param TimesheetQuery $query
|
||||
* @param ActivityStatisticService $activityStatisticService
|
||||
* @return array
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ final class LocaleFormatExtensions extends AbstractExtension
|
||||
return $this->formatter;
|
||||
}
|
||||
|
||||
private function getLocale()
|
||||
public function getLocale(): string
|
||||
{
|
||||
if (null === $this->locale) {
|
||||
$this->locale = \Locale::getDefault();
|
||||
|
||||
66
src/Twig/TwigRendererTrait.php
Normal file
66
src/Twig/TwigRendererTrait.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Symfony\Bridge\Twig\Extension\TranslationExtension;
|
||||
use Symfony\Contracts\Translation\LocaleAwareInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
trait TwigRendererTrait
|
||||
{
|
||||
protected function renderTwigTemplateWithLanguage(Environment $twig, string $template, array $options = [], ?string $language = null, ?string $formatLocale = null): string
|
||||
{
|
||||
$previousTranslation = null;
|
||||
$previousFormatLocale = null;
|
||||
|
||||
if ($language !== null) {
|
||||
$previousTranslation = $this->switchTranslationLocale($twig, $language);
|
||||
}
|
||||
if ($formatLocale !== null) {
|
||||
$previousFormatLocale = $this->switchFormatLocale($twig, $formatLocale);
|
||||
}
|
||||
|
||||
$content = $twig->render($template, $options);
|
||||
|
||||
if ($previousTranslation !== null) {
|
||||
$this->switchTranslationLocale($twig, $previousTranslation);
|
||||
}
|
||||
if ($previousFormatLocale !== null) {
|
||||
$this->switchFormatLocale($twig, $previousFormatLocale);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
protected function switchTranslationLocale(Environment $twig, string $language): string
|
||||
{
|
||||
/** @var TranslationExtension $extension */
|
||||
$extension = $twig->getExtension(TranslationExtension::class);
|
||||
/** @var LocaleAwareInterface $translator */
|
||||
$translator = $extension->getTranslator();
|
||||
$previous = $translator->getLocale();
|
||||
$translator->setLocale($language);
|
||||
|
||||
return $previous;
|
||||
}
|
||||
|
||||
protected function switchFormatLocale(Environment $twig, string $language): string
|
||||
{
|
||||
/** @var LocaleFormatExtensions $extension */
|
||||
$extension = $twig->getExtension(LocaleFormatExtensions::class);
|
||||
$previous = $extension->getLocale();
|
||||
$extension->setLocale($language);
|
||||
|
||||
return $previous;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user