|
- <?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, $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)
- );
- } 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): 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 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';
- }
- }
-
- ?>
|