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.

125 lines
3.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 $translator;
  8. const DOMAIN = 'admin';
  9. public function __construct(TranslatorInterface $translator)
  10. {
  11. $this->translator = $translator;
  12. }
  13. public function transField($fieldName, $entityClass): string
  14. {
  15. return $this->transEntityThenDefault(
  16. $this->buildTransIdField($fieldName, $entityClass),
  17. $this->buildTransIdField($fieldName, $entityClass, true)
  18. );
  19. }
  20. public function transHelp($fieldName, $entityClass): string
  21. {
  22. $fieldName = $fieldName . '_help';
  23. return $this->transEntityThenDefault(
  24. $this->buildTransIdField($fieldName, $entityClass),
  25. $this->buildTransIdField($fieldName, $entityClass, true),
  26. true
  27. );
  28. }
  29. public function transPanel($panelName, $entityClass): string
  30. {
  31. return $this->transEntityThenDefault(
  32. $this->buildTransIdPanel($panelName, $entityClass),
  33. $this->buildTransIdPanel($panelName, $entityClass, true)
  34. );
  35. }
  36. public function transTitle($pageName, $entityClass = null, $params = [])
  37. {
  38. $entityName = $this->getEntityName($entityClass);
  39. $paramsTranslation = [] ;
  40. if($entityName) {
  41. $baseIdEntityLabel = 'entity.' . $entityName ;
  42. $paramsTranslation = [
  43. '%label%' => $this->trans($baseIdEntityLabel . '.label'),
  44. '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
  45. ] ;
  46. }
  47. if (isset($params['id'])) {
  48. $paramsTranslation['%id%'] = $params['id'];
  49. }
  50. return $this->trans(
  51. 'title.' . $pageName,
  52. $paramsTranslation
  53. );
  54. }
  55. private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string
  56. {
  57. $translation = $this->trans($idTranslationEntity);
  58. if ($translation == $idTranslationEntity) {
  59. $translation = $this->trans($idTranslationDefault);
  60. if ($translation == $idTranslationDefault) {
  61. if ($returnEmpty) {
  62. $translation = '';
  63. } else {
  64. $translation = $idTranslationEntity;
  65. }
  66. }
  67. }
  68. return $translation;
  69. }
  70. private function buildTransIdField($fieldName, $entityClass, $default = false): string
  71. {
  72. if ($default) {
  73. $entityName = 'default';
  74. } else {
  75. $entityName = $this->getEntityName($entityClass);
  76. }
  77. return 'entity.' . $entityName . '.fields.' . $fieldName;
  78. }
  79. private function buildTransIdPanel($panelName, $entityClass, $default = false): string
  80. {
  81. if ($default) {
  82. $entityName = 'default';
  83. } else {
  84. $entityName = $this->getEntityName($entityClass);
  85. }
  86. return 'entity.' . $entityName . '.panels.' . $panelName;
  87. }
  88. private function trans($id, $params = [], $domain = self::DOMAIN): string
  89. {
  90. return $this->translator->trans($id, $params, $domain);
  91. }
  92. private function getEntityName($entityClass): string
  93. {
  94. $entityNameExplode = explode('\\', $entityClass);
  95. return strtolower($entityNameExplode[count($entityNameExplode) - 1]);
  96. }
  97. }
  98. ?>