Skip to content

Commit

Permalink
Search in translation text as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismeonmounteverest committed Oct 30, 2023
1 parent d0b80e7 commit f4f4725
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
25 changes: 14 additions & 11 deletions src/Controller/Admin/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Utilities\TranslatedFlashTrait;
use App\Utilities\TranslatorTrait;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use MessageFormatter;
use Pagerfanta\Pagerfanta;
Expand All @@ -31,6 +32,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Class TranslationController.
Expand All @@ -44,11 +46,13 @@ class TranslationController extends AbstractController

private TranslationModel $translationModel;
private string $enabledLocales;
private EntityManagerInterface $entityManager;

public function __construct(TranslationModel $translationModel, string $locales)
public function __construct(TranslationModel $translationModel, EntityManagerInterface $entityManager, string $locales)
{
$this->translationModel = $translationModel;
$this->enabledLocales = $locales;
$this->entityManager = $entityManager;
}

/**
Expand Down Expand Up @@ -85,7 +89,7 @@ public function editTranslation(
$request->getSession()->set('originalReferrer', $request->headers->get('referer'));
}

$translationRepository = $this->getDoctrine()->getRepository(Word::class);
$translationRepository = $this->entityManager->getRepository(Word::class);
/** @var Word $original */
$original = $translationRepository->findOneBy([
'code' => $code,
Expand Down Expand Up @@ -120,7 +124,7 @@ public function editTranslation(
if ('' === $invalidMessage) {
$originalDomain = $translation->getDomain();

$em = $this->getDoctrine()->getManager();
$em = $this->entityManager;
// Make sure the ID of the translations match
$translation->setCode($original->getCode());
$translation->setDomain($data->domain);
Expand Down Expand Up @@ -246,7 +250,7 @@ public function createTranslationForId(
$createForm->handleRequest($request);

if ($createForm->isSubmitted() && $createForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->entityManager;
/** @var TranslationRequest $data */
$data = $createForm->getData();
$invalidMessage = $this->checkIfICUFormatIsValid($data, $data->englishText);
Expand Down Expand Up @@ -319,7 +323,7 @@ public function createTranslationDirect(
$createForm->handleRequest($request);

if ($createForm->isSubmitted() && $createForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->entityManager;
/** @var TranslationRequest $data */
$data = $createForm->getData();
$invalidMessage = $this->checkIfICUFormatIsValid($data, $data->englishText);
Expand Down Expand Up @@ -417,16 +421,15 @@ public function addTranslation(Request $request, Language $language, $code)
$data = $addForm->getData();
$invalidMessage = $this->checkIfICUFormatIsValid($data, $data->translatedText);
if ('' === $invalidMessage) {
$em = $this->getDoctrine()->getManager();
$translation->setDomain($original->getDomain());
$translation->setSentence($data->translatedText);
$translation->setLanguage($language);
$translation->setCreated(new DateTime());
$translation->setAuthor($translator);
// No need for a description as the English original has one
$translation->setDescription('');
$em->persist($translation);
$em->flush();
$this->entityManager->persist($translation);
$this->entityManager->flush();
$this->translationModel->refreshTranslationsCacheForLocale($language->getShortCode());
$this->addTranslatedFlash('notice', 'translation.add', [
'translationId' => $original->getCode(),
Expand Down Expand Up @@ -568,7 +571,7 @@ public function listTranslations(Request $request, Language $language, $type, $c
$translations->setCurrentPage($page);

/** @var WordRepository $translationRepository */
$translationRepository = $this->getDoctrine()->getRepository(Word::class);
$translationRepository = $this->entityManager->getRepository(Word::class);
$countAll = $translationRepository->getTranslatableItemsCount('en');
$countTranslated = $translationRepository->getTranslatableItemsCount($language->getShortCode());

Expand Down Expand Up @@ -597,10 +600,10 @@ public function listTranslations(Request $request, Language $language, $type, $c
*
* @return Response
*/
public function statistics(Request $request)
public function statistics(Request $request, EntityManagerInterface $entityManager)
{
/** @var WordRepository $translationRepository */
$translationRepository = $this->getDoctrine()->getRepository(Word::class);
$translationRepository = $entityManager->getRepository(Word::class);
$countAll = $translationRepository->getTranslatableItemsCount('en');
$translationDetails = $translationRepository->getTranslationDetails($this->enabledLocales);

Expand Down
15 changes: 8 additions & 7 deletions src/Model/TranslationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
use App\Pagerfanta\TranslationAdapter;
use App\Pagerfanta\UpdateTranslationAdapter;
use App\Utilities\ManagerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class TranslationModel
{
use ManagerTrait;

/** @var TranslatorInterface */
private $translator;

Expand All @@ -29,9 +28,11 @@ class TranslationModel

/** @var string */
private $locales;
private EntityManagerInterface $entityManager;

public function __construct(
TranslatorInterface $translator,
EntityManagerInterface $entityManager,
Filesystem $filesystem,
string $cacheDirectory,
string $locales
Expand All @@ -40,6 +41,7 @@ public function __construct(
$this->cacheDirectory = $cacheDirectory;
$this->filesystem = $filesystem;
$this->locales = $locales;
$this->entityManager = $entityManager;
}

/**
Expand All @@ -64,7 +66,7 @@ public function refreshTranslationsCache(): void
public function getAdapter($type, $locale, $code)
{
$translationAdapter = null;
$connection = $this->getManager()->getConnection();
$connection = $this->entityManager->getConnection();

switch ($type) {
case 'missing':
Expand All @@ -89,15 +91,14 @@ public function getAdapter($type, $locale, $code)

public function updateDomainOfTranslations(Word $updatedTranslation)
{
$em = $this->getManager();
$translationRepository = $em->getRepository(Word::class);
$translationRepository = $this->entityManager->getRepository(Word::class);
$translations = $translationRepository->findBy(['code' => $updatedTranslation->getCode()]);

foreach ($translations as $translation) {
$translation->setDomain($updatedTranslation->getDomain());
$em->persist($translation);
$this->entityManager->persist($translation);
}
$em->flush();
$this->entityManager->flush();
}

private function removeAndWarmupCache(string $locale): void
Expand Down
1 change: 1 addition & 0 deletions src/Pagerfanta/TranslationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(Connection $connection, string $locale, string $code
";
if (!empty($code)) {
$this->query .= " WHERE (pi_lang.code LIKE '%" . $code . "%' OR pi_dflt.code LIKE '%" . $code . "%')";
$this->query .= " OR (pi_lang.Sentence LIKE '%" . $code . "%' OR pi_dflt.Sentence LIKE '%" . $code . "%')";
}
$this->query .= '
ORDER BY created desc';
Expand Down

0 comments on commit f4f4725

Please sign in to comment.