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.

155 lines
4.3KB

  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 $translator;
  8. const DOMAIN = 'admin';
  9. public function __construct(TranslatorInterface $translator)
  10. {
  11. $this->translator = $translator;
  12. }
  13. public function transAction($action)
  14. {
  15. return $this->trans('action.' . $action);
  16. }
  17. public function transMenu($menu)
  18. {
  19. return $this->trans('menu.' . $menu);
  20. }
  21. public function transFlashMessage($name)
  22. {
  23. return $this->trans('flash_message.' . $name);
  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 = [])
  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. ?>