Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ composer.lock
*.example
.phpunit.result.cache
Configuration/README
node_modules
16 changes: 16 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true,
"bracketSpacing": true,
"arrowParens": "always",
"printWidth": 120,
"useTabs": false,
"proseWrap": "always",
"endOfLine": "auto",
"importOrder": ["^@/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
139 changes: 139 additions & 0 deletions Classes/ContentRepository/RetranslationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare(strict_types=1);

namespace Sitegeist\LostInTranslation\ContentRepository;

use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\Service\ContentContext;
use Neos\Neos\Domain\Service\ContentContextFactory;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;

/**
* @Flow\Scope("singleton")
*/
class RetranslationService
{
/**
* @Flow\InjectConfiguration(path="nodeTranslation.languageDimensionName")
* @var string
*/
protected $languageDimensionName;

public function __construct(
protected readonly ContentDimensionPresetSourceInterface $contentDimensionPresetSource,
protected readonly ContentContextFactory $contentContextFactory,
protected readonly NodeTranslationService $nodeTranslationService,
) {
}

public function findFirstUpdateDateOnNodeOrDescendants(
Node $sourceNode,
ContentContext $sourceContext,
ContentContext $targetContext
): ?\DateTimeInterface {
/** @var ?Node $targetNode */
$targetNode = $targetContext->getNodeByIdentifier($sourceNode->getIdentifier());
$sourceReferenceDate = $sourceNode->getLastModificationDateTime();
if (!$targetNode) {
return $sourceReferenceDate;
}
$targetReferenceDate = $targetNode->getLastModificationDateTime();
if ($targetReferenceDate < $sourceReferenceDate) {
return $sourceReferenceDate;
}

foreach ($sourceNode->getChildNodes('Neos.Neos:Content,Neos.Neos:ContentCollection') as $sourceChildNode) {
/** @var Node $sourceChildNode */
$updateDate = $this->findFirstUpdateDateOnNodeOrDescendants($sourceChildNode, $sourceContext, $targetContext);
if ($updateDate) {
return $updateDate;
}
}

return null;
}

/**
* @param array<string,string> $targetCoordinates
*/
public function retranslateNode(
string $nodeAggregateId,
string $workspaceName,
array $targetCoordinates,
): void {
$sourceContentContext = $this->getReferenceContentContext($workspaceName, $targetCoordinates);

$sourceNode = $sourceContentContext->getNodeByIdentifier($nodeAggregateId);
if (!$sourceNode) {
throw new \Exception('No source node found in workspace and dimension space point');
}

$targetContentContext = $this->getContentContext($workspaceName, $targetCoordinates, true);
$targetNode = $targetContentContext->getNodeByIdentifier($nodeAggregateId);
if (!$targetNode) {
// translation will be done implicitly here
$targetContentContext->adoptNode($sourceNode);
}

$this->translateDescendants($sourceNode, $targetContentContext);
}

private function translateDescendants(NodeInterface $node, ContentContext $targetContentContext): void
{
$targetNode = $targetContentContext->getNodeByIdentifier($node->getIdentifier());
if (!$targetNode) {
// translation will be done implicitly here
$targetContentContext->adoptNode($node);
} else {
/** @var Node $node */
/** @var Node $targetNode */
if ($targetNode->getLastModificationDateTime() < $node->getLastModificationDateTime()) {
$this->nodeTranslationService->translateNode($node, $targetNode, $targetContentContext);
}
}
foreach ($node->getChildNodes('Neos.Neos:Content,Neos.Neos:ContentCollection') as $sourceChildNode) {
$this->translateDescendants($sourceChildNode, $targetContentContext);
}
}

/**
* @param array<string,string> $coordinates
*/
public function getContentContext(string $workspaceName, array $coordinates, bool $withRemoved): ContentContext
{
$dimensions = [];
foreach ($coordinates as $dimensionName => $dimensionValue) {
$dimensions[$dimensionName] = $this->contentDimensionPresetSource->getAllPresets()[$dimensionName]['presets'][$dimensionValue]['values'];
}

/** @var ContentContext $contentContext */
$contentContext = $this->contentContextFactory->create([
'workspaceName' => $workspaceName,
'dimensions' => $dimensions,
'targetDimensions' => $coordinates,
'invisibleContentShown' => true,
'removedContentShown' => $withRemoved,
]);

return $contentContext;
}

/**
* @param array<string,string> $coordinates
*/
public function getReferenceContentContext(string $workspaceName, array $coordinates): ContentContext
{
$targetLanguagePreset = $this->contentDimensionPresetSource->getAllPresets()[$this->languageDimensionName]['presets'][$coordinates[$this->languageDimensionName]];
$referenceLanguage = $targetLanguagePreset['options']['referenceLanguage'] ?? null;
if ($referenceLanguage === null) {
throw new \Exception('No reference language configured for target language ' . $coordinates[$this->languageDimensionName]);
}
$referenceCoordinates = $coordinates;
$referenceCoordinates[$this->languageDimensionName] = $referenceLanguage;

return $this->getContentContext($workspaceName, $referenceCoordinates, false);
}
}
87 changes: 87 additions & 0 deletions Classes/Controller/RetranslationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Sitegeist\LostInTranslation\Controller;

use Neos\ContentRepository\Domain\Model\Node;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
use Sitegeist\LostInTranslation\ContentRepository\RetranslationService;

class RetranslationController extends ActionController
{
/**
* @Flow\InjectConfiguration(path="nodeTranslation.languageDimensionName")
* @var string
*/
protected $languageDimensionName;

public function __construct(
protected readonly ContentDimensionPresetSourceInterface $contentDimensionPresetSource,
protected readonly RetranslationService $retranslationService,
) {
}

public function getTranslationMetadataAction(string $nodeAggregateId, string $workspaceName, string $coordinates): string
{
$targetCoordinates = \json_decode($coordinates, true);
$targetLanguagePreset = $this->contentDimensionPresetSource->getAllPresets()[$this->languageDimensionName]['presets'][$targetCoordinates[$this->languageDimensionName]];
$sourceLanguage = $targetLanguagePreset['options']['referenceLanguage'] ?? null;

$targetContentContext = $this->retranslationService->getContentContext($workspaceName, $targetCoordinates, false);
$targetNode = $targetContentContext->getNodeByIdentifier($nodeAggregateId);
if (!$targetNode instanceof Node) {
throw new \Exception('Node not found in workspace and dimension space point');
}

$sourceUpdateDate = null;
$sourceLanguagePreset = null;
if ($sourceLanguage) {
$sourceContentContext = $this->retranslationService->getReferenceContentContext($workspaceName, $targetCoordinates);
/** @var ?Node $sourceNode */
$sourceNode = $sourceContentContext->getNodeByIdentifier($nodeAggregateId);
if ($sourceNode instanceof Node) {
$sourceUpdateDate = $this->retranslationService->findFirstUpdateDateOnNodeOrDescendants(
$sourceNode,
$sourceContentContext,
$targetContentContext
);
}
$sourceLanguagePreset = $this->contentDimensionPresetSource->getAllPresets()[$this->languageDimensionName]['presets'][$sourceContentContext->getTargetDimensions()[$this->languageDimensionName]];
}

/** otherwise, getNodeByIdentifier might register a new object ¯\_(ツ)_/¯ */
$this->persistenceManager->clearState();

return \json_encode(
[
'isUpToDate' => $sourceUpdateDate === null,
'referenceLanguage' => $sourceLanguage
? [
'label' => $sourceLanguagePreset ? $sourceLanguagePreset['label'] : null,
'dateModified' => $sourceUpdateDate?->format(\DateTime::ATOM),
]
: null,
],
JSON_THROW_ON_ERROR,
);
}

public function retranslateNodeAction(
string $nodeAggregateId,
string $workspaceName,
string $targetCoordinates,
): string {
$targetCoordinates = \json_decode($targetCoordinates, true);
$this->retranslationService->retranslateNode($nodeAggregateId, $workspaceName, $targetCoordinates);

return \json_encode(
[
'message' => 'Successfully translated'
],
JSON_THROW_ON_ERROR,
);
}
}
28 changes: 26 additions & 2 deletions Configuration/NodeTypes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
'Neos.Neos:Node':
options:
automaticTranslation: true
ui:
inspector:
groups:
lostInTranslation:
label: 'Sitegeist.LostInTranslation:NodeTypes.Node:groups.lostInTranslation'
icon: language
tab: default
views:
retranslate:
label: 'Sitegeist.LostInTranslation:NodeTypes.Node:views.retranslate'
group: lostInTranslation
position: end
view: 'Sitegeist.LostInTranslation/Inspector/Views/RetranslateNodeView'

'Neos.Neos:Document':
properties:
Expand All @@ -16,5 +29,16 @@
metaKeywords:
options:
automaticTranslation: true


ui:
inspector:
groups:
lostInTranslation:
label: 'Sitegeist.LostInTranslation:NodeTypes.Document:groups.lostInTranslation'
icon: language
tab: default
views:
retranslate:
label: 'Sitegeist.LostInTranslation:NodeTypes.Document:views.retranslate'
group: lostInTranslation
position: end
view: 'Sitegeist.LostInTranslation/Inspector/Views/RetranslateDocumentView'
8 changes: 8 additions & 0 deletions Configuration/Policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ privilegeTargets:
'Neos\Flow\Security\Authorization\Privilege\Method\MethodPrivilege':
'Sitegeist.LostInTranslation:AccessBackendModule':
matcher: 'method(Sitegeist\LostInTranslation\Controller\LostInTranslationModuleController->(index)Action())'
'Sitegeist.LostInTranslation:Retranslation':
matcher: 'method(Sitegeist\LostInTranslation\Controller\RetranslationController->(.*)Action())'
'Neos\Neos\Security\Authorization\Privilege\ModulePrivilege':
'Sitegeist.LostInTranslation:AccessBackendModule':
matcher: 'management/sitegeist_lostintranslation'
Expand All @@ -12,9 +14,15 @@ roles:
-
privilegeTarget: 'Sitegeist.LostInTranslation:AccessBackendModule'
permission: GRANT
-
privilegeTarget: 'Sitegeist.LostInTranslation:Retranslation'
permission: GRANT

'Neos.Neos:AbstractEditor':
privileges:
-
privilegeTarget: 'Sitegeist.LostInTranslation:AccessBackendModule'
permission: GRANT
-
privilegeTarget: 'Sitegeist.LostInTranslation:Retranslation'
permission: GRANT
17 changes: 17 additions & 0 deletions Configuration/Routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-
name: 'LostInTranslation - Translation Metadata'
uriPattern: 'lostintranslation/retranslation/getmetadata'
defaults:
'@package': 'Sitegeist.LostInTranslation'
'@controller': 'Retranslation'
'@action': 'getTranslationMetadata'
httpMethods: ['GET']

-
name: 'LostInTranslation - Retranslation'
uriPattern: 'lostintranslation/retranslation/retranslatenode'
defaults:
'@package': 'Sitegeist.LostInTranslation'
'@controller': 'Retranslation'
'@action': 'retranslateNode'
httpMethods: ['POST']
8 changes: 8 additions & 0 deletions Configuration/Settings.Neos.Ui.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Neos:
Neos:
userInterface:
translation:
autoInclude:
'Sitegeist.LostInTranslation':
- Main
- 'NodeTypes/*'
Ui:
changes:
types:
Expand All @@ -10,3 +16,5 @@ Neos:
javascript:
'Sitegeist.LostInTranslation:HostFramePlugin':
resource: 'resource://Sitegeist.LostInTranslation/Public/Scripts/HostFrame/Plugin.js'
'Sitegeist.LostInTranslation:InspectorViewPlugin':
resource: 'resource://Sitegeist.LostInTranslation/Public/JavaScript/Plugin.js'
16 changes: 16 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ Sitegeist:
# can be configured to extract and apply translations
#
translationConnectors: []

Neos:
Flow:
mvc:
routes:
Sitegeist.LostInTranslation:
position: "before Neos.Neos"
security:
authentication:
providers:
'Neos.Neos:Backend':
requestPatterns:
'Sitegeist.LostInTranslation:Retranslation':
pattern: 'ControllerObjectName'
patternOptions:
controllerObjectNamePattern: 'Sitegeist\LostInTranslation\Controller\RetranslationController'
10 changes: 10 additions & 0 deletions Neos.Ui/neos-bridge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@sitegeist/lostintranslation-neos-bridge",
"license": "GPL-3.0-or-later",
"version": "",
"main": "./src/index.ts",
"dependencies": {
"react-use": "^17.2.4",
"ts-toolbelt": "^9.6.0"
}
}
15 changes: 15 additions & 0 deletions Neos.Ui/neos-bridge/src/GlobalRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useNeos} from './NeosContext';

export interface IGlobalRegistry {
get(key: string): {
get: <T>(key: string) => T
getAllAsList: <T>() => T[]
set(key: string, value: any): void
} | undefined
set(key: string, value: any): void
}

export function useGlobalRegistry(): IGlobalRegistry {
const neos = useNeos();
return neos.globalRegistry;
}
Loading
Loading