如何在 Symfony2 中禁用标准翻译加载器
How to disable standard translation loaders in Symfony2
我在 Symfony2 中使用自定义存储来存储翻译,但我想确保只从我的存储中加载翻译,而不是从其他来源(例如 Yaml 文件)加载。我怎样才能禁用标准装载机?在我的自定义 Translator
class 中,我有以下代码:
/**
* {@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);
}
但在 parent::initializeCatalogue($locale);
中它会加载所有标准加载程序。我发现 this post,我如何让这个人只删除缓存文件,以确保只从数据库中获取翻译,或者我错过了什么?
您缺少在您的应用中禁用标准翻译加载器的一些东西。
注册服务
将您的 custom translation loader 添加到您的服务中(记得将 class 替换为您自己的):
services:
my.translation.loader.dbext:
class: YouApp\CommonBundle\Services\MyTranslationLoader
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: translation.loader, alias: dbext}
dbext - 是虚假消息文件的扩展名(可以随意更改)。当 symfony 尝试加载具有此类扩展名的文件时,加载程序将替换为您的 class.
创建虚假消息文件
最后一步是创建那些伪造的 messages.en.dbext、messages.fr.dbext 和其他消息。*.dbext 文件,因为它在 sf2 文档中:
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.
在这种情况下,这应该对您有所帮助。
编辑:
如果您在服务注册中未使用 YAML,则可以使用 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>
或PHP
$container
->register('my.translation.loader.dbext', 'YouApp\CommonBundle\Services\MyTranslationLoader')
->addTag('translation.loader', array('alias' => 'dbext'))
;
我在 Symfony2 中使用自定义存储来存储翻译,但我想确保只从我的存储中加载翻译,而不是从其他来源(例如 Yaml 文件)加载。我怎样才能禁用标准装载机?在我的自定义 Translator
class 中,我有以下代码:
/**
* {@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);
}
但在 parent::initializeCatalogue($locale);
中它会加载所有标准加载程序。我发现 this post,我如何让这个人只删除缓存文件,以确保只从数据库中获取翻译,或者我错过了什么?
您缺少在您的应用中禁用标准翻译加载器的一些东西。
注册服务
将您的 custom translation loader 添加到您的服务中(记得将 class 替换为您自己的):
services:
my.translation.loader.dbext:
class: YouApp\CommonBundle\Services\MyTranslationLoader
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: translation.loader, alias: dbext}
dbext - 是虚假消息文件的扩展名(可以随意更改)。当 symfony 尝试加载具有此类扩展名的文件时,加载程序将替换为您的 class.
创建虚假消息文件
最后一步是创建那些伪造的 messages.en.dbext、messages.fr.dbext 和其他消息。*.dbext 文件,因为它在 sf2 文档中:
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.
在这种情况下,这应该对您有所帮助。
编辑:
如果您在服务注册中未使用 YAML,则可以使用 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>
或PHP
$container
->register('my.translation.loader.dbext', 'YouApp\CommonBundle\Services\MyTranslationLoader')
->addTag('translation.loader', array('alias' => 'dbext'))
;