+ -
当前位置:首页 → 问答吧 → 网站多语言支持之自写I18n解析类使用方法及源码下载

网站多语言支持之自写I18n解析类使用方法及源码下载

时间:2008-11-03

来源:互联网

注:此文摘自本人博客,下面的下载地址链接也是本博客地址。

本文中的I18n是什么?
I18n是一个由PHP语言编写的语言文件解析类,经过测试已经完全支持PHP4,PHP5。它能正常解析格式为
msgid "id"
msgstr "编号"
的语言文件。使您能够更好,更方便的规划一个多语网站。

I18n类:
[php]/**  
* 多语言支持i18n类  
* @author dx_andy  
* @version 1.0.0  
* @since 2008.10.30-  
* @copyright http://www.phplamp.com  
* @copyright http://www.phplamp.org  
* @copyright http://www.ucake.cn  
*/  
if (!defined('DS')) define ('DS', DIRECTORY_SEPARATOR);   
class I18n {   
      
    // 当前的语言   
    var $language = null;   
      
    // 语言文件所在路径   
    var $path = null;   
      
    // 语言文件的类型   
    var $category = "lc_messages";   
      
    // 语言文件   
    var $po = "default";   
      
    // 语言文件的扩展名   
    var $ext = ".po";   
      
    /*以下变量最好不要更改*/  
      
    // 翻译后的健值对应数据   
    var $_T = array();   
      
    /**  
     * I18n初始化 For Php4  
     */  
    function I18n($conf = array()) {   
        if (is_array($conf) && !empty($conf)) {   
            if (key_exists("language", $conf)) $this->language = $conf['language'];   
            else $this->error("未定义语言");   
               
            if (key_exists("path", $conf)) $this->path = $conf['path'];   
               
            if (key_exists("category", $conf)) $this->category = $conf['category'];   
               
            if (key_exists("po", $conf)) $this->po = $conf['po'];   
            else $this->po = "default";   
               
            if (key_exists("ext", $conf)) $this->ext = $conf['ext'];   
            else $this->ext = ".po";   
        }   
    }   
      
    /**  
     * I18n初始化 For Php5  
     */  
    function __construct($conf = array()) {   
        $this->I18n($conf);   
    }   
      
    /**  
     * 要翻译的字符串  
     * @param string $string 要翻译的字符串  
     */  
    function translate($string = null) {   
        if (empty($this->_T)) {   
            // 读取语言文件   
            if (file_exists($this->path . "locale" . DS . $this->language . DS . $this->category . DS . $this->po . $this->ext)) {   
                $_F = fopen($this->path . "locale" . DS . $this->language . DS . $this->category . DS . $this->po . $this->ext, "r");   
            } else $this->error("没有找到名为:" . $this->po . $this->ext . "的语言文件");   
            // 解析语言文件   
            do {   
                $record = fgets($_F, 1024);   
                if (preg_match("/msgid[[:space:]]+\"(.+)\"/i", $record, $regs)) {   
                    $key = $regs[1];   
                    $type = 1;   
                } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"/i", $record, $regs) && $key && $type == 1) {   
                    $this->_T[$key] = $regs[1];   
                    unset($key);   
                    unset($type);   
                } elseif (preg_match("/msgid[[:space:]]+\'(.+)\'/i", $record, $regs)) {   
                    $key = $regs[1];   
                    $type = 2;   
                } elseif (preg_match("/msgstr[[:space:]]+\'(.+)\'/i", $record, $regs) && $key && $type == 2) {   
                    $this->_T[$key] = $regs[1];   
                    unset($key);   
                    unset($type);   
                }   
            } while (!feof($_F));   
            @fclose($_F);   
        }   
        return $this->__tranlate($string);   
    }   
      
    /**  
     * 取出翻译结果  
     * @param string $string 要翻译的字符串  
     * @return string  
     */  
    function __tranlate($string = null) {   
        if ($string && !empty($this->_T)) {   
            if (key_exists($string, $this->_T))   
                return $this->_T[$string];   
        }   
        return $string;   
    }   
      
    /**  
     * 错误信息  
     * @param string $errorMsg 要显示的错误信息  
     * @return null  
     */  
    function error($errorMsg) {   
        if ($errorMsg) echo $errorMsg;   
        exit;   
    }   
      
}  [/php]

常规PHP项目使用方法与I18n类下载

如果您也像我一样热衷于CakePHP框架稳定版本,那么你可以试一下本人手写的这个I18n Vendor。使用非常方便。就算CakePHP1.2版的稳定版出来后,也不会因为升级而考虑到重新编写语言文件。因为这个I1n8类是完全按照CakePHP1.2版本的多语言支持来写的。
使用前请先下载此I18n类文件。然后打开你的Cake核心目录中的basics.php。修改其内的__()函数为如下内容
[php]/**  
* I18n Vendor要用到的翻译函数  
* @param string $string 要翻译的字符串  
* @param boolean $return 返回值或直接输出  
* @return unknow  
*/  
function __($string = null, $return = false) {   
    if (!empty($string)) {   
        if (!class_exists("I18n") || !key_exists("_i18n", $GLOBALS)) {   
            vendor("i18n");   
            $conf = array(   
                "language" => Configure::read("Config.language"),   
                "path" => APP,   
            );   
            global $_i18n;   
            $_i18n = new I18n($conf);   
        }   
        $string = $GLOBALS['_i18n']->translate($string);   
    }   
    if (!$return) echo $string;   
    else return $string;   
}  [/php]

然后打开app_controller.php添加如下内容
class AppController extends Controller {   
      
    /*定义网站语言*/  
    var $language = "zh_cn";   
  
    // 语言设定   
    function beforeRender() {   
        //{{{ Start 网站语言   
        if (!$this->language) $language = "zh_cn";   
        Configure::write("Config.language", $this->language);   
        //}}} End 网站语言   
    }   
      
}  

这样配置好后,你可以很方便的配置一个Controller的语言。比如:

class UsersController extends AppController {   
  
    var $language = "zh_cn";   
      
    function admin_login() {   
  
        /*您的代码*/  
  
    }   
}  

当然你也可以在Action中设定此Action的语言,例如:
class UsersController extends AppController {   
      
    function admin_login() {   
  
        $this->language = "zh_cn";   
  
        /*您的代码*/  
  
    }   
}  

好了,假如你对此种方法很感兴趣,你马上可以在这里下载到。

点击下载CakePHP稳定版I18n Vendor及使用方法

作者: dx_andy   发布时间: 2008-11-03

作者: nianjin   发布时间: 2008-11-04