Skip to content

Commit

Permalink
Rework translation processing
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Jun 13, 2024
1 parent e9455e3 commit f39a27a
Show file tree
Hide file tree
Showing 14 changed files with 634 additions and 693 deletions.
217 changes: 113 additions & 104 deletions Classes/Controller/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function listAction(): ResponseInterface
$defaultValue = $value;

$translations = $this->translationRepository
->getAllRecordsByIdentifier(
->findAllByComponentTypePlaceholderValueAndLanguage(
$componentId,
$typeId,
$placeholder,
Expand Down Expand Up @@ -271,6 +271,91 @@ public function listAction(): ResponseInterface
return $this->moduleResponse();
}

/**
* @param int $uid
*
* @return ResponseInterface
*/
public function translatedAction(int $uid): ResponseInterface
{
$translated = array_merge(
[
$this->translationRepository->findByUid($uid),
],
$this->translationRepository->findByPidAndLanguage($uid)
);

$languages = $this->translationService->getAllLanguages();
$untranslated = $languages;

/** @var Translation $translation */
foreach ($translated as $translation) {
unset($untranslated[$translation->getSysLanguageUid()]);
}

$this->view->assign('originalUid', $uid);
$this->view->assign('translated', $translated);
$this->view->assign('untranslated', $untranslated);
$this->view->assign('languages', $languages);

return $this->moduleResponse();
}

/**
* @param int $parent
* @param array<int, string> $new
* @param array<int, string> $update
*
* @return ResponseInterface
*
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @throws Exception
*/
public function translateRecordAction(int $parent, array $new = [], array $update = []): ResponseInterface
{
$this->translationRepository
->injectPersistenceManager($this->persistenceManager);

/** @var Translation|null $parentTranslation */
$parentTranslation = $this->translationRepository->findByUid($parent);

if ($parentTranslation instanceof Translation) {
foreach ($new as $language => $value) {
$translation = $this->translationService
->createTranslationFromParent(
$parentTranslation,
$language,
$value
);

if ($translation instanceof Translation) {
$this->translationRepository->add($translation);
}
}
}

foreach ($update as $translationUid => $value) {
/** @var Translation $translation */
$translation = $this->translationRepository->findByUid($translationUid);

if ($translation instanceof Translation) {
$translation->setValue($value);

$this->translationRepository->update($translation);
}
}

$this->persistenceManager->persistAll();

return (new ForwardResponse('translated'))
->withControllerName('Translation')
->withExtensionName('NrTextdb')
->withArguments([
'uid' => $parent,
]);
}

/**
* Create an export of the current filtered textDB entries to import it safely into another system.
*
Expand Down Expand Up @@ -327,27 +412,29 @@ public function exportAction(): ResponseInterface

if ($language->getLanguageId() === 0) {
$translations = $this->translationRepository
->getAllRecordsByIdentifier(
->findAllByComponentTypePlaceholderValueAndLanguage(
(int) $component,
(int) $type,
$placeholder,
$value
);

$originals = $this->writeTranslationExportFile(
$language,
$translations,
$exportDir,
$targetFileName,
$enableTargetMarker
);
} else {
$translations = $this->translationRepository
->getTranslatedRecordsForLanguage(
->findByTranslationsAndLanguage(
$originals,
$language->getLanguageId()
);

$this->writeTranslationExportFile(
$language,
$translations,
$exportDir,
$targetFileName,
Expand Down Expand Up @@ -435,89 +522,6 @@ private function getExportFileNameForLanguage(SiteLanguage $language): string
return $language->getLocale()->getLanguageCode() . '.textdb_import.xlf';
}

/**
* @param int $uid
*
* @return ResponseInterface
*/
public function translatedAction(int $uid): ResponseInterface
{
$translated = array_merge(
[
$this->translationRepository->findRecordByUid($uid),
],
$this->translationRepository->getTranslatedRecords($uid)
);

$languages = $this->translationService->getAllLanguages();
$untranslated = $languages;

/** @var Translation $translation */
foreach ($translated as $translation) {
unset($untranslated[$translation->getLanguageUid()]);
}

$this->view->assign('originalUid', $uid);
$this->view->assign('translated', $translated);
$this->view->assign('untranslated', $untranslated);
$this->view->assign('languages', $languages);

return $this->moduleResponse();
}

/**
* @param int $parent
* @param array<int, string> $new
* @param array<int, string> $update
*
* @return ResponseInterface
*
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @throws Exception
*/
public function translateRecordAction(int $parent, array $new = [], array $update = []): ResponseInterface
{
$this->translationRepository->injectPersistenceManager($this->persistenceManager);

/** @var Translation $originalTranslation */
$originalTranslation = $this->translationRepository->findByUid($parent);

if (
($originalTranslation->getComponent() !== null)
&& ($originalTranslation->getEnvironment() !== null)
&& ($originalTranslation->getType() !== null)
) {
foreach ($new as $language => $value) {
$this->translationRepository
->createTranslation(
$originalTranslation->getComponent()->getName(),
$originalTranslation->getEnvironment()->getName(),
$originalTranslation->getType()->getName(),
$originalTranslation->getPlaceholder(),
$language,
$value
);
}
}

foreach ($update as $translationUid => $value) {
$translation = $this->translationRepository->findRecordByUid($translationUid);

if ($translation instanceof Translation) {
$translation->setValue($value);
$this->translationRepository->update($translation);
}
}

$this->persistenceManager->persistAll();

return (new ForwardResponse('translated'))
->withControllerName('Translation')
->withExtensionName('NrTextdb')
->withArguments(['uid' => $parent]);
}

/**
* Import translations from file.
*
Expand Down Expand Up @@ -562,11 +566,10 @@ public function importAction(bool $update = false): ResponseInterface
$languageCode = trim($matches[1], '.');
$languageCode = $languageCode === '' ? 'en' : $languageCode;

$imported = 0;
$updated = 0;
$languages = [];
$errors = [];

$imported = 0;
$updated = 0;
$languages = [];
$errors = [];
$forceUpdate = $update;

foreach ($this->translationService->getAllLanguages() as $language) {
Expand Down Expand Up @@ -699,7 +702,7 @@ private function getPlaceholderFromKey(string $key): ?string
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
*/
protected function getExtensionConfiguration(): mixed
private function getExtensionConfiguration(): mixed
{
return $this->extensionConfiguration->get('nr_textdb');
}
Expand All @@ -709,7 +712,7 @@ protected function getExtensionConfiguration(): mixed
*
* @return array<array-key, int|string>
*/
protected function getConfigFromBeUserData(): array
private function getConfigFromBeUserData(): array
{
$serializedConfig = $this->getBackendUser()->getModuleData(static::class);
if (is_string($serializedConfig)
Expand All @@ -730,22 +733,24 @@ protected function getConfigFromBeUserData(): array
*
* @param array<array-key, int|string> $config
*/
protected function persistConfigInBeUserData(array $config): void
private function persistConfigInBeUserData(array $config): void
{
$this->getBackendUser()->pushModuleData(static::class, serialize($config));
}

/**
* Write the translation file for export and returns the uid of entries written to file.
*
* @param SiteLanguage $language
* @param QueryResultInterface $translations
* @param string $exportDir
* @param string $filename
* @param bool $enableTargetMarker
*
* @return int[]
*/
protected function writeTranslationExportFile(
private function writeTranslationExportFile(
SiteLanguage $language,
QueryResultInterface $translations,
string $exportDir,
string $filename,
Expand All @@ -765,7 +770,7 @@ protected function writeTranslationExportFile(
$entries = '';
$writtenTranslationIds = [];

$maker = $enableTargetMarker ? 'target' : 'source';
$marker = $enableTargetMarker ? 'target' : 'source';

/** @var Translation $translation */
foreach ($translations as $translation) {
Expand All @@ -789,13 +794,17 @@ protected function writeTranslationExportFile(
$translation->getComponent()->getName(),
$translation->getType()->getName(),
$translation->getPlaceholder(),
$maker,
$marker,
$translation->getValue(),
$maker
$marker
);
}

$fileContent = sprintf($markup, $entries);
$fileContent = sprintf(
$markup,
$language->getLocale()->getLanguageCode(),
$entries
);

file_put_contents($exportDir . '/' . $filename, $fileContent);

Expand All @@ -809,7 +818,7 @@ protected function writeTranslationExportFile(
*
* @return void
*/
protected function registerDocHeaderButtons(ModuleTemplate $moduleTemplate): void
private function registerDocHeaderButtons(ModuleTemplate $moduleTemplate): void
{
// Instantiate required classes
$buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
Expand Down Expand Up @@ -878,7 +887,7 @@ protected function registerDocHeaderButtons(ModuleTemplate $moduleTemplate): voi
/**
* @return BackendUserAuthentication
*/
protected function getBackendUser(): BackendUserAuthentication
private function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
Expand All @@ -895,7 +904,7 @@ protected function getBackendUser(): BackendUserAuthentication
*
* @return array<string, mixed>
*/
protected function getPagination(QueryResultInterface $items, array $settings): array
private function getPagination(QueryResultInterface $items, array $settings): array
{
$currentPage = $this->request->hasArgument('currentPage')
? (int) $this->request->getArgument('currentPage') : 1;
Expand Down Expand Up @@ -928,7 +937,7 @@ protected function getPagination(QueryResultInterface $items, array $settings):
*
* @return void
*/
protected function addFlashMessageToQueue(
private function addFlashMessageToQueue(
string $messageTitle,
string $messageText,
ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::ERROR
Expand All @@ -955,7 +964,7 @@ protected function addFlashMessageToQueue(
*
* @return LanguageService
*/
protected function getLanguageService(): LanguageService
private function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
Expand Down
Loading

0 comments on commit f39a27a

Please sign in to comment.