2015-02-11 25 views
2

我在Symfony2中使用自定義存儲器進行翻譯,但是我想確保翻譯只能從我的存儲中加載,而不是從其他來源(如Yaml文件)加載。我如何禁用標準裝載機?在我的自定義Translator類我有以下代碼:如何在Symfony2中禁用標準翻譯加載器

/** 
    * {@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,在那裏我如何讓人只刪除緩存文件,以確保翻譯只從數據庫獲得,或者我錯過了什麼?

回答

0

您錯過了幾件事情可以在您的應用中禁用標準翻譯加載器。

註冊服務

添加到您服務您的custom translation loader(記得用自己替換類):

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

dbext的 - 是假消息的文件擴展名(隨意更改) 。當symfony嘗試使用這種擴展加載文件時,加載器將被您的類替換。

製造假消息文件

最後一步是創建那些假messages.en.dbext,messages.fr.dbext和其他信息* dbext的文件,因爲它是SF2文檔中:。

如果您從數據庫加載翻譯,您仍然需要一個 資源文件,但該文件可能爲空或包含一點點關於從數據庫加載這些資源的信息。文件 是觸發自定義加載程序的加載方法的關鍵。

這應該對你有幫助。

編輯:

如果你不是在你的服務註冊使用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')) 
; 
+0

@excluded_once走了 - 難道它幫助?一切正常嗎? – 2015-03-27 11:21:33