Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

232 Zeilen
7.1KB

  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, $params=[]): string
  14. {
  15. return $this->trans('action.' . $action, $params);
  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. if (isset($params['title'])) {
  115. $paramsTranslation['%title%'] = $params['title'];
  116. }
  117. return $this->trans(
  118. 'title.' . $pageName,
  119. $paramsTranslation
  120. );
  121. }
  122. private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false, $params = []): string
  123. {
  124. $translation = $this->trans($idTranslationEntity, $params);
  125. if ($translation == $idTranslationEntity) {
  126. $translation = $this->trans($idTranslationDefault, $params);
  127. if ($translation == $idTranslationDefault) {
  128. if ($returnEmpty) {
  129. $translation = '';
  130. } else {
  131. $translation = $idTranslationEntity;
  132. }
  133. }
  134. }
  135. return $translation;
  136. }
  137. private function buildTransIdField($fieldName, $entityClass, $default = false): string
  138. {
  139. return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default);
  140. }
  141. private function buildTransIdPanel($panelName, $entityClass, $default = false): string
  142. {
  143. return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default);
  144. }
  145. private function buildTransIdModal($modalName, $entityClass, $default = false): string
  146. {
  147. return $this->buildTransIdEntitySection($modalName, $entityClass, 'modals', $default);
  148. }
  149. private function buildTransIdCard($cardName, $entityClass, $default = false): string
  150. {
  151. return $this->buildTransIdEntitySection($cardName, $entityClass, 'cards', $default);
  152. }
  153. private function buildTransIdBox($boxName, $entityClass, $default = false): string
  154. {
  155. return $this->buildTransIdEntitySection($boxName, $entityClass, 'boxes', $default);
  156. }
  157. private function buildTransIdFlash($flashName, $entityClass, $default = false): string
  158. {
  159. return $this->buildTransIdEntitySection($flashName, $entityClass, 'flashes', $default);
  160. }
  161. private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string
  162. {
  163. if ($default) {
  164. $entityName = 'default';
  165. } else {
  166. $entityName = $this->getEntityName($entityClass);
  167. }
  168. return 'entity.' . $entityName . '.' . $entitySection . '.' . $name;
  169. }
  170. public function trans($id, $params = [], $domain = self::DOMAIN): string
  171. {
  172. return $this->translator->trans($id, $params, $domain);
  173. }
  174. private function getEntityName($entityClass): string
  175. {
  176. if (is_object($entityClass)) {
  177. $entityClass = get_class($entityClass);
  178. }
  179. if (is_string($entityClass)) {
  180. $entityNameExplode = explode('\\', $entityClass);
  181. return $entityNameExplode[count($entityNameExplode) - 1];
  182. }
  183. return 'default';
  184. }
  185. }
  186. ?>