Repository: EasyCorp/EasyAdminBundle (language: php)

Issue:
Allow EasyAdminTwigExtension::representAsString to use Translatable method
**Short description of what this feature will allow to do:**
The method `EasyAdminTwigExtension::representAsString` could be improved to allow us to customize display of Stringable entities with an interface that you could easily create:

```php
        if (\is_object($value)) {

            // Added this part of code:
            if (value instanceof TranslatableObject) {
                return $value->toTranslatableString($this->translator));
            }

            if (method_exists($value, '__toString')) {
                return (string) $value;
            }

            if (method_exists($value, 'getId')) {
                return sprintf('%s #%s', $value::class, $value->getId());
            }

            return sprintf('%s #%s', $value::class, substr(md5(spl_object_hash($value)), 0, 7));
        }
```

```php
// Some namespace of EA;

interface TranslatableObject {

    function toTranslatableString(\Symfony\Contracts\Translation\TranslatorInterface $translator): string;
}
```


This way we could easily translate our entities displayed in form and grids.
What's your thoughts @javiereguiluz ?



Discussion / hints:
Better use [TranslatableInterface](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Contracts/Translation/TranslatableInterface.php)


Indeed !
I made the issue in a hurry, but nice suggestion 🙏🏼

On Sat, 9 Mar 2024, 6:29 pm Serg N. Kalachev, ***@***.***>
wrote:

> Better use TranslatableInterface
> <https://github.com/symfony/symfony/blob/6.4/src/Symfony/Contracts/Translation/TranslatableInterface.php>
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/EasyCorp/EasyAdminBundle/issues/6191#issuecomment-1986924582>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAKFGZ4QJNJWURVUUDJFPTTYXNBGZAVCNFSM6AAAAABEOHVMJKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBWHEZDINJYGI>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>

Would you accept & merge a [TranslatableInterface](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Contracts/Translation/TranslatableInterface.php) proposal @javiereguiluz ? 
@Geolim4 yes! I like this proposal. It's a small code addition, easy to maintain and can help some folks. Thanks!
Thanks, I think there's also some improvement about Stringable entities that are printed everywhere around the differents page:

Example: 

![image](https://github.com/EasyCorp/EasyAdminBundle/assets/1332071/0544b9dc-13e9-4bf8-b1af-13cd677a4cce)

On "detail" page I have a fieldCollection of entities that are weirdly rendered as you can see above, I'll try to add them to the PR as well.


Fix patch:
diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php
index d153c5091a..4538037250 100644
--- a/src/Resources/config/services.php
+++ b/src/Resources/config/services.php
@@ -133,6 +133,7 @@
             ->arg(1, service(AdminContextProvider::class))
             ->arg(2, new Reference('security.csrf.token_manager', ContainerInterface::NULL_ON_INVALID_REFERENCE))
             ->arg(3, new Reference('asset_mapper.importmap.renderer', ContainerInterface::NULL_ON_INVALID_REFERENCE))
+            ->arg(4, service('translator.default'))
             ->tag('twig.extension')
 
         ->set(EaCrudFormTypeExtension::class)
diff --git a/src/Twig/EasyAdminTwigExtension.php b/src/Twig/EasyAdminTwigExtension.php
index f56e55561a..865218a2f4 100644
--- a/src/Twig/EasyAdminTwigExtension.php
+++ b/src/Twig/EasyAdminTwigExtension.php
@@ -11,6 +11,8 @@
 use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer;
 use Symfony\Component\DependencyInjection\ServiceLocator;
 use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
+use Symfony\Contracts\Translation\TranslatableInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
 use Twig\Environment;
 use Twig\Error\RuntimeError;
 use Twig\Extension\AbstractExtension;
@@ -31,13 +33,15 @@ class EasyAdminTwigExtension extends AbstractExtension implements GlobalsInterfa
     private AdminContextProvider $adminContextProvider;
     private ?CsrfTokenManagerInterface $csrfTokenManager;
     private ?ImportMapRenderer $importMapRenderer;
+    private TranslatorInterface $translator;
 
-    public function __construct(ServiceLocator $serviceLocator, AdminContextProvider $adminContextProvider, ?CsrfTokenManagerInterface $csrfTokenManager, ?ImportMapRenderer $importMapRenderer)
+    public function __construct(ServiceLocator $serviceLocator, AdminContextProvider $adminContextProvider, ?CsrfTokenManagerInterface $csrfTokenManager, ?ImportMapRenderer $importMapRenderer, TranslatorInterface $translator)
     {
         $this->serviceLocator = $serviceLocator;
         $this->adminContextProvider = $adminContextProvider;
         $this->csrfTokenManager = $csrfTokenManager;
         $this->importMapRenderer = $importMapRenderer;
+        $this->translator = $translator;
     }
 
     public function getFunctions(): array
@@ -155,6 +159,10 @@ public function representAsString($value): string
         }
 
         if (\is_object($value)) {
+            if ($value instanceof TranslatableInterface) {
+                return $value->trans($this->translator);
+            }
+
             if (method_exists($value, '__toString')) {
                 return (string) $value;
             }


Test patch: