Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:32:15+00:00 2026-06-03T06:32:15+00:00

File based translations don’t work for me because clients need to change the texts.

  • 0

File based translations don’t work for me because clients need to change the texts.

So I am thinking about implementing this interface to fetch data from the database and cache the results in an APC cache.
Is this a good solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:32:16+00:00Added an answer on June 3, 2026 at 6:32 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Rails 2.3 support a file-based session store out of the box any more?
I'm storing several versions of a file based on a digest of the original
I am required to write a script that would download a file (based on
I have a function TRANSLATE which reads value from the xml file based on
I want to include the last 2 files in my php file based on
I can generate my models and schema.yml file based on an existing database. But
On a django site, I want to generate an excel file based on some
I'm doing password based file encryption in Java; I'm using AES as the underlying
I have an XML based file format that I'm using to store and load
I'm trying to parse text-based file attachments (txt, doc, etc...). However, I can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.