Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TranslatorAdmin.php 4.0KB

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