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.

TranslatorAdmin.php 6.9KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 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. ?>