Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Allow setting the translation domain globally and per-admin (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gremo authored and kunicmarko20 committed Feb 3, 2019
1 parent 2d66b5b commit 2247098
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -45,6 +45,7 @@ sonata_auto_configure:
manager_type: orm
label_catalogue: ~
label_translator_strategy: ~
translation_domain: ~
group: ~
pager_type: ~
entity:
Expand Down Expand Up @@ -140,6 +141,7 @@ use App\Entity\Category;
* icon="<i class='fa fa-user'></i>",
* labelTranslatorStrategy="sonata.admin.label.strategy.native",
* labelCatalogue="App",
* translationDomain="messages",
* pagerType="simple",
* controller=CategoryController::class,
* entity=Category::class,
Expand Down
5 changes: 5 additions & 0 deletions src/Annotation/AdminOptions.php
Expand Up @@ -62,6 +62,11 @@ final class AdminOptions
*/
public $labelCatalogue;

/**
* @var string
*/
public $translationDomain;

/**
* @var string
*/
Expand Down
Expand Up @@ -57,6 +57,8 @@ public function process(ContainerBuilder $container): void
->getParameter('sonata.auto_configure.admin.label_catalogue');
$annotationDefaults['label_translator_strategy'] = $container
->getParameter('sonata.auto_configure.admin.label_translator_strategy');
$annotationDefaults['translation_domain'] = $container
->getParameter('sonata.auto_configure.admin.translation_domain');
$annotationDefaults['group'] = $container->getParameter('sonata.auto_configure.admin.group');
$annotationDefaults['pager_type'] = $container->getParameter('sonata.auto_configure.admin.pager_type');

Expand Down Expand Up @@ -84,7 +86,7 @@ public function process(ContainerBuilder $container): void
$this->setDefaultValuesForAnnotation($annotation, $name, $annotationDefaults);

$container->removeDefinition($id);
$container->setDefinition(
$definition = $container->setDefinition(
$annotation->adminCode ?? $id,
(new Definition($adminClass))
->addTag('sonata.admin', $annotation->getOptions())
Expand All @@ -96,6 +98,10 @@ public function process(ContainerBuilder $container): void
->setAutoconfigured(true)
->setAutowired(true)
);

if ($annotation->translationDomain) {
$definition->addMethodCall('setTranslationDomain', [$annotation->translationDomain]);
}
}
}

Expand All @@ -113,6 +119,10 @@ private function setDefaultValuesForAnnotation(AdminOptions $annotation, string
$annotation->labelTranslatorStrategy = $defaults['label_translator_strategy'];
}

if (!$annotation->translationDomain) {
$annotation->translationDomain = $defaults['translation_domain'];
}

if (!$annotation->group) {
$annotation->group = $defaults['group'];
}
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -41,6 +41,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('label_translator_strategy')
->defaultNull()
->end()
->scalarNode('translation_domain')
->defaultNull()
->end()
->scalarNode('group')
->defaultNull()
->end()
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/SonataAutoConfigureExtension.php
Expand Up @@ -26,6 +26,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
'sonata.auto_configure.admin.label_translator_strategy',
$mergedConfig['admin']['label_translator_strategy']
);
$container->setParameter(
'sonata.auto_configure.admin.translation_domain',
$mergedConfig['admin']['translation_domain']
);
$container->setParameter('sonata.auto_configure.admin.group', $mergedConfig['admin']['group']);
$container->setParameter('sonata.auto_configure.admin.pager_type', $mergedConfig['admin']['pager_type']);

Expand Down
Expand Up @@ -25,6 +25,11 @@ public function testParametersInContainer(): void
'sonata.auto_configure.admin.label_translator_strategy',
null
);

$this->assertContainerBuilderHasParameter(
'sonata.auto_configure.admin.translation_domain',
null
);
}

protected function getContainerExtensions(): array
Expand Down

0 comments on commit 2247098

Please sign in to comment.