Skip to content

Commit

Permalink
Custom translation domain (#17)
Browse files Browse the repository at this point in the history
* added custom translation domain `studioUi`, changed translation domain for API

* changed translation domain name, added translation domain to config

* Apply php-cs-fixer changes

* changed translation domain, removed domain const from installer

* Apply php-cs-fixer changes

* changed translation domain also in settings

---------

Co-authored-by: Corepex <[email protected]>
  • Loading branch information
Corepex and Corepex authored Mar 13, 2024
1 parent e83cd27 commit 3c3359e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/pimcore/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ api_platform:
api_keys:
access_token:
name: 'Authorization'
type: 'header'
type: 'header'

pimcore:
translations:
domains:
- studio
96 changes: 96 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,107 @@

namespace Pimcore\Bundle\StudioApiBundle;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Pimcore\Bundle\StudioApiBundle\Service\TranslatorService;
use Pimcore\Extension\Bundle\Installer\Exception\InstallationException;
use Pimcore\Extension\Bundle\Installer\SettingsStoreAwareInstaller;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

/**
* @internal
*/
final class Installer extends SettingsStoreAwareInstaller
{
public function __construct(
private readonly Connection $db,
BundleInterface $bundle,
) {
parent::__construct($bundle);
}

/**
* @throws SchemaException|Exception
*/
public function install(): void
{
$schema = $this->db->createSchemaManager()->introspectSchema();

$this->createTranslationTable($schema);
$this->executeDiffSql($schema);

parent::install();
}

private function createTranslationTable(Schema $schema): void
{
$translationsDomainTableName = 'translations_' . TranslatorService::DOMAIN;
if (!$schema->hasTable($translationsDomainTableName)) {
$translationDomainTable = $schema->createTable($translationsDomainTableName);

$translationDomainTable->addColumn('key', 'string', [
'notnull' => true,
'length' => 190,
]);

$translationDomainTable->addColumn('type', 'string', [
'notnull' => true,
'length' => 15,
]);

$translationDomainTable->addColumn('language', 'string', [
'notnull' => true,
'length' => 10,
]);

$translationDomainTable->addColumn('text', 'text');

$translationDomainTable->addColumn('creationDate', 'integer', [
'unsigned' => true,
'length' => 11,
]);

$translationDomainTable->addColumn('modificationDate', 'integer', [
'unsigned' => true,
'length' => 11,
]);

$translationDomainTable->addColumn('userOwner', 'integer', [
'unsigned' => true,
'length' => 11,
]);

$translationDomainTable->addColumn('userModification', 'integer', [
'unsigned' => true,
'length' => 11,
]);

$translationDomainTable->setPrimaryKey(['key', 'language'], 'pk_translation');
$translationDomainTable->addIndex(['language'], 'idx_language');
}
}

/**
* @throws Exception
*/
private function executeDiffSql(Schema $newSchema): void
{
$currentSchema = $this->db->createSchemaManager()->introspectSchema();
$schemaComparator = new Comparator($this->db->getDatabasePlatform());
$schemaDiff = $schemaComparator->compareSchemas($currentSchema, $newSchema);
$dbPlatform = $this->db->getDatabasePlatform();
if (!$dbPlatform instanceof AbstractPlatform) {
throw new InstallationException('Could not get database platform.');
}

$sqlStatements = $dbPlatform->getAlterSchemaSQL($schemaDiff);

if (!empty($sqlStatements)) {
$this->db->executeStatement(implode(';', $sqlStatements));
}
}
}
2 changes: 1 addition & 1 deletion src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class TranslatorService implements TranslatorServiceInterface
{
private TranslatorBagInterface $translator;

private const DOMAIN = 'admin';
public const DOMAIN = 'studio';

public function __construct(
TranslatorInterface $translator
Expand Down

0 comments on commit 3c3359e

Please sign in to comment.