src/Application/Controller/TranslationController.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Application\Controller;
  4. use App\Application\Messenger\ErrorResponseBuilder;
  5. use App\Application\Service\CacheService;
  6. use JsonException;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Messenger\MessageBusInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class TranslationController extends AbstractController
  12. {
  13.     public function __construct(
  14.         ErrorResponseBuilder $errorResponseBuilder,
  15.         MessageBusInterface $commandBus,
  16.         MessageBusInterface $queryBus,
  17.         private readonly string $frontTranslationsDir,
  18.         private readonly CacheService $cacheService,
  19.     ) {
  20.         parent::__construct($errorResponseBuilder$commandBus$queryBus);
  21.     }
  22.     /**
  23.      * @throws JsonException
  24.      */
  25.     #[Route('/api/translations/{locale}')]
  26.     public function index(string $locale): JsonResponse
  27.     {
  28.         $filePath "{$this->frontTranslationsDir}/$locale.json";
  29.         if (!file_exists($filePath)) {
  30.             throw new NotFoundHttpException(sprintf('Translation file for locale "%s" not found'$locale));
  31.         }
  32.         $content file_get_contents($filePath);
  33.         $response = new JsonResponse(json_decode($contenttrue512JSON_THROW_ON_ERROR));
  34.         $response->headers->set('Content-Encoding''gzip');
  35.         $response->setContent(gzencode($response->getContent(), 9));
  36.         $this->cacheService->getCacheHeaders(["/translations/$locale"], $response->headers);
  37.         return $response;
  38.     }
  39. }