Source for file gettext.php
Documentation is available at gettext.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains the Translation2_Container_gettext class
* LICENSE: Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @author Michael Wallner <mike@php.net>
* @copyright 2004-2008 Lorenzo Alberton, Michael Wallner
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: gettext.php,v 1.32 2008/02/02 18:05:06 quipo Exp $
* @link http://pear.php.net/package/Translation2
* require Translation2_Container class
require_once 'Translation2/Container.php';
* require I18Nv2 for locale handling
require_once 'I18Nv2.php';
* Storage driver for fetching data with gettext
* This storage driver requires the gettext extension
* and the PEAR::I18Nv2 class for locale handling
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @author Michael Wallner <mike@php.net>
* @copyright 2004-2008 Lorenzo Alberton, Michael Wallner
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: gettext.php,v 1.32 2008/02/02 18:05:06 quipo Exp $
* @link http://pear.php.net/package/Translation2
* @see /docs/gettext_readme.txt for an usage example
var $cachedDomains = array ();
* Initialize the container
* @param array $options gettext parameters
* @return boolean|PEAR_Errorobject if domains INI file doesn't exist
$this->_setDefaultOptions ();
!$this->options['blank_on_missing']
'Cannot find domains INI file "%s" [%s on line %d]',
$this->options['domains_path_file'], __FILE__ , __LINE__
foreach ((array) $this->_domains as $domain => $path) {
// {{{ _setDefaultOptions()
* Set some default options
function _setDefaultOptions ()
$this->options['langs_avail_file'] = 'langs.ini';
$this->options['domains_path_file'] = 'domains.ini';
$this->options['default_domain'] = 'messages';
$this->options['carriage_return'] = "\n";
$this->options['file_type'] = 'mo';
$this->options['default_lang'] = 'en';
$this->options['default_encoding'] = 'iso-8859-1';
$this->options['blank_on_missing'] = false;
* @param string $langID new langID
* @return string previous langID
function _switchLang ($langID)
$langID = $this->_getLangID ($langID);
$oldLang = $this->currentLang['id'];
* Fetch the available langs if they're not cached yet.
foreach ((array) $this->langs as $id => $lang) {
$this->langs[$id]['id'] = $id;
* @param string $langID language ID
* @return array language data
if (!PEAR ::isError ($langData = parent ::setLang($langID))) {
I18Nv2 ::setLocale ($langID);
* Get all the strings from a domain (parsing the .mo file)
* @param string $pageID page/group ID
* @param string $langID language ID
* @return array|PEAR_Error
function getPage($pageID = null , $langID = null )
$oldLang = $this->_switchLang ($langID);
$curLang = $this->currentLang['id'];
$pageID = $this->options['default_domain'];
if (isset ($this->cachedDomains[$curLang][$pageID])) {
$this->_switchLang ($oldLang);
return $this->cachedDomains[$curLang][$pageID];
if (!isset ($this->_domains[$pageID])) {
$this->_switchLang ($oldLang);
'The domain "%s" was not specified in the domains INI '.
'file "%s" [%s on line %d]', $pageID,
$this->options['domains_path_file'], __FILE__ , __LINE__
include_once 'File/Gettext.php';
$gtFile = &File_Gettext ::factory ($this->options['file_type']);
$path = $this->_domains[$pageID] . '/'. $curLang . '/LC_MESSAGES/';
$file = $path . $pageID . '.'. $this->options['file_type'];
if (PEAR ::isError ($e = $gtFile->load ($file))) {
$this->_switchLang ($oldLang);
'%s [%s on line %d]', $e->getMessage (), __FILE__ , __LINE__
$this->_switchLang ($oldLang);
'Cannot find file "%s" [%s on line %d]',
$file, __FILE__ , __LINE__
$this->cachedDomains[$curLang][$pageID] = $gtFile->strings;
$this->_switchLang ($oldLang);
* Get a single item from the container, without caching the whole page
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param string $langID language ID
function getOne($stringID, $pageID = null , $langID = null )
$oldLang = $this->_switchLang ($langID);
$curLang = $this->currentLang['id'];
$pageID = $this->options['default_domain'];
$this->_switchLang ($oldLang);
$page = $this->getPage($pageID, $langID);
if (PEAR ::isError ($page = $this->getPage($pageID, $langID))) {
return $this->raiseError($page->getMessage (), $page->getCode ());
// return original string if there's no translation available
if (isset ($page[$stringID]) && strlen($page[$stringID])) {
} else if (false == $this->options['blank_on_missing']) {
* Get the stringID for the given string
* @param string $string string
* @param string $pageID page/group ID
* @return string|PEAR_Error
$pageID = $this->options['default_domain'];
'The domain "%s" was not specified in the domains '.
'INI file "%s" [%s on line %d]', $pageID,
$this->options['domains_path_file'], __FILE__ , __LINE__
Documentation generated on Tue, 06 May 2008 06:00:29 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|