I18N with gettext
| From: | Marcos Lois Bermúdez | Date: | Tue, 03 Dec 2002 11:56:34 +0000 |
| Subject: | I18N with gettext | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-11312@lists.php.net to get a copy of this message | ||
I write a little warper to gettext, i send here the file, it works, but i
don't know it's all correct.
Regards.
-----------------------------------------------
Marcos Lois Bermúdez
marcos.lois@iqc-services.com
IQC - Services
Avda. Garcia Barbón, 90 / 4ºB
36201 Vigo (Pontevedra)
Tel: +34 986.884.244
Fax: +34 986.415.074
http://www.iqc-services.com
-----------------------------------------------
<?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Marcos Lois <marcos.lois@securez.org> | // | | // +----------------------------------------------------------------------+ // // $Id: // require_once 'Common.php'; /** * Get message, from a gettext mo file, and return corresponding message * when get() is called. * * Full path to the file may look like this: * /usr/local/apache/htdocs/lang/ja/LC_MESSAGES/general.mo * In this case, you can use I18N_Messages_File as follows: * your script: * $domain = 'general'; * $lang = 'ja'; * $dir = '/usr/local/apache/htdocs/lang/'; * require_once 'I18N/Messages/Gettext.php'; * $i18n =& I18N_Messages_Gettext($lang,$domain,$dir); * $i18n->_('hello'); * */ class I18N_Messages_Gettext extends I18N_Messages_Common { // {{ properties /** * Holds directory information * * @type : string Directory name * @access: private */ var $_dir; // }} // {{ constructor /** * Save Lanuguage and the directory name where language file resides. * Then load the file. * * @param : string Lanuguage Code * @param : string Directory Name * * @return: void * @access: public */ function __construct($lang = 'en', $domain = '',$dir = './') { parent::__construct(); $this->setDir($dir); $this->bindLanguage($lang); $this->bindDomain($domain); $this->_load(); } // }} // {{ I18N_Messages_Gettext() /** * For pre-Zend2 compatibility. Call actual constructor * * @param : string Lanuguage Code * @param : string Directory Name * * @return: void * @access: public */ function I18N_Messages_Gettext($lang = 'en', $domain = '', $dir = './') { $this->__construct($lang,$domain,$dir); } /** * Load the lanuguage file * * @param : string Language code * * @return: void * @access: private */ function _load() { // Set the lang setlocale(LC_ALL, $this->_lang); putenv("LANG=" . $this->_lang); // Bind the domain bindtextdomain($this->_domain, $this->_dir); textdomain($this->_domain); } /** * Set charset of message * * @param : string Charset * * @return: void * @access: public */ function setCharset($charset = I18N_MESSAGES_DEFAULT_CHARSET) { $this->_charset = $charset; bind_textdomain_codeset($this->_domain, $charset); } /** * Set directory * * @return: string Directory name * @access: public */ function setDir($dir) { $this->_dir = $dir; } /** * Return directory name * * @return: string Directory name * @access: public */ function getDir() { return $this->_dir; } function get($messageID) { return gettext($messageID); } } ?>
<?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Marcos Lois <marcos.lois@securez.org> | // | | // +----------------------------------------------------------------------+ // // $Id: // require_once 'Common.php'; /** * Get message, from a gettext mo file, and return corresponding message * when get() is called. * * Full path to the file may look like this: * /usr/local/apache/htdocs/lang/ja/LC_MESSAGES/general.mo * In this case, you can use I18N_Messages_File as follows: * your script: * $domain = 'general'; * $lang = 'ja'; * $dir = '/usr/local/apache/htdocs/lang/'; * require_once 'I18N/Messages/Gettext.php'; * $i18n =& I18N_Messages_Gettext($lang,$domain,$dir); * $i18n->_('hello'); * */ class I18N_Messages_Gettext extends I18N_Messages_Common { // {{ properties /** * Holds directory information * * @type : string Directory name * @access: private */ var $_dir; // }} // {{ constructor /** * Save Lanuguage and the directory name where language file resides. * Then load the file. * * @param : string Lanuguage Code * @param : string Directory Name * * @return: void * @access: public */ function __construct($lang = 'en', $domain = '',$dir = './') { parent::__construct(); $this->setDir($dir); $this->bindLanguage($lang); $this->bindDomain($domain); $this->_load(); } // }} // {{ I18N_Messages_Gettext() /** * For pre-Zend2 compatibility. Call actual constructor * * @param : string Lanuguage Code * @param : string Directory Name * * @return: void * @access: public */ function I18N_Messages_Gettext($lang = 'en', $domain = '', $dir = './') { $this->__construct($lang,$domain,$dir); } /** * Load the lanuguage file * * @param : string Language code * * @return: void * @access: private */ function _load() { // Set the lang setlocale(LC_ALL, $this->_lang); putenv("LANG=" . $this->_lang); // Bind the domain bindtextdomain($this->_domain, $this->_dir); textdomain($this->_domain); } /** * Set charset of message * * @param : string Charset * * @return: void * @access: public */ function setCharset($charset = I18N_MESSAGES_DEFAULT_CHARSET) { $this->_charset = $charset; bind_textdomain_codeset($this->_domain, $charset); } /** * Set directory * * @return: string Directory name * @access: public */ function setDir($dir) { $this->_dir = $dir; } /** * Return directory name * * @return: string Directory name * @access: public */ function getDir() { return $this->_dir; } function get($messageID) { return gettext($messageID); } } ?>