|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?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, $params=[]): string
- {
- return $this->trans('action.' . $action, $params);
- }
-
- public function transMenu($menu, $params = []): string
- {
- return $this->trans('menu.' . $menu, $params);
- }
-
- public function transFlashMessage($type, $name, $entityClass = false, $params = []): string
- {
- if ($entityClass) {
- return $this->transEntityThenDefault(
- $this->buildTransIdFlash($type . '.' . $name, $entityClass),
- $this->buildTransIdFlash($type . '.' . $name, $entityClass, true),
- false,
- $params
- );
- } else {
- 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 transFieldIndex($fieldName, $entityClass): string
- {
- $idTranslationFieldIndex = $this->buildTransIdField($fieldName . 'Index', $entityClass);
-
- $translation = $this->trans($idTranslationFieldIndex);
-
- if ($translation == $idTranslationFieldIndex) {
- return $this->transField($fieldName, $entityClass);
- } else {
- return $translation;
- }
- }
-
- 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 transChoice(string $entityName, string $field, string $choice): string
- {
- return $this->transField($field . 'Choices.' . $choice, $entityName);
- }
-
- 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 transModal($modalName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdModal($modalName, $entityClass),
- $this->buildTransIdModal($modalName, $entityClass, true)
- );
- }
-
- public function transCard($cardName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdCard($cardName, $entityClass),
- $this->buildTransIdCard($cardName, $entityClass, true)
- );
- }
-
- public function transBox($cardName, $entityClass): string
- {
- return $this->transEntityThenDefault(
- $this->buildTransIdBox($cardName, $entityClass),
- $this->buildTransIdBox($cardName, $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, $params = []): string
- {
-
- $translation = $this->trans($idTranslationEntity, $params);
-
- if ($translation == $idTranslationEntity) {
- $translation = $this->trans($idTranslationDefault, $params);
-
- 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 buildTransIdModal($modalName, $entityClass, $default = false): string
- {
- return $this->buildTransIdEntitySection($modalName, $entityClass, 'modals', $default);
- }
-
- private function buildTransIdCard($cardName, $entityClass, $default = false): string
- {
- return $this->buildTransIdEntitySection($cardName, $entityClass, 'cards', $default);
- }
-
- private function buildTransIdBox($boxName, $entityClass, $default = false): string
- {
- return $this->buildTransIdEntitySection($boxName, $entityClass, 'boxes', $default);
- }
-
- private function buildTransIdFlash($flashName, $entityClass, $default = false): string
- {
- return $this->buildTransIdEntitySection($flashName, $entityClass, 'flashes', $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';
- }
- }
-
- ?>
|