|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
-
- namespace Lc\SovBundle\Translation;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use Symfony\Contracts\Translation\TranslatorInterface;
-
- class TranslatorAdmin
- {
- protected TranslatorInterface $translator;
-
- const DOMAIN = 'admin';
-
- public function __construct(TranslatorInterface $translator)
- {
- $this->translator = $translator;
- }
-
- public function transAction($action): string
- {
- return $this->trans('action.' . $action);
- }
-
- public function transMenu($menu): string
- {
- return $this->trans('menu.' . $menu);
- }
-
- public function transFlashMessage($name, $params = []): string
- {
- return $this->trans('flash_message.' . $name, $params);
- }
-
- public function transField($fieldName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdField($fieldName, $entityClass),
- $this->buildTransIdField($fieldName, $entityClass, true)
- );
- }
-
- public function transChoices(array $choices, string $entityName,string $field): array
- {
- $newChoices = [];
- foreach ($choices as $key => $choice) {
- $newChoices[$this->transField($field.'Choices.'.$choice, $entityName)] = $choice;
- }
-
- return $newChoices;
- }
-
- 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 = []): string
- {
- $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
- {
- return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default);
- }
-
- private function buildTransIdPanel($panelName, $entityClass, $default = false): string
- {
- return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default);
- }
-
- private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string
- {
- if ($default) {
- $entityName = 'default';
- } else {
- $entityName = $this->getEntityName($entityClass);
- }
-
- return 'entity.' . $entityName . '.' . $entitySection . '.' . $name;
- }
-
- public function trans($id, $params = [], $domain = self::DOMAIN): string
- {
- return $this->translator->trans($id, $params, $domain);
- }
-
- private function getEntityName($entityClass): string
- {
- if (is_object($entityClass)) {
- $entityClass = get_class($entityClass);
- }
-
- if (is_string($entityClass)) {
- $entityNameExplode = explode('\\', $entityClass);
- return $entityNameExplode[count($entityNameExplode) - 1];
- }
-
- return 'default';
- }
- }
-
- ?>
|