PHP code example of webfactory / polyglot-bundle

1. Go to this page and download the library: Download webfactory/polyglot-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webfactory / polyglot-bundle example snippets



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Table]
#[ORM\Entity]
class Document
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private int $id;

    #[ORM\Column]
    private string $text;

    public function getText(): string
    {
        return $this->text;
    }
}
 


namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;

#[Polyglot\Locale(primary: "en_GB")]
class Document
{
    #[Polyglot\TranslationCollection]
    #[ORM\OneToMany(targetEntity: \DocumentTranslation::class, mappedBy: 'entity')]
    private Collection $translations;

    /**
     * @var TranslatableInterface<string>
     */
    #[Polyglot\Translatable]
    #[ORM\Column(type: 'translatable_string')]
    private TranslatableInterface $text;

    public function __construct(...)
    {
        // ...
        $this->translations = new ArrayCollection();
    }

    public function getText(): string
    {
        return $this->text->translate();
    }
}



namespace App\Entity;
    
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;

#[ORM\Table]
#[ORM\UniqueConstraint(columns: ['entity_id', 'locale'])]
#[ORM\Entity]
class DocumentTranslation
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private int $id;

    #[ORM\Column]
    #[Polyglot\Locale]
    private string $locale;

    #[ORM\ManyToOne(targetEntity: Document::class, inversedBy: 'translations')]
    private Document $entity;

    public function getLocale(): string
    {
        return $this->locale;
    }

    #[ORM\Column]
    private string $text;
}

 
...
class Document 
{ 
    ... 
    public function getText(string $locale = null): string
    {
        return $this->text->translate($locale);    
    }
}

    $myDocument = ...;
    $text = $myDocument->getText();
    
    if ($text) { ... }  // Never holds because the value holder is returned (even if it contains a "" translation value)
    if ($text === 'someValue') { ... } // Strict type check prevents calling the __toString() method



use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;

// ...
class Document
{
    // ... fields and collections omitted for brevity
    #[ORM\Column(type: '...yourtype')]
    #[Polyglot\Translatable]
    private TranslatableInterface|<other type> $text;

    // ...
}