1

I'm looking for a solution to automatically translate the entities of my Symfony application. I'm stuck with a legacy database where translations are stored in the same table as extra fields:

id | name | name_de | name_fr
 1 | cat  | Katze   | chat
 2 | dog  | Hund    | chien

My entity is mapped accordinggly:

class Animal
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     *
     * @var integer
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=64, nullable=false)
     *
     * @var string
     */
    private $name;

    /**
     * @ORM\Column(name="name_de", type="string", length=64, nullable=false)
     *
     * @var string
     */
    private $nameDe;

    /**
     * @ORM\Column(name="name_fr", type="string", length=64, nullable=false)
     *
     * @var string
     */
    private $nameFr;

    /* Followed by getters and setters */
}

I've already looking into the Translatable extension, but that cannot match my database schema. I also started with a custom annotation hooking into the postLoad event, but then I was stopped by the simple issue that postLoad may be triggered in the proxy state of an entity.

Next I'd look into a custom query walker (basically a modified approach of the Translatable extension), but I'd hope there's a less complex solution out there.

Cheers Matthias

althaus
  • 2,009
  • 2
  • 25
  • 33

1 Answers1

1

There are quite a few solutions here and I guess I haven't looked at half of them.

Anyhow, the least complex and at least a little bit clean solution I came up with so far is using a static class for translating. This could look something like this:

class Translation
{

    /**
     * @var string
     */
    private static $language;

    /**
     * tries to get a translated property by prepending the
     * configured language to the given entities getter
     *
     * @param object $entity
     * @param string $getter
     * @return mixed
     */
    public static function getTranslated($entity, $getter) {
        $language = self::getLanguage();
        $translatedGetter = $getter.$language;
        if(method_exists($entity, $translatedGetter)) {
            return $entity->$translatedGetter();
        } else {
            return $entity->$getter;
        }
    }

    /**
     * @return string
     */
    public static function getLanguage()
    {
        return self::$language;
    }

    /**
     * @param string $language
     */
    public static function setLanguage($language)
    {
        self::$language = ucfirst(strtolower($language));
    }

}

You then set the language whenever your application starts and implement the translations either in your entities:

/**
 * @return string
 */
public function getName()
{
    return Translation::getTranslated($this, __FUNCTION__);
}

or call it from outside:

Translation::getTranslated($animal, "getName");

So with the first method this code:

Translation::setLanguage("DE");

// far more code

/** @var Animal[] $animals */
$animals = $entityManager->getRepository(Animal::class)->findAll();

foreach ($animals as $animal) {
    echo $animal->getName()."<br >";
}

would put out:

Katze
Hund

This is just one way to do it with a static class, of course.

StoryTeller
  • 1,673
  • 12
  • 30