src/Service/TranslationService.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Translation;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. class TranslationService extends AbstractController
  10. {
  11.     /**
  12.      * @var Request|null
  13.      */
  14.     private $request;
  15.     /**
  16.      * @var SessionInterface
  17.      */
  18.     private $session;
  19.     /**
  20.      * TranslationService constructor.
  21.      * @param RequestStack $request_stack
  22.      * @param SessionInterface $session
  23.      */
  24.     public function __construct(RequestStack $request_stackSessionInterface $session)
  25.     {
  26.         // init request
  27.         $this->request $request_stack->getCurrentRequest();
  28.         $this->session $session;
  29.     }
  30.     /**
  31.      * @param string $name
  32.      * @param string $translation
  33.      * @return Translation|object
  34.      */
  35.     public function setTranslation(string $namestring $translation)
  36.     {
  37.         $user $this->getUser();
  38.         if (!$user) {
  39.             $userId 0;
  40.             $language $this->request->getLocale();
  41.         } else {
  42.             $userId $user->getId();
  43.             $language $user->getLanguage();
  44.         }
  45.         $entityManager $this->getDoctrine()->getManager();
  46.         $translationObject = new Translation();
  47.         $entityManager->persist($translationObject
  48.             ->setLanguage($language)
  49.             ->setName($name)
  50.             ->setTranslation($translation)
  51.             ->setCreated(new \DateTime())
  52.             ->setCreatedBy($userId)
  53.             ->setModified(new \DateTime())
  54.             ->setModifiedBy($userId));
  55.         $entityManager->flush();
  56.         $entityManager->refresh($translationObject);
  57.         return $translationObject;
  58.     }
  59.     /**
  60.      * @param string $name
  61.      * @return string|null
  62.      */
  63.     public function getTranslation(string $name): ?string
  64.     {
  65.         $entityManager $this->getDoctrine()->getManager();
  66.         $language $this->determineLanguage();
  67.         $translations $entityManager->getRepository(Translation::class)->findBy([
  68.             "name" => $name,
  69.             "language" => $language,
  70.         ], [
  71.             "id" => "DESC"
  72.         ], 1);
  73.         // if we have no translation fall back to default language EN
  74.         if (!count($translations)) {
  75.             $translations $entityManager->getRepository(Translation::class)->findBy([
  76.                 "name" => $name,
  77.                 "language" => "en",
  78.             ], [
  79.                 "id" => "DESC"
  80.             ], 1);
  81.         }
  82.         // if there is even no EN translation just return the name
  83.         if (!count($translations)) {
  84.             return $name;
  85.         }
  86.         return htmlspecialchars($translations[0]->getTranslation());
  87.     }
  88.     /**
  89.      * @return mixed|string
  90.      */
  91.     private function determineLanguage()
  92.     {
  93.         // try to load from session first
  94.         if ($this->session->get("_locale"false)) {
  95.             return $this->session->get("_locale");
  96.         }
  97.         // try to load from user second
  98.         if ($this->getUser()) {
  99.             return $this->getUser()->getLanguage();
  100.         }
  101.         // try to load from cookie
  102.         if (isset($_COOKIE["language"])) {
  103.             return $_COOKIE["language"];
  104.         }
  105.         // else use locale
  106.         return $this->request->getLocale();
  107.     }
  108. }