Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom translation domain #17

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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_ui
97 changes: 97 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,108 @@

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\Extension\Bundle\Installer\Exception\InstallationException;
use Pimcore\Extension\Bundle\Installer\SettingsStoreAwareInstaller;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

/**
* @internal
*/
final class Installer extends SettingsStoreAwareInstaller
{
public const TRANSLATION_DOMAIN = 'studio_ui';
Corepex marked this conversation as resolved.
Show resolved Hide resolved

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_' . self::TRANSLATION_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));
}
}
}
3 changes: 2 additions & 1 deletion src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use InvalidArgumentException;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;
use Pimcore\Bundle\StudioApiBundle\Installer;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -28,7 +29,7 @@ final class TranslatorService implements TranslatorServiceInterface
{
private TranslatorBagInterface $translator;

private const DOMAIN = 'admin';
private const DOMAIN = Installer::TRANSLATION_DOMAIN;
Corepex marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(
TranslatorInterface $translator
Expand Down