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.

186 lines
5.5KB

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