您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TranslatorAdmin.php 6.9KB

3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ?>