|
- <?php
-
- namespace Lc\SovBundle\Translation;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use Symfony\Contracts\Translation\TranslatorInterface;
-
- class TranslatorAdmin
- {
- protected $translator;
-
- const DOMAIN = 'admin';
-
- public function __construct(TranslatorInterface $translator)
- {
- $this->translator = $translator;
- }
-
- public function transField($fieldName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdField($fieldName, $entityClass),
- $this->buildTransIdField($fieldName, $entityClass, true)
- );
- }
-
- public function transHelp($fieldName, $entityClass): string
- {
- $fieldName = $fieldName . '_help';
-
- return $this->transEntityThenDefault(
- $this->buildTransIdField($fieldName, $entityClass),
- $this->buildTransIdField($fieldName, $entityClass, true),
- true
- );
- }
-
- public function transPanel($panelName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdPanel($panelName, $entityClass),
- $this->buildTransIdPanel($panelName, $entityClass, true)
- );
- }
-
- public function transTitle($pageName, $entityClass = null, $params = [])
- {
- $entityName = $this->getEntityName($entityClass);
-
- $paramsTranslation = [] ;
-
- if($entityName) {
- $baseIdEntityLabel = 'entity.' . $entityName ;
- $paramsTranslation = [
- '%label%' => $this->trans($baseIdEntityLabel . '.label'),
- '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
- ] ;
- }
-
- if (isset($params['id'])) {
- $paramsTranslation['%id%'] = $params['id'];
- }
-
- return $this->trans(
- 'title.' . $pageName,
- $paramsTranslation
- );
- }
-
- private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string
- {
- $translation = $this->trans($idTranslationEntity);
-
- if ($translation == $idTranslationEntity) {
- $translation = $this->trans($idTranslationDefault);
-
- if ($translation == $idTranslationDefault) {
- if ($returnEmpty) {
- $translation = '';
- } else {
- $translation = $idTranslationEntity;
- }
- }
- }
-
- return $translation;
- }
-
- private function buildTransIdField($fieldName, $entityClass, $default = false): string
- {
- if ($default) {
- $entityName = 'default';
- } else {
- $entityName = $this->getEntityName($entityClass);
- }
-
- return 'entity.' . $entityName . '.fields.' . $fieldName;
- }
-
- private function buildTransIdPanel($panelName, $entityClass, $default = false): string
- {
- if ($default) {
- $entityName = 'default';
- } else {
- $entityName = $this->getEntityName($entityClass);
- }
-
-
-
- return 'entity.' . $entityName . '.panels.' . $panelName;
- }
-
- private function trans($id, $params = [], $domain = self::DOMAIN): string
- {
- return $this->translator->trans($id, $params, $domain);
- }
-
- private function getEntityName($entityClass): string
- {
- $entityNameExplode = explode('\\', $entityClass);
- return strtolower($entityNameExplode[count($entityNameExplode) - 1]);
- }
- }
-
- ?>
|