简体   繁体   中英

How to disable standard translation loaders in Symfony2

I use a custom storage for translations in Symfony2, but I want to make sure that the translations will be loaded only from my storage, and not from other sources (such as Yaml files). How can I disable the standard loaders? In my custom Translator class I have the following code:

   /**
     * {@inheritdoc}
     */
    protected function loadCatalogue($locale)
    {
        $this->initializeCatalogue($locale);
    }

    /**
     * {@inheritdoc}
     */
    protected function initializeCatalogue($locale)
    {
        $this->addLoader('storageLoader', $this->container->get('my.translation.loader'));
        $this->addResource('storageLoader', $this->container->get('storage.getter'), $locale);

        parent::initializeCatalogue($locale);
    }

But in parent::initializeCatalogue($locale); it loads all standard loaders. I found this post , where how I get guy only delete cache files, to make sure that translation getting only from the database, or did I miss something?

You're missing few things to disable standard translation loaders in your app.

Register service

Add to your services your custom translation loader (remember to replace class with your own):

services:
    my.translation.loader.dbext:
        class: YouApp\CommonBundle\Services\MyTranslationLoader
        arguments: [@doctrine.orm.entity_manager]
        tags:
            - { name: translation.loader, alias: dbext}

dbext - is extension of fake messages files (feel free to change it). When symfony tries to load file with such extension, loader will be replaced with your class.

Create fake message files

Last step is to create those fake messages.en.dbext, messages.fr.dbext and other messages.*.dbext files as it is in sf2 documentation:

If you're loading translations from a database, you'll still need a resource file, but it might either be blank or contain a little bit of information about loading those resources from the database. The file is key to trigger the load method on your custom loader.

That should help you in this case.

EDIT:

If you're not using YAML in your service registration, you can go with XML

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service
            id="my.translation.loader.dbext"
            class="YouApp\CommonBundle\Services\MyTranslationLoader">
            <tag name="translation.loader" alias="dbext" />
        </service>
    </services>
</container>

or PHP

$container
    ->register('my.translation.loader.dbext', 'YouApp\CommonBundle\Services\MyTranslationLoader')
    ->addTag('translation.loader', array('alias' => 'dbext'))
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM