You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 line
5.9KB

  1. <?php
  2. namespace Lc\SovBundle\Translation;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  4. use Symfony\Contracts\Translation\TranslatorInterface;
  5. class TranslatorAdmin
  6. {
  7. protected TranslatorInterface $translator;
  8. const DOMAIN = 'admin';
  9. public function __construct(TranslatorInterface $translator)
  10. {
  11. $this->translator = $translator;
  12. }
  13. public function transAction($action): string
  14. {
  15. return $this->trans('action.' . $action);
  16. }
  17. public function transMenu($menu): string
  18. {
  19. return $this->trans('menu.' . $menu);
  20. }
  21. public function transFlashMessage($name, $params = []): string
  22. {
  23. return $this->trans('flash_message.' . $name, $params);
  24. }
  25. public function transField($fieldName, $entityClass): string
  26. {
  27. return $this->transEntityThenDefault(
  28. $this->buildTransIdField($fieldName, $entityClass),
  29. $this->buildTransIdField($fieldName, $entityClass, true)
  30. );
  31. }
  32. public function transChoices(array $choices, string $entityName,string $field): array
  33. {
  34. $newChoices = [];
  35. foreach ($choices as $key => $choice) {
  36. $newChoices[$this->transField($field.'Choices.'.$choice, $entityName)] = $choice;
  37. }
  38. return $newChoices;
  39. }
  40. public function transChoice(string $entityName,string $field, string $choice): string
  41. {
  42. return $this->transField($field.'Choices.'.$choice, $entityName);
  43. }
  44. public function transHelp($fieldName, $entityClass): string
  45. {
  46. $fieldName = $fieldName . '_help';
  47. return $this->transEntityThenDefault(
  48. $this->buildTransIdField($fieldName, $entityClass),
  49. $this->buildTransIdField($fieldName, $entityClass, true),
  50. true
  51. );
  52. }
  53. public function transPanel($panelName, $entityClass): string
  54. {
  55. return $this->transEntityThenDefault(
  56. $this->buildTransIdPanel($panelName, $entityClass),
  57. $this->buildTransIdPanel($panelName, $entityClass, true)
  58. );
  59. }
  60. public function transModal($modalName, $entityClass): string
  61. {
  62. return $this->transEntityThenDefault(
  63. $this->buildTransIdModal($modalName, $entityClass),
  64. $this->buildTransIdModal($modalName, $entityClass, true)
  65. );
  66. }
  67. public function transCard($cardName, $entityClass): string
  68. {
  69. return $this->transEntityThenDefault(
  70. $this->buildTransIdCard($cardName, $entityClass),
  71. $this->buildTransIdCard($cardName, $entityClass, true)
  72. );
  73. }
  74. public function transBox($cardName, $entityClass): string
  75. {
  76. return $this->transEntityThenDefault(
  77. $this->buildTransIdBox($cardName, $entityClass),
  78. $this->buildTransIdBox($cardName, $entityClass, true)
  79. );
  80. }
  81. public function transTitle($pageName, $entityClass = null, $params = []): string
  82. {
  83. $entityName = $this->getEntityName($entityClass);
  84. $paramsTranslation = [];
  85. if ($entityName) {
  86. $baseIdEntityLabel = 'entity.' . $entityName;
  87. $paramsTranslation = [
  88. '%label%' => $this->trans($baseIdEntityLabel . '.label'),
  89. '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
  90. ];
  91. }
  92. if (isset($params['id'])) {
  93. $paramsTranslation['%id%'] = $params['id'];
  94. }
  95. return $this->trans(
  96. 'title.' . $pageName,
  97. $paramsTranslation
  98. );
  99. }
  100. private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string
  101. {
  102. $translation = $this->trans($idTranslationEntity);
  103. if ($translation == $idTranslationEntity) {
  104. $translation = $this->trans($idTranslationDefault);
  105. if ($translation == $idTranslationDefault) {
  106. if ($returnEmpty) {
  107. $translation = '';
  108. } else {
  109. $translation = $idTranslationEntity;
  110. }
  111. }
  112. }
  113. return $translation;
  114. }
  115. private function buildTransIdField($fieldName, $entityClass, $default = false): string
  116. {
  117. return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default);
  118. }
  119. private function buildTransIdPanel($panelName, $entityClass, $default = false): string
  120. {
  121. return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default);
  122. }
  123. private function buildTransIdModal($modalName, $entityClass, $default = false): string
  124. {
  125. return $this->buildTransIdEntitySection($modalName, $entityClass, 'modals', $default);
  126. }
  127. private function buildTransIdCard($cardName, $entityClass, $default = false): string
  128. {
  129. return $this->buildTransIdEntitySection($cardName, $entityClass, 'cards', $default);
  130. }
  131. private function buildTransIdBox($boxName, $entityClass, $default = false): string
  132. {
  133. return $this->buildTransIdEntitySection($boxName, $entityClass, 'boxes', $default);
  134. }
  135. private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string
  136. {
  137. if ($default) {
  138. $entityName = 'default';
  139. } else {
  140. $entityName = $this->getEntityName($entityClass);
  141. }
  142. return 'entity.' . $entityName . '.' . $entitySection . '.' . $name;
  143. }
  144. public function trans($id, $params = [], $domain = self::DOMAIN): string
  145. {
  146. return $this->translator->trans($id, $params, $domain);
  147. }
  148. private function getEntityName($entityClass): string
  149. {
  150. if (is_object($entityClass)) {
  151. $entityClass = get_class($entityClass);
  152. }
  153. if (is_string($entityClass)) {
  154. $entityNameExplode = explode('\\', $entityClass);
  155. return $entityNameExplode[count($entityNameExplode) - 1];
  156. }
  157. return 'default';
  158. }
  159. }
  160. ?>