Skip to content

Commit

Permalink
[FEATURE] respect the feature to overwrite language files in ext_loca…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphex2 committed Jan 19, 2024
1 parent c4a0b39 commit aa5d036
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 105 deletions.
16 changes: 9 additions & 7 deletions Classes/Controller/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Datamints\DatamintsLocallangBuilder\Controller;

use Datamints\DatamintsLocallangBuilder\Controller\Traits\CallActionMethodTrait;
use Datamints\DatamintsLocallangBuilder\Utility\DatabaseUtility;
use Datamints\DatamintsLocallangBuilder\Domain\Repository\Traits\{ExtensionRepositoryTrait,
LocallangRepositoryTrait,
TranslationRepositoryTrait,
TranslationValueRepositoryTrait};
use Datamints\DatamintsLocallangBuilder\Mvc\View\JsonView;
use Datamints\DatamintsLocallangBuilder\Service\Traits\{CachesServiceTrait, ProviderServiceTrait};
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

class ApplicationController extends AbstractController
{
Expand All @@ -28,14 +30,14 @@ public function initializeClearAction()

/**
* main Action
*
* Entry-Point transfer some basic configuration towards the vue-app
* @return \Psr\Http\Message\ResponseInterface
*
* @return ResponseInterface
* @throws \TYPO3\CMS\Core\Package\Exception
*/
public function mainAction():ResponseInterface
{
$extensionVersion = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion($this->request->getControllerExtensionKey());
$extensionVersion = ExtensionManagementUtility::getExtensionVersion($this->request->getControllerExtensionKey());
$this->logger->info("Opened the translate-module.");
$this->view->assignMultiple([
'version' => $extensionVersion,
Expand All @@ -54,9 +56,10 @@ public function mainAction():ResponseInterface

/**
* clear Action
*
* clears all related extension related db-tables when requested by e.g. reimport-action
* @return \Psr\Http\Message\ResponseInterface
*
* @\Symfony\Component\Routing\Annotation\Route
* @return ResponseInterface
*/
public function clearAction():ResponseInterface
{
Expand All @@ -67,8 +70,7 @@ public function clearAction():ResponseInterface
$this->translationRepository->removeAllQuick();
$this->translationValueRepository->removeAllQuick();

\Datamints\DatamintsLocallangBuilder\Utility\DatabaseUtility::persistAll();

DatabaseUtility::persistAll();

return $this->jsonResponse(json_encode(['message' => 'All database-tables have been reset to zero.']));
}
Expand Down
1 change: 1 addition & 0 deletions Classes/Controller/ExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct()
* Those have to be fetches in an different (LocallangController::show) call, to save some memory-space
* The Cache can be invoked by triggering the cache-clear button in typo3 backend
*
* @\Symfony\Component\Routing\Annotation\Route
* @return \Psr\Http\Message\ResponseInterface
*/
public function listAction():ResponseInterface
Expand Down
32 changes: 17 additions & 15 deletions Classes/Domain/Model/Locallang.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Datamints\DatamintsLocallangBuilder\Domain\Model;

use JsonSerializable;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

/**
* This file is part of the "datamints_locallang_builder" Extension for TYPO3 CMS.
Expand All @@ -11,7 +13,7 @@
* (c) 2021 Mark Weisgerber <[email protected] / [email protected]>
* Locallang
*/
class Locallang extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity implements JsonSerializable
class Locallang extends AbstractEntity implements JsonSerializable
{

/**
Expand All @@ -38,16 +40,16 @@ class Locallang extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity implement
/**
* translations
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Datamints\DatamintsLocallangBuilder\Domain\Model\Translation>
* @var ObjectStorage<Translation>
* @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
* @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
*/
protected ?\TYPO3\CMS\Extbase\Persistence\ObjectStorage $translations = null;
protected ?ObjectStorage $translations = null;

/**
* Bidirectional for easier db-queries
*
* @var \Datamints\DatamintsLocallangBuilder\Domain\Model\Extension
* @var Extension
* @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
*/
protected $relatedExtension = null;
Expand All @@ -72,7 +74,7 @@ public function __construct()
*/
public function initializeObject()
{
$this->translations = $this->translations ?: new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
$this->translations = $this->translations ?: new ObjectStorage();
}

/**
Expand All @@ -99,21 +101,21 @@ public function setPath($path)
/**
* Adds a Translation
*
* @param \Datamints\DatamintsLocallangBuilder\Domain\Model\Translation $translation
* @param Translation $translation
* @return void
*/
public function addTranslation(\Datamints\DatamintsLocallangBuilder\Domain\Model\Translation $translation)
public function addTranslation(Translation $translation)
{
$this->translations->attach($translation);
}

/**
* Removes a Translation
*
* @param \Datamints\DatamintsLocallangBuilder\Domain\Model\Translation $translationToRemove The Translation to be removed
* @param Translation $translationToRemove The Translation to be removed
* @return void
*/
public function removeTranslation(\Datamints\DatamintsLocallangBuilder\Domain\Model\Translation $translationToRemove)
public function removeTranslation(Translation $translationToRemove)
{
$this->translations->detach($translationToRemove);
}
Expand Down Expand Up @@ -181,7 +183,7 @@ public function setFilename($filename)
/**
* Returns the translations
*
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Datamints\DatamintsLocallangBuilder\Domain\Model\Translation> translations
* @return ObjectStorage<Translation> translations
*/
public function getTranslations()
{
Expand All @@ -191,18 +193,18 @@ public function getTranslations()
/**
* Sets the translations
*
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Datamints\DatamintsLocallangBuilder\Domain\Model\Translation>|null $translations
* @param ObjectStorage<Translation>|null $translations
* @return void
*/
public function setTranslations(?\TYPO3\CMS\Extbase\Persistence\ObjectStorage $translations)
public function setTranslations(?ObjectStorage $translations)
{
$this->translations = $translations;
}

/**
* Returns the relatedExtension
*
* @return \Datamints\DatamintsLocallangBuilder\Domain\Model\Extension $relatedExtension
* @return Extension $relatedExtension
*/
public function getRelatedExtension()
{
Expand All @@ -212,10 +214,10 @@ public function getRelatedExtension()
/**
* Sets the relatedExtension
*
* @param \Datamints\DatamintsLocallangBuilder\Domain\Model\Extension $relatedExtension
* @param Extension $relatedExtension
* @return void
*/
public function setRelatedExtension(\Datamints\DatamintsLocallangBuilder\Domain\Model\Extension $relatedExtension)
public function setRelatedExtension(Extension $relatedExtension)
{
$this->relatedExtension = $relatedExtension;
}
Expand Down
40 changes: 40 additions & 0 deletions Classes/Service/CustomTranslationsOverlayService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Copyright (c) 2021. Mark Weisgerber ([email protected] / [email protected])
*/

namespace Datamints\DatamintsLocallangBuilder\Service;

use Datamints\DatamintsLocallangBuilder\Domain\Model\Locallang;
use Datamints\DatamintsLocallangBuilder\Domain\Model\Runtime\LocallangExport;
use Datamints\DatamintsLocallangBuilder\Exporter\XmlExporter;
use Datamints\DatamintsLocallangBuilder\Service\Traits\ConfigurationServiceTrait;
use Datamints\DatamintsLocallangBuilder\Service\Traits\LocallangServiceTrait;
use Datamints\DatamintsLocallangBuilder\Service\Traits\XmlServiceTrait;
use Datamints\DatamintsLocallangBuilder\Utility\LanguageUtility;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class CustomTranslationsOverlayService extends AbstractService
{
/**
* Overwrites the path for custom translations that have been overwritten by an extension.
* See: https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/Localization/ManagingTranslations.html?highlight=locallangxmloverride#custom-translations
*
* Beware!! currently we only accept ONE override. When multiple values are given we'll choose the first in the list!
* @param string $path
* @return string
*/
public function setOverlay(string $path): string
{
foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] as $locallangXMLOverridePath => $locallangXMLOverrides){
if($locallangXMLOverridePath === $path){
return $locallangXMLOverrides[0];
}

}
// passthrough the original given value if no override matches
return $path;
}
}
58 changes: 34 additions & 24 deletions Classes/Service/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@

namespace Datamints\DatamintsLocallangBuilder\Service;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use Datamints\DatamintsLocallangBuilder\Exporter\XmlExporter;
use Datamints\DatamintsLocallangBuilder\Domain\Model\Locallang;
use Datamints\DatamintsLocallangBuilder\Utility\LanguageUtility;
use Datamints\DatamintsLocallangBuilder\Service\Traits\XmlServiceTrait;
use Datamints\DatamintsLocallangBuilder\Domain\Model\Runtime\LocallangExport;
use Datamints\DatamintsLocallangBuilder\Service\Traits\LocallangServiceTrait;
use Datamints\DatamintsLocallangBuilder\Exporter\XmlExporter;
use Datamints\DatamintsLocallangBuilder\Utility\LanguageUtility;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ExportService extends AbstractService
{
use LocallangServiceTrait;
use XmlServiceTrait;
use \Datamints\DatamintsLocallangBuilder\Service\Traits\ConfigurationServiceTrait;
public function __construct(
protected readonly LocallangService $locallangService,
protected readonly XmlService $xmlService,
protected readonly CustomTranslationsOverlayService $customTranslationsOverlayService
)
{}

/**
* Constant to fileadmin save-storage
*/
public const FILEADMIN_PATH = 'fileadmin/locallang-builder/';
public const string FILEADMIN_PATH = 'fileadmin/locallang-builder/';

/**
* Export
*
* @param \Datamints\DatamintsLocallangBuilder\Domain\Model\Locallang $locallang
* @param array $exportConfiguration
*
* @return array
* @throws Exception
*/
public function export(Locallang $locallang, array $exportConfiguration): array
{
$countries = $this->locallangService->getCountryList($locallang); // Getting country list, so we know, which files have to be generated
$countries = $this->locallangService->getCountryList(
$locallang
); // Getting country list, so we know, which files have to be generated
$outputLocallangs = [];
foreach ($countries as $country) {
/** @var LocallangExport $locallangExport */
Expand All @@ -47,15 +47,15 @@ public function export(Locallang $locallang, array $exportConfiguration): array
$targetPath = '';

// When we want to save the files to fileadmin instead
if($exportConfiguration['selectedTarget'] === 'fileadmin') {
if ($exportConfiguration['selectedTarget'] === 'fileadmin') {
$targetPath = $this->getFileadminOutputPath($locallang);
} else { // Otherwise we overwrite the existing files
$targetPath = $locallang->getPath();
}
if($locallangExport->getLanguageCode() != 'en') { // we dont need the lang-code for default-language
if ($locallangExport->getLanguageCode() != 'en') { // we dont need the lang-code for default-language
$targetPath = LanguageUtility::getCountryLanguagePath($country, $targetPath);
}
$locallangExport->setTargetPath($targetPath);
$locallangExport->setTargetPath($this->customTranslationsOverlayService->setOverlay($targetPath));
$outputLocallangs[$country] = $locallangExport;
}

Expand All @@ -72,26 +72,36 @@ public function export(Locallang $locallang, array $exportConfiguration): array
return $savedFiles;
}


/**
* Delivers the alternative fileadmin output path
*
* @param \Datamints\DatamintsLocallangBuilder\Domain\Model\Locallang $locallang
*
* @param Locallang $locallang
* @return string
* @throws Exception
*/
protected function getFileadminOutputPath(Locallang $locallang): string
{
$readableDateTime = new \DateTime();
$customPath = $this->configurationService->getExtensionConfiguration()['exportPath'];
$exportPathDate = boolval($this->configurationService->getExtensionConfiguration()['exportPathDate']);
if(!is_dir(GeneralUtility::getFileAbsFileName($customPath))) { // Checking if export-folder exists. It may be that the admin changed the extension-settings recently, so a permanent check is required
if (!is_dir(
GeneralUtility::getFileAbsFileName($customPath)
)) { // Checking if export-folder exists. It may be that the admin changed the extension-settings recently, so a permanent check is required
\mkdir(GeneralUtility::getFileAbsFileName($customPath));
}

if(GeneralUtility::validPathStr($customPath)) { // Check if the path is valid and does not contain illegal chars
return $customPath . $locallang->getRelatedExtension()->getName() . (($exportPathDate) ? '/' . $readableDateTime->format('Y-m-d___H-i-s') . '/' : '/') . ManifestBuildService::EXTENSION_LANGUAGE_PATH . $locallang->getFilename();
if (GeneralUtility::validPathStr(
$customPath
)) { // Check if the path is valid and does not contain illegal chars
return $customPath . $locallang->getRelatedExtension()->getName(
) . (($exportPathDate) ? '/' . $readableDateTime->format(
'Y-m-d___H-i-s'
) . '/' : '/') . ManifestBuildService::EXTENSION_LANGUAGE_PATH . $locallang->getFilename();
} else {
throw new \TYPO3\CMS\Core\Exception('The path defined in the extensionsettings for exportPath is not valid. It may contain illegal characters, is not in the project-scope or does not exist. Please check: ' . $customPath);
throw new Exception(
'The path defined in the extensionsettings for exportPath is not valid. It may contain illegal characters, is not in the project-scope or does not exist. Please check: ' . $customPath
);
}
return '';
}
Expand Down
Loading

0 comments on commit aa5d036

Please sign in to comment.