-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathTranslationController.php
More file actions
123 lines (102 loc) · 4.53 KB
/
TranslationController.php
File metadata and controls
123 lines (102 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Lexik\Bundle\TranslationBundle\Controller;
use Lexik\Bundle\TranslationBundle\Form\Handler\TransUnitFormHandler;
use Lexik\Bundle\TranslationBundle\Form\Type\TransUnitType;
use Lexik\Bundle\TranslationBundle\Manager\LocaleManagerInterface;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Util\Csrf\CsrfCheckerTrait;
use Lexik\Bundle\TranslationBundle\Util\Overview\StatsAggregator;
use Lexik\Bundle\TranslationBundle\Util\Profiler\TokenFinder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Cédric Girard <c.girard@lexik.fr>
*/
class TranslationController extends AbstractController
{
use CsrfCheckerTrait;
public function __construct(
private readonly StorageInterface $translationStorage,
private readonly StatsAggregator $statsAggregator,
private readonly TransUnitFormHandler $transUnitFormHandler,
private readonly TranslatorInterface $translator,
private readonly LocaleManagerInterface $localeManager,
private readonly ?TokenFinder $tokenFinder
) {
}
/**
* Display an overview of the translation status per domain.
*/
public function overviewAction(): Response
{
$stats = $this->statsAggregator->getStats();
return $this->render('@LexikTranslation/Translation/overview.html.twig', [
'layout' => $this->getParameter('lexik_translation.base_layout'),
'locales' => $this->getManagedLocales(),
'domains' => $this->translationStorage->getTransUnitDomains(),
'latestTrans' => $this->translationStorage->getLatestUpdatedAt(),
'stats' => $stats,
]);
}
/**
* Display the translation grid.
*/
public function gridAction(): Response
{
$tokens = null;
if ($this->getParameter('lexik_translation.dev_tools.enable') && $this->tokenFinder !== null) {
$tokens = $this->tokenFinder->find();
}
return $this->render('@LexikTranslation/Translation/grid.html.twig', [
'layout' => $this->getParameter('lexik_translation.base_layout'),
'inputType' => $this->getParameter('lexik_translation.grid_input_type'),
'autoCacheClean' => $this->getParameter('lexik_translation.auto_cache_clean'),
'toggleSimilar' => $this->getParameter('lexik_translation.grid_toggle_similar'),
'locales' => $this->getManagedLocales(),
'tokens' => $tokens,
]);
}
/**
* Remove cache files for managed locales.
*/
public function invalidateCacheAction(Request $request): Response
{
if (method_exists($this->translator, 'removeLocalesCacheFiles')) {
$this->translator->removeLocalesCacheFiles($this->getManagedLocales());
}
$message = $this->translator->trans('translations.cache_removed', [], 'LexikTranslationBundle');
if ($request->isXmlHttpRequest()) {
$this->checkCsrf();
return new JsonResponse(['message' => $message]);
}
$this->addFlash('success', $message);
return $this->redirect($this->generateUrl('lexik_translation_grid'));
}
/**
* Add a new trans unit with translation for managed locales.
*/
public function newAction(Request $request): Response
{
$form = $this->createForm(TransUnitType::class, $this->transUnitFormHandler->createFormData(), $this->transUnitFormHandler->getFormOptions());
if ($this->transUnitFormHandler->process($form, $request)) {
$message = $this->translator->trans('translations.successfully_added', [], 'LexikTranslationBundle');
$this->addFlash('success', $message);
/** @var SubmitButton $btn */
$btn = $form->get('save_add');
$redirectUrl = $btn->isClicked() ? 'lexik_translation_new' : 'lexik_translation_grid';
return $this->redirect($this->generateUrl($redirectUrl));
}
return $this->render('@LexikTranslation/Translation/new.html.twig', ['layout' => $this->getParameter('lexik_translation.base_layout'), 'form' => $form->createView()]);
}
/**
* Returns managed locales.
*/
protected function getManagedLocales(): array
{
return $this->localeManager->getLocales();
}
}