Translations and Symfony2 in database
This could be what you are looking for:
Use a database as a translation provider in Symfony 2
Introduction
This article explain how to use a database as translation storage in Symfony 2. Using a database to provide translations is quite easy to do in Symfony 2, but unfortunately it’s actually not explained in Symfony 2 website.
Creating language entities
At first, we have to create database entities for language management. In my case, I’ve created three entities : the Language entity contain every available languages (like french, english, german).
The second entity is named LanguageToken. It represent every available language tokens. The token entity represent the source tag of the xliff files. Every translatable text available is a token. For example, I use home_page as a token and it’s translated as Page principale in french and as Home page in english.
The last entity is the LanguageTranslation entity : it contain the translation of a token in a specific language. In the example below, the Page principale is a LanguageTranslation entity for the language french and the token home_page.
It’s quite inefficient, but the translations are cached in a file by Symfony 2, finally it’s used only one time at Symfony 2 first execution (except if you delete Symfony 2’s cache files).
The code of the Language entity is visible here :
/** * @ORM\Entity(repositoryClass="YourApp\YourBundle\Repository\LanguageRepository") */ class Language { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue */ private $id; /** @ORM\column(type="string", length=200) */ private $locale; /** @ORM\column(type="string", length=200) */ private $name; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getLocale() { return $this->locale; } public function setLocale($locale) { $this->locale = $locale; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } }
The code of the LanguageToken entity is visible here :
/** * @ORM\Entity(repositoryClass="YourApp\YourBundle\Repository\LanguageTokenRepository") */ class LanguageToken { /** * @ORM\Id @ORM\Column(type="integer") * @ORM\GeneratedValue */ private $id; /** @ORM\column(type="string", length=200, unique=true) */ private $token; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getToken() { return $this->token; } public function setToken($token) { $this->token = $token; } }
And the LanguageTranslation entity’s code is visible here :
/** * @ORM\Entity(repositoryClass="YourApp\YourBundle\Repository\LanguageTranslationRepository") */ class LanguageTranslation { /** * @ORM\Id @ORM\Column(type="integer") * @ORM\GeneratedValue */ private $id; /** @ORM\column(type="string", length=200) */ private $catalogue; /** @ORM\column(type="text") */ private $translation; /** * @ORM\ManyToOne(targetEntity="YourApp\YourBundle\Entity\Language", fetch="EAGER") */ private $language; /** * @ORM\ManyToOne(targetEntity="YourApp\YourBundle\Entity\LanguageToken", fetch="EAGER") */ private $languageToken; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getCatalogue() { return $this->catalogue; } public function setCatalogue($catalogue) { $this->catalogue = $catalogue; } public function getTranslation() { return $this->translation; } public function setTranslation($translation) { $this->translation = $translation; } public function getLanguage() { return $this->language; } public function setLanguage($language) { $this->language = $language; } public function getLanguageToken() { return $this->languageToken; } public function setLanguageToken($languageToken) { $this->languageToken = $languageToken; } }
Implementing a LoaderInterface
The second step is to create a class implementing the Symfony\Component\Translation\Loader\LoaderInterface. The corresponding class is shown here :
class DBLoader implements LoaderInterface{ private $transaltionRepository; private $languageRepository; /** * @param EntityManager $entityManager */ public function __construct(EntityManager $entityManager){ $this->transaltionRepository = $entityManager->getRepository("AppCommonBundle:LanguageTranslation"); $this->languageRepository = $entityManager->getRepository("AppCommonBundle:Language"); } function load($resource, $locale, $domain = 'messages'){ //Load on the db for the specified local $language = $this->languageRepository->getLanguage($locale); $translations = $this->transaltionRepository->getTranslations($language, $domain); $catalogue = new MessageCatalogue($locale); /**@var $translation Frtrains\CommonbBundle\Entity\LanguageTranslation */ foreach($translations as $translation){ $catalogue->set($translation->getLanguageToken()->getToken(), $translation->getTranslation(), $domain); } return $catalogue; } }
The DBLoader class need to have every translations from the LanguageTranslationRepository (the translationRepository member). The getTranslations($language, $domain) method of the translationRepository object is visible here :
class LanguageTranslationRepository extends EntityRepository { /** * Return all translations for specified token * @param type $token * @param type $domain */ public function getTranslations($language, $catalogue = "messages"){ $query = $this->getEntityManager()->createQuery("SELECT t FROM AppCommonBundle:LanguageTranslation t WHERE t.language = :language AND t.catalogue = :catalogue"); $query->setParameter("language", $language); $query->setParameter("catalogue", $catalogue); return $query->getResult(); } ... }
The DBLoader class will be created by Symfony as a service, receiving an EntityManager as constructor argument. All arguments of the load method let you customize the way the translation loader interface work.
Create a Symfony service with DBLoader
The third step is to create a service using the previously created class. The code to add to the config.yml file is here :
services: translation.loader.db: class: MyApp\CommonBundle\Services\DBLoader arguments: [@doctrine.orm.entity_manager] tags: - { name: translation.loader, alias: db}
The transation.loader tag indicate to Symfony to use this translation loader for the db alias.
Create fake translation files
The last step is to create an app/Resources/translations/messages.xx.db file for every translation (with xx = en, fr, de, …).
I didn’t found the way to notify Symfony to use DBLoader as default translation loader. The only quick hack I’ve found is to create a app/Resources/translations/messages.en.db file. The db extension correspond to the db alias used in the service declaration. A corresponding file is created for every language available on the website, like messages.fr.db for french or messages.de.db for german.
When Symfony find the messages.xx.db file he load the translation.loader.db to manage this unknown extension and then the DBLoader use database content to provide translation.
I’ve also didn’t found the way to clean properly the translations cache on database modification (the cache have to be cleaned to force Symfony to recreate it). The code I actually use is visible here :
/** * Remove language in every cache directories */ private function clearLanguageCache(){ $cacheDir = __DIR__ . "/../../../../app/cache"; $finder = new \Symfony\Component\Finder\Finder(); //TODO quick hack... $finder->in(array($cacheDir . "/dev/translations", $cacheDir . "/prod/translations"))->files(); foreach($finder as $file){ unlink($file->getRealpath()); } }
This solution isn’t the pretiest one (I will update this post if I find better solution) but it’s working ^^
Be Sociable, Share!