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.

160 Zeilen
4.6KB

  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 transTitle($pageName, $entityClass = null, $params = []): string
  61. {
  62. $entityName = $this->getEntityName($entityClass);
  63. $paramsTranslation = [];
  64. if ($entityName) {
  65. $baseIdEntityLabel = 'entity.' . $entityName;
  66. $paramsTranslation = [
  67. '%label%' => $this->trans($baseIdEntityLabel . '.label'),
  68. '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
  69. ];
  70. }
  71. if (isset($params['id'])) {
  72. $paramsTranslation['%id%'] = $params['id'];
  73. }
  74. return $this->trans(
  75. 'title.' . $pageName,
  76. $paramsTranslation
  77. );
  78. }
  79. private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string
  80. {
  81. $translation = $this->trans($idTranslationEntity);
  82. if ($translation == $idTranslationEntity) {
  83. $translation = $this->trans($idTranslationDefault);
  84. if ($translation == $idTranslationDefault) {
  85. if ($returnEmpty) {
  86. $translation = '';
  87. } else {
  88. $translation = $idTranslationEntity;
  89. }
  90. }
  91. }
  92. return $translation;
  93. }
  94. private function buildTransIdField($fieldName, $entityClass, $default = false): string
  95. {
  96. return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default);
  97. }
  98. private function buildTransIdPanel($panelName, $entityClass, $default = false): string
  99. {
  100. return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default);
  101. }
  102. private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string
  103. {
  104. if ($default) {
  105. $entityName = 'default';
  106. } else {
  107. $entityName = $this->getEntityName($entityClass);
  108. }
  109. return 'entity.' . $entityName . '.' . $entitySection . '.' . $name;
  110. }
  111. public function trans($id, $params = [], $domain = self::DOMAIN): string
  112. {
  113. return $this->translator->trans($id, $params, $domain);
  114. }
  115. private function getEntityName($entityClass): string
  116. {
  117. if (is_object($entityClass)) {
  118. $entityClass = get_class($entityClass);
  119. }
  120. if (is_string($entityClass)) {
  121. $entityNameExplode = explode('\\', $entityClass);
  122. return $entityNameExplode[count($entityNameExplode) - 1];
  123. }
  124. return 'default';
  125. }
  126. }
  127. ?>