Zend_Translate 多语言化之Zend_Translate_Adapter_Tmx 翻译适配器

下面介绍使用 Zend_Translate_Adapter_Tmx 翻译适配器

mytranslation.tmx

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tmx SYSTEM "tmx14.dtd">
<tmx version="1.4">
   <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
           adminlang="en-us" srclang="de-at" o-tmf="abc"
           creationtool="XYZTool" >
   </header>
   <body>
       <tu tuid='message1'>
           <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
           <tuv xml:lang="en"><seg>message1</seg></tuv>
     <tuv xml:lang="zh"><seg>消息1</seg></tuv>
     <tuv xml:lang="ja"><seg>新たな需要1</seg></tuv>
     <tuv xml:lang="ar"><seg>الطلب الجديد1</seg></tuv>
    </tu>
       <tu tuid='message2'>
           <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
           <tuv xml:lang="en"><seg>message2</seg></tuv>
     <tuv xml:lang="zh"><seg>消息2</seg></tuv>
     <tuv xml:lang="ja"><seg>新たな需要2</seg></tuv>
     <tuv xml:lang="ar"><seg>الطلب الجديد2</seg></tuv>
       </tu>
    <tu tuid='message3'>
           <tuv xml:lang="de"><seg>Nachricht3</seg></tuv>
           <tuv xml:lang="en"><seg>message3</seg></tuv>
     <tuv xml:lang="zh"><seg>消息3</seg></tuv>
     <tuv xml:lang="ja"><seg>新たな需要3</seg></tuv>
     <tuv xml:lang="ar"><seg>الطلب الجديد3</seg></tuv>
       </tu>
    <tu tuid='hello world'>
           <tuv xml:lang="de"><seg>Nachricht4</seg></tuv>
           <tuv xml:lang="en"><seg>message4</seg></tuv>
     <tuv xml:lang="zh"><seg>世界 你好!</seg></tuv>
     <tuv xml:lang="ja"><seg>新たな需要4</seg></tuv>
     <tuv xml:lang="ar"><seg>الطلب الجديد4</seg></tuv>
       </tu>
   </body>
</tmx>



Controller.php

public function indexAction()
    {
        //echo time();
        $path = dirname(dirname(dirname(dirname(dirname(__FILE__))))).DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR;
        //echo $path;  exit();
         $frontendOptions = array(
        'lifetime' => 10,
        'automatic_serialization' => true);
        $backendOptions = array(
        //'cache_dir' => dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'systemcache'.DIRECTORY_SEPARATOR,
        'cache_dir' => dirname(dirname(dirname(dirname(__FILE__)))).DIRECTORY_SEPARATOR.'systemcache'.DIRECTORY_SEPARATOR,
        );

        //$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

        Zend_Translate::setCache($cache);
        $translate = new Zend_Translate(
        array(
        'adapter' => 'Tmx',//Tbx Tmx
        //'content' => 'path/to/mytranslation.tmx',
        //'content' => $path.'multi.xml',
        //'content' => $path.'aaa.xml',
        'content' => $path.'mytranslation.tmx',
        //'content' => 'mytranslation.tmx',
        'locale'  => 'ar'
        )
        );

        //$translate->setLocale('en');
        print $translate->_("message1") . "\n <br />";
        print $translate->_("message2") . "\n <br />";
        print $translate->_("message3") . "\n <br />";
        print $translate->_("hello world") . "\n <br />";
        //print $translate->_("New Demand") . "\n <br />";


    }


你可能感兴趣的:(Zend_Translate 多语言化之Zend_Translate_Adapter_Tmx 翻译适配器)