Smarty多語言化的簡單實現

  <script type="text/javascript">document.domain = "csdn.net";</script>

Smarty是一個使用PHP寫出來的模板PHP模板引擎,是目前業界最著名的PHP模板引擎之一。它分離了邏輯代碼和外在的內容,提供了一種易於管理和使用的方法,用來將原本與HTML 代碼混雜在一起PHP 代碼邏輯分離。

 

用過Smarty的人都知道Smarty是個好東西。做模板一定要多語言化,可是Smarty卻沒有實現這功能,無聊之際自己簡單實現了把。

看了幾個開源框架的多語言化實現方法,都是替換實現的,或許這是最好的方法吧,誰有更好的實現技術,請通知我一下,一起學習。

 

下面進入正題

Smarty 是通過緩存技術來實現的PHP模板引擎,通過事先把模板編譯並緩存起來。對於模板編譯成的php文件的命名,有自己有命名規則。具體是什麼,在這就不深加討論了,有興趣的可以自己研究源碼。要想實現多語言化,並不破壞Smarty的優越性,那麼,模板編譯成的php文件也應該是分多語言的。也就是說生成諸如en.php和zh.php的模板php文件。在模板編譯保存之前需要採用替換技術,對模板進行多語言化,在此我選擇了重寫 _fetch_resource_info(在提取模板時就對其進行多語言化),同樣的,需要重寫Smarty的_get_compile_path以便區別是哪種語言下的模板,這也就是對模板編譯後保存的php文件名進行了多語言區分。

 

下面是實現代碼

//Template.class.php
<?php
 require 'smarty/libs/Smarty.class.php';

 /*
  * Template extends Smarty
  */
 class Template extends Smarty{

  /*
   * overwrite _fetch_resource_info
   * add _translate_source_content
   */
  function _fetch_resource_info(&$params)
  {
   $_return = parent::_fetch_resource_info($params);
   if($_return) 
    $this->_translate_source_content($params['source_content']);
   return $_return;
  }

  /*
   * overwrite _get_compile_path
   * add path identify by language
   */
   function _get_compile_path($resource_name)
  {
    if(!isset($GLOBALS['language']))
     $this->_set_Locale_language();
   return parent::_get_auto_filename($this->compile_dir, $resource_name,
            $this->_compile_id) .'.'.$GLOBALS['language']. '.php';
  }

  /*
   * set the language,get from browser
   */
  function _set_Locale_language(){
   preg_match('/^([a-z/-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
   $GLOBALS['language'] = $matches[1];
   if($GLOBALS['language'] == '')
    $GLOBALS['language'] = 'en';
  }
  
  /*
   * translate the template
   */
  function _translate_source_content(&$source_content){
   $dir = 'languages';
   if(!isset($GLOBALS['language']))
     $this->_set_Locale_language();

   $language = $GLOBALS['language'];
   if(!is_file($dir.'/'.$language.'.php'))
    $language = 'en';
   $languageFile = $dir.'/'.$language.'.php';

   if (is_file($languageFile) && include ($languageFile))
    $source_content = preg_replace('///[//#([^//]//[]+)]/Usie',"///$GLOBALS[///$language]['//1' ]", $source_content);
  }
 }
?>

 

在此之前還要把相關的多語言化內容加載進來。

定義一個加載言語的方法。在些直接放到言語文件中了。

//en.php

<?php
 
 $english = array(
  //global
  "hello"      => "你好",
  "world"      => "Smarty",
 );

 add_translation("zh",$english);


 function add_translation($country_code, $language_array) {

  $country_code = strtolower($country_code);
  $country_code = trim($country_code);
  if (is_array($language_array) && sizeof($language_array) > 0 && $country_code != "") {
   
   if (!isset($GLOBALS[$country_code])) {
    $GLOBALS[$country_code] = $language_array;
   } else {
    $GLOBALS[$country_code] = array_merge($GLOBALS[$country_code],$language_array);
   }
   return true;
   
  }
  return false;
  
 }
?>

現在只要在模板中定義如 [#hello] [#world]  並把相應的翻譯內容加入對應的語言文件中就可以了。

到些Smarty多語言化實現結束。

 

總結:

1.定義翻譯內容的語言文件

2.定義加載翻譯內容的方法 add_translation

3.定義獲取言語的方法 _set_Locale_language

3.定義獲翻譯的方法 _translate_source_content

4.重載_fetch_resource_info對模板進行翻譯

5.重載_get_compile_path按多言語化的要求生成編譯後的模板php文件名。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章