2014-02-26 138 views
1

我正在將Symfony中的一個小應用程序遷移到單獨的項目中,並且它唯一依賴的 Symfony組件是I18N字符串轉換。由例如:Symfony 1.4兼容I18N翻譯系統?

  • action.class.php

    $this->culture = 'pt_BR'; 
    
  • templates/search_box.php

    <h1><?php echo __('Destination') ?></h1> 
    
  • i18n/pt_BR/messages.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <xliff version="1.0"> 
        <file datatype="plaintext" source-language="en" 
         target-language="pt_BR" original="messages"> 
        <body> 
         <note> 
         SEARCH BOX 
         </note> 
         <trans-unit id="0"> 
         <source>Destination</source> 
         <target>Destino</target> 
         </trans-unit> 
        </body> 
        </file> 
    </xliff> 
    

是否有類似的,最好兼容的系統,可作爲PHP包使用?

我有興趣保留xml文件的翻譯,但我也可以 居住在一個不同的格式。

更新

我現在,通過@ AKKY的回答啓發:

  • composer.json

    { 
        "name": "example/layout", 
        "description": 
        "Provides common layout components such as header/footer.", 
        "license": "proprietary", 
        "require": { 
         "symfony/translation": "2.4.*", 
         "symfony/config": "~2.0" 
        } 
    } 
    
  • i18n/messages.pt_BR.xliff

    <?xml version="1.0" encoding="utf-8"?> 
    <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> 
        <file source-language="en" datatype="plaintext" original="messages"> 
        <body> 
         <trans-unit id="0"> 
         <source>Destination</source> 
         <target>Destino</target> 
         </trans-unit> 
        </body> 
        </file> 
    </xliff> 
    

    不幸的是,<note>元素導致了一條錯誤消息,所以我刪除了它。

  • public/helper.inc.php

    <?php 
    
    require_once '../vendor/autoload.php'; 
    
    use Symfony\Component\Translation\Translator; 
    use Symfony\Component\Translation\MessageSelector; 
    use Symfony\Component\Translation\Loader\ArrayLoader; 
    use Symfony\Component\Translation\Loader\XliffFileLoader; 
    use Symfony\Component\Config\Resource\FileResource; 
    
    $loader = new XliffFileLoader(); 
    $catalogue = $loader->load('../i18n/messages.pt_BR.xliff', 'pt_BR'); 
    
    function __($text) { 
        global $catalogue; 
        return $catalogue->get($text); 
    } 
    
  • public/header.php

    <?php require_once('helper.inc.php') ?> 
    
    <h1><?php echo __('Destination') ?></h1> 
    

回答

2

資源文件格式的名稱是XLIFF。你可能會發現XLIFF處理php包,以及xliff2(你最喜歡的i18n格式)轉換器。

即使您沒有在新項目中使用Symfony,也可以使用Symfony Components Translation。作曲家和自動加載將在您的常規php項目中提供。我推薦它。

+0

我嘗試了Symfony翻譯組件,並跟進[另一個問題](http://stackoverflow.com/questions/22063982),因爲我無法讓它工作。 – feklee

+1

查看我更新的答案。使用Symfony 2的翻譯組件排序工作,但我不得不刪除''標籤。 – feklee

+0

您的更新中的composer.json似乎有一些冗餘行。如果您是包用戶,通常只需要「需要」部分。當你製作並向其他人提供作曲家套裝時,可能會有其他行。 – akky