Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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