zend framework 多語言配置及實例

1.11版本php

application.ini中的配置:html

resources.locale.default = "zh_CN"session

resources.translate.registry_key = "Zend_Translate"app

resources.translate.adapter = arraythis

resources.translate.options.scan = "directory"spa

resources.translate.locale = "zh_CN"orm

resources.translate.data.zh_CN = APPLICATION_PATH "/languages/zh_CN.php"htm

resources.translate.data.en_US = APPLICATION_PATH "/languages/en_US.php"ip



Bootstrap.php以下:文檔

classBootstrap extends Zend_Application_Bootstrap_Bootstrap

{

protected function_initTranslate ()

    {

        $options = $this->getOption('resources');

        $options = $options['translate'];

        if (! isset($options['data'])) {

            throw new Zend_Application_Resource_Exception(

           '對不起,沒有找到語言文件!');

        }

        $adapter = isset($options['adapter']) ? $options['adapter'] : Zend_Translate::AN_ARRAY;

        $session = new Zend_Session_Namespace('aa');

        if ($session->locale) {

            $locale = $session->locale;

        } else {

            $locale =isset($options['locale']) ? $options['locale'] :null;

        }

        $data = '';

        if (isset($options['data'][$locale])) {

            $data = $options['data'][$locale];

        }

        $translateOptions =isset($options['options']) ? $options['options'] :array();

        $translate =new Zend_Translate($adapter, $data, $locale, 

        $translateOptions);

        Zend_Form::setDefaultTranslator($translate);

        Zend_Registry::set('Zend_Translate', $translate);

        return $translate;

    }

}


IndexController.php以下:

 public functionchangeAction()

    {

     $this->getFrontController()->setParam('noViewRenderer',true);

     $language=$this->getRequest()->getParam('language');

     $lang=new Zend_Session_Namespace('aa');

     if ($language == 'zh_CN'){

     $lang->locale='zh_CN';

     }elseif($language == 'en_US'){

     $lang->locale='en_US';

     }else{

     $lang->locale='zh_CN';

     }

   echo "<script>location.href=document.referrer;</script>";

    }


視圖index.phtml以下:

<div>

<h1><?phpecho $this->translate('teacher');?></h1>

<h2><?phpecho $this->translate('hello_world');?></h2>

<h3><?phpecho $this->translate('document');?></h3>

<ahref="<?phpecho $this->baseUrl();?>/index/change/language/<?phpecho $this->translate('get_lg');?>">

<?phpecho $this->translate('language');?></a>

</div>


application/languages下的兩個語言文件:

en_US.php文件以下:

<?php 

return array(

'hello_world'=> 'Hello_World',

'document'  => 'Document',

'teacher'  => 'Teacher',

'get_lg'  => 'zh_CN',

'language'  => '簡體中文',

);

?>


zh_CN.php文件以下:

<?php 

return array(

'hello_world'=>'你好世界',

'document'=>'文檔',

'teacher'=>'老師',

'get_lg'=>'en_US',

'language'=>'English',

);

?>

相關文章
相關標籤/搜索