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 4.4KB

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