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
53 changes: 29 additions & 24 deletions src/Translation/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\CreateTranslationData;
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\TranslationData;
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface;
use Pimcore\Model\Translation;
Expand Down Expand Up @@ -55,25 +56,20 @@ public function getAllTranslations(string $locale): array
/**
* {@inheritdoc}
*/
public function createTranslations(string $key, string $type): void
public function createTranslations(array $translationData): void
{
$languages = $this->adminResolver->getLanguages();

if ($this->getTranslationByKey($key) !== null) {
throw new ElementExistsException(
sprintf("Translation with key '%s' already exists", $key),
);
}

$t = new Translation();
$t->setDomain(TranslatorServiceInterface::DOMAIN);
$t->setKey($key);
$t->setType($type);
$t->setCreationDate(time());
$t->setModificationDate(time());
$this->setNewValues($t, $languages);
$t->save();
/** @var CreateTranslationData $translation */
foreach ($translationData as $translation) {
if ($this->getTranslationByKey($translation->getKey()) !== null) {
throw new ElementExistsException(
sprintf("Translation with key '%s' already exists", $translation->getKey()),
);
}

$this->createTranslationEntry($translation->getKey(), $translation->getType(), $languages);
}
}

/**
Expand All @@ -86,18 +82,15 @@ public function updateTranslations(array $translationData, string $locale): void

/** @var TranslationData $translation */
foreach ($translationData as $translation) {
if ($this->getTranslationByKey($translation->getKey()) === null) {
$entry = $this->getTranslationByKey($translation->getKey());
if ($entry === null) {
throw new NotFoundException('translation', $translation->getKey(), 'key');
}

$t = new Translation();
$t->setDomain(TranslatorServiceInterface::DOMAIN);
$t->setKey($translation->getKey());
$t->setType($translation->getType());
$t->addTranslation($locale, $translation->getTranslation());
$t->setCreationDate(time());
$t->setModificationDate(time());
$t->save();
$entry->addTranslation($locale, $translation->getTranslation());
$entry->setType($translation->getType());
$entry->setModificationDate(time());
$entry->save();
}
}

Expand All @@ -111,6 +104,18 @@ public function deleteTranslation(string $key): void
$translation->delete();
}

private function createTranslationEntry(string $key, string $type, array $languages): void
{
$t = new Translation();
$t->setDomain(TranslatorServiceInterface::DOMAIN);
$t->setKey($key);
$t->setType($type);
$t->setCreationDate(time());
$t->setModificationDate(time());
$this->setNewValues($t, $languages);
$t->save();
}

private function getTranslationList(): Listing
{
$list = new Translation\Listing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getAllTranslations(string $locale): array;
/**
* @throws ElementExistsException
*/
public function createTranslations(string $key, string $type): void;
public function createTranslations(array $translationData): void;

/**
* @param array<TranslationData> $translationData
Expand Down
21 changes: 9 additions & 12 deletions src/Translation/Schema/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\StudioBackendBundle\Translation\Schema;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

Expand All @@ -26,26 +27,22 @@
schema: 'CreateTranslation',
title: 'Translation Create',
description: 'Translation Crete Scheme for API',
required: ['key', 'type'],
required: ['translationData'],
type: 'object'
)]
final readonly class CreateTranslation
{
public function __construct(
#[Property(description: 'Key', type: 'string', example: 'my_translation_key')]
private string $key,
#[Property(description: 'Type', type: 'string', example: 'simple')]
private string $type = 'simple',
#[Property(description: 'Translation Data', type: 'array', items: new Items(ref: CreateTranslationData::class))]
private array $translationData = []
) {
}

public function getKey(): string
/**
* @return array<CreateTranslationData>
*/
public function getTranslationData(): array
{
return $this->key;
}

public function getType(): string
{
return $this->type;
return $this->translationData;
}
}
51 changes: 51 additions & 0 deletions src/Translation/Schema/CreateTranslationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Translation\Schema;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/**
* @internal
*/
#[Schema(
schema: 'CreateTranslationData',
title: 'Translation Data for create',
description: 'Translation Data Scheme for create endpoint of the API',
required: ['key', 'type'],
type: 'object'
)]
final readonly class CreateTranslationData
{
public function __construct(
#[Property(description: 'Key', type: 'string', example: 'my_translation_key')]
private string $key,
#[Property(description: 'Type', type: 'string', example: 'simple')]
private string $type = 'simple',
) {
}

public function getKey(): string
{
return $this->key;
}

public function getType(): string
{
return $this->type;
}
}
8 changes: 7 additions & 1 deletion src/Translation/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ public function __construct(
$this->translatorBag = $this->getTranslatorBag();
}

/**
* {@inheritdoc}
*/
public function createTranslations(CreateTranslation $translation): void
{
$this->translationRepository->createTranslations($translation->getKey(), $translation->getType());
$this->translationRepository->createTranslations($translation->getTranslationData());
}

/**
* {@inheritdoc}
*/
public function updateTranslations(UpdateTranslation $translation): void
{
$this->translationRepository->updateTranslations($translation->getTranslationData(), $translation->getLocale());
Expand Down
Loading