Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions Classes/ContentRepository/NodeTranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Neos\Neos\Utility\NodeUriPathSegmentGenerator;
use Sitegeist\LostInTranslation\Domain\TranslatableProperty\TranslatablePropertyNamesFactory;
use Sitegeist\LostInTranslation\Domain\TranslationServiceInterface;
use Sitegeist\LostInTranslation\Utility\ArrayFlatteningUtility;

/**
* @Flow\Scope("singleton")
Expand Down Expand Up @@ -287,22 +288,33 @@ public function translateNode(NodeInterface $sourceNode, NodeInterface $targetNo
/** @phpstan-ignore arguments.count */
$properties = (array)$sourceNode->getProperties(true);
$propertiesToTranslate = [];

foreach ($properties as $propertyName => $propertyValue) {
if (empty($propertyValue) || !is_string($propertyValue)) {
if (empty($propertyValue)) {
continue;
}
assert($propertyName !== '');
assert($propertyValue !== '');
if (!$translatableProperties->isTranslatable($propertyName)) {
continue;
}
if ((trim(strip_tags($propertyValue))) == "") {
if (is_string($propertyValue) && trim(strip_tags($propertyValue)) === "") {
continue;
}
$propertiesToTranslate[$propertyName] = $propertyValue;
unset($properties[$propertyName]);
if ($connector = $translatableProperties->getTranslationObjectConnector($propertyName)) {
$propertiesToTranslate[$propertyName] = $connector->extractTranslations($propertyValue);
unset($properties[$propertyName]);
} else {
$propertiesToTranslate[$propertyName] = $propertyValue;
unset($properties[$propertyName]);
}
}

if (count($propertiesToTranslate) > 0) {
$translatedProperties = $this->translationService->translate($propertiesToTranslate, $targetLanguage, $sourceLanguage);
$propertiesToTranslateDeflated = ArrayFlatteningUtility::deflate($propertiesToTranslate);
/** @var array<non-empty-string, string> $translatedPropertiesDeflated */
$translatedPropertiesDeflated = $this->translationService->translate($propertiesToTranslateDeflated, $targetLanguage, $sourceLanguage);
$translatedProperties = ArrayFlatteningUtility::enflate($translatedPropertiesDeflated);
$properties = array_merge($translatedProperties, $properties);
}

Expand All @@ -311,9 +323,18 @@ public function translateNode(NodeInterface $sourceNode, NodeInterface $targetNo
if ($propertyName === 'uriPathSegment' && !preg_match('/^[a-z0-9\-]+$/i', $propertyValue)) {
$propertyValue = $this->nodeUriPathSegmentGenerator->generateUriPathSegment(null, $propertyValue);
}

if ($targetNode->getProperty($propertyName) !== $propertyValue) {
$targetNode->setProperty($propertyName, $propertyValue);
if (is_array($propertyValue)) {
$targetValue = $targetNode->getProperty($propertyName);
if ($connector = $translatableProperties->getTranslationObjectConnector($propertyName)) {
if (is_object($targetValue)) {
$targetValue = $connector->applyTranslations($targetValue, $propertyValue);
}
}
} else {
$targetValue = $propertyValue;
}
if ($targetNode->getProperty($propertyName) !== $targetValue) {
$targetNode->setProperty($propertyName, $targetValue);
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion Classes/Domain/TranslatableProperty/TranslatablePropertyName.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@

namespace Sitegeist\LostInTranslation\Domain\TranslatableProperty;

use Sitegeist\LostInTranslation\Domain\TranslationConnectorInterface;

class TranslatablePropertyName
{
/**
* @var string
*/
protected $name;

public function __construct(string $name)
/**
* @var TranslationConnectorInterface<object>|null
*/
protected $translationConnector;

/**
* @param string $name
* @param TranslationConnectorInterface<object>|null $translationConnector
*/
public function __construct(string $name, ?TranslationConnectorInterface $translationConnector = null)
{
$this->name = $name;
$this->translationConnector = $translationConnector;
}

public function getName(): string
{
return $this->name;
}

/**
* @return TranslationConnectorInterface<object>|null
*/
public function getTranslationConnector(): ?TranslationConnectorInterface
{
return $this->translationConnector;
}
}
16 changes: 16 additions & 0 deletions Classes/Domain/TranslatableProperty/TranslatablePropertyNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Sitegeist\LostInTranslation\Domain\TranslatableProperty;

use Sitegeist\LostInTranslation\Domain\TranslationConnectorInterface;

/**
* @implements \IteratorAggregate<int, TranslatablePropertyName>
*/
Expand All @@ -28,6 +30,20 @@ public function isTranslatable(string $propertyName): bool
return false;
}

/**
* @param string $propertyName
* @return TranslationConnectorInterface<object>|null
*/
public function getTranslationObjectConnector(string $propertyName): ?TranslationConnectorInterface
{
foreach ($this->translatableProperties as $translatableProperty) {
if ($translatableProperty->getName() == $propertyName) {
return $translatableProperty->getTranslationConnector();
}
}
return null;
}

/**
* @return \ArrayIterator<int, TranslatablePropertyName>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeType;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Sitegeist\LostInTranslation\Domain\TranslationConnectorInterface;

class TranslatablePropertyNamesFactory
{
Expand All @@ -15,6 +17,24 @@ class TranslatablePropertyNamesFactory
*/
protected $translateInlineEditables;

/**
* @var bool
* @Flow\InjectConfiguration(path="nodeTranslation.translateTypesWithConnectors")
*/
protected $translateTypesWithConnectors;

/**
* @Flow\InjectConfiguration(path="nodeTranslation.translationConnectors")
* @var array<class-string, class-string>
*/
protected $translationConnectors;

/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;

/**
* @var array<string, TranslatablePropertyNames>
*/
Expand All @@ -28,20 +48,28 @@ public function createForNodeType(NodeType $nodeType): TranslatablePropertyNames
$propertyDefinitions = $nodeType->getProperties();
$translateProperties = [];
foreach ($propertyDefinitions as $propertyName => $propertyDefinition) {
if (array_key_exists('type', $propertyDefinition) && $propertyDefinition['type'] !== 'string') {
$type = $propertyDefinition['type'];

// @deprecated Fallback for renamed setting translateOnAdoption -> automaticTranslation
$automaticTranslationIsEnabled = $propertyDefinition[ 'options' ][ 'automaticTranslation' ]
?? ($propertyDefinition[ 'options' ][ 'translateOnAdoption' ] ?? null);
$isInlineEditable = $propertyDefinition['ui']['inlineEditable']
?? false;
$translationConnector = $this->translationConnectors[$type]
?? null;

if ($automaticTranslationIsEnabled === false) {
continue;
}
if (isset($propertyDefinition['options']['automaticTranslation']) && !$propertyDefinition['options']['automaticTranslation']) {
continue; // do not translate (inline-editable) properties explicitly set to: 'automaticTranslation: false'
}
if ($this->translateInlineEditables && ($propertyDefinitions[$propertyName]['ui']['inlineEditable'] ?? false)) {

if ($type === "string" && $this->translateInlineEditables && $isInlineEditable) {
$translateProperties[] = new TranslatablePropertyName($propertyName);
continue;
}
// @deprecated Fallback for renamed setting translateOnAdoption -> automaticTranslation
if ($propertyDefinition['options']['automaticTranslation'] ?? ($propertyDefinition['options']['translateOnAdoption'] ?? false)) {
} elseif ($type === "string" && $automaticTranslationIsEnabled === true) {
$translateProperties[] = new TranslatablePropertyName($propertyName);
continue;
} elseif ($translationConnector && ($this->translateTypesWithConnectors || $automaticTranslationIsEnabled)) {
$translationConnectorInstance = $this->objectManager->get($translationConnector);
assert($translationConnectorInstance instanceof TranslationConnectorInterface);
$translateProperties[] = new TranslatablePropertyName($propertyName, $translationConnectorInstance);
}
}
$this->firstLevelCache[$nodeType->getName()] = new TranslatablePropertyNames(...$translateProperties);
Expand Down
24 changes: 24 additions & 0 deletions Classes/Domain/TranslationConnectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Sitegeist\LostInTranslation\Domain;

/**
* @template T of object
*/
interface TranslationConnectorInterface
{
/**
* @param T $object
* @return array<non-empty-string, string>
*/
public function extractTranslations(object $object): array;

/**
* @param T $object
* @param array<non-empty-string, string> $translations
* @return T
*/
public function applyTranslations(object $object, array $translations): object;
}
59 changes: 59 additions & 0 deletions Classes/Utility/ArrayFlatteningUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Sitegeist\LostInTranslation\Utility;

/**
* Utility to deflate and enflate an array of named strings that may be nested by using a seperator
* that must never be used in the array keys
*/
class ArrayFlatteningUtility
{
private const SEPERATOR = '.';

/**
* @param array<non-empty-string, string|array<non-empty-string, string>> $array to deflate
* @return array<non-empty-string, string>
*/
public static function deflate(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
assert($key !== '');
if (is_string($value)) {
$result[$key] = $value;
} elseif (is_array($value)) {
foreach ($value as $subkey => $subvalue) {
$result[$key . self::SEPERATOR . $subkey] = $subvalue;
}
}
}
return $result;
}

/**
* @param array<non-empty-string, string> $array to enflate
* @return array<non-empty-string, string|array<non-empty-string, string>>
*/
public static function enflate(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
assert($key !== '');
if (str_contains($key, self::SEPERATOR)) {
list($mainKey, $subKey) = explode(self::SEPERATOR, $key, 2);
assert($mainKey !== '');
assert($subKey !== '');
if (array_key_exists($mainKey, $result) && is_array($result[$mainKey])) {
$result[$mainKey][$subKey] = $value;
} else {
$result[$mainKey] = [$subKey => $value];
}
} else {
$result[$key] = $value;
}
}
return $result;
}
}
14 changes: 14 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ Sitegeist:
skipAuthorizationChecks: false

excludedNodePaths: []

#
# Translate all object properties that have a translationConnector configured
# if this is set to false each property must be enabled via options.automaticTranslation
#
translateTypesWithConnectors: true

#
# Connectors to translate value object properties
#
# for each value object type a clas implementing the TranslationConnectorInterface
# can be configured to extract and apply translations
#
translationConnectors: []
Loading