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.

228 line
6.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, $params = []): string
  18. {
  19. return $this->trans('menu.' . $menu, $params);
  20. }
  21. public function transFlashMessage($type, $name, $entityClass = false, $params = []): string
  22. {
  23. if ($entityClass) {
  24. return $this->transEntityThenDefault(
  25. $this->buildTransIdFlash($type . '.' . $name, $entityClass),
  26. $this->buildTransIdFlash($type . '.' . $name, $entityClass, true),
  27. false,
  28. $params
  29. );
  30. } else {
  31. return $this->trans('flash_message.' . $name, $params);
  32. }
  33. }
  34. public function transField($fieldName, $entityClass): string
  35. {
  36. return $this->transEntityThenDefault(
  37. $this->buildTransIdField($fieldName, $entityClass),
  38. $this->buildTransIdField($fieldName, $entityClass, true)
  39. );
  40. }
  41. public function transFieldIndex($fieldName, $entityClass): string
  42. {
  43. $idTranslationFieldIndex = $this->buildTransIdField($fieldName . 'Index', $entityClass);
  44. $translation = $this->trans($idTranslationFieldIndex);
  45. if ($translation == $idTranslationFieldIndex) {
  46. return $this->transField($fieldName, $entityClass);
  47. } else {
  48. return $translation;
  49. }
  50. }
  51. public function transChoices(array $choices, string $entityName, string $field): array
  52. {
  53. $newChoices = [];
  54. foreach ($choices as $key => $choice) {
  55. $newChoices[$this->transField($field . 'Choices.' . $choice, $entityName)] = $choice;
  56. }
  57. return $newChoices;
  58. }
  59. public function transChoice(string $entityName, string $field, string $choice): string
  60. {
  61. return $this->transField($field . 'Choices.' . $choice, $entityName);
  62. }
  63. public function transHelp($fieldName, $entityClass): string
  64. {
  65. $fieldName = $fieldName . '_help';
  66. return $this->transEntityThenDefault(
  67. $this->buildTransIdField($fieldName, $entityClass),
  68. $this->buildTransIdField($fieldName, $entityClass, true),
  69. true
  70. );
  71. }
  72. public function transPanel($panelName, $entityClass): string
  73. {
  74. return $this->transEntityThenDefault(
  75. $this->buildTransIdPanel($panelName, $entityClass),
  76. $this->buildTransIdPanel($panelName, $entityClass, true)
  77. );
  78. }
  79. public function transModal($modalName, $entityClass): string
  80. {
  81. return $this->transEntityThenDefault(
  82. $this->buildTransIdModal($modalName, $entityClass),
  83. $this->buildTransIdModal($modalName, $entityClass, true)
  84. );
  85. }
  86. public function transCard($cardName, $entityClass): string
  87. {
  88. return $this->transEntityThenDefault(
  89. $this->buildTransIdCard($cardName, $entityClass),
  90. $this->buildTransIdCard($cardName, $entityClass, true)
  91. );
  92. }
  93. public function transBox($cardName, $entityClass): string
  94. {
  95. return $this->transEntityThenDefault(
  96. $this->buildTransIdBox($cardName, $entityClass),
  97. $this->buildTransIdBox($cardName, $entityClass, true)
  98. );
  99. }
  100. public function transTitle($pageName, $entityClass = null, $params = []): string
  101. {
  102. $entityName = $this->getEntityName($entityClass);
  103. $paramsTranslation = [];
  104. if ($entityName) {
  105. $baseIdEntityLabel = 'entity.' . $entityName;
  106. $paramsTranslation = [
  107. '%label%' => $this->trans($baseIdEntityLabel . '.label'),
  108. '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
  109. ];
  110. }
  111. if (isset($params['id'])) {
  112. $paramsTranslation['%id%'] = $params['id'];
  113. }
  114. return $this->trans(
  115. 'title.' . $pageName,
  116. $paramsTranslation
  117. );
  118. }
  119. private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false, $params = []): string
  120. {
  121. $translation = $this->trans($idTranslationEntity, $params);
  122. if ($translation == $idTranslationEntity) {
  123. $translation = $this->trans($idTranslationDefault, $params);
  124. if ($translation == $idTranslationDefault) {
  125. if ($returnEmpty) {
  126. $translation = '';
  127. } else {
  128. $translation = $idTranslationEntity;
  129. }
  130. }
  131. }
  132. return $translation;
  133. }
  134. private function buildTransIdField($fieldName, $entityClass, $default = false): string
  135. {
  136. return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default);
  137. }
  138. private function buildTransIdPanel($panelName, $entityClass, $default = false): string
  139. {
  140. return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default);
  141. }
  142. private function buildTransIdModal($modalName, $entityClass, $default = false): string
  143. {
  144. return $this->buildTransIdEntitySection($modalName, $entityClass, 'modals', $default);
  145. }
  146. private function buildTransIdCard($cardName, $entityClass, $default = false): string
  147. {
  148. return $this->buildTransIdEntitySection($cardName, $entityClass, 'cards', $default);
  149. }
  150. private function buildTransIdBox($boxName, $entityClass, $default = false): string
  151. {
  152. return $this->buildTransIdEntitySection($boxName, $entityClass, 'boxes', $default);
  153. }
  154. private function buildTransIdFlash($flashName, $entityClass, $default = false): string
  155. {
  156. return $this->buildTransIdEntitySection($flashName, $entityClass, 'flashes', $default);
  157. }
  158. private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string
  159. {
  160. if ($default) {
  161. $entityName = 'default';
  162. } else {
  163. $entityName = $this->getEntityName($entityClass);
  164. }
  165. return 'entity.' . $entityName . '.' . $entitySection . '.' . $name;
  166. }
  167. public function trans($id, $params = [], $domain = self::DOMAIN): string
  168. {
  169. return $this->translator->trans($id, $params, $domain);
  170. }
  171. private function getEntityName($entityClass): string
  172. {
  173. if (is_object($entityClass)) {
  174. $entityClass = get_class($entityClass);
  175. }
  176. if (is_string($entityClass)) {
  177. $entityNameExplode = explode('\\', $entityClass);
  178. return $entityNameExplode[count($entityNameExplode) - 1];
  179. }
  180. return 'default';
  181. }
  182. }
  183. ?>