Source for file gettext.php
Documentation is available at gettext.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains the Translation2_Admin_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-2007 Lorenzo Alberton, Michael Wallner
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: gettext.php,v 1.30 2007/11/10 00:02:50 quipo Exp $
* @link http://pear.php.net/package/Translation2
* require Translation2_Container_gettext class
require_once 'Translation2/Container/gettext.php';
* Storage driver for storing/fetching data to/from a gettext file
* This storage driver requires the gettext extension
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @author Michael Wallner <mike@php.net>
* @copyright 2004-2007 Lorenzo Alberton, Michael Wallner
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: gettext.php,v 1.30 2007/11/10 00:02:50 quipo Exp $
* @link http://pear.php.net/package/Translation2
var $_fields = array ('name', 'meta', 'error_text', 'encoding');
* Creates a new entry in the langs_avail .ini file.
* @param array $langData language data
* @param string $path path to gettext data dir
* @return mixed Returns true on success or PEAR_Error on failure.
function addLang($langData, $path = null )
$path = $this->_domains[$this->options['default_domain']];
$path .= '/'. $langData['lang_id'] . '/LC_MESSAGES';
include_once 'System.php';
if (!System ::mkdir (array ('-p', $path))) {
'Cannot create new language in path "%s"', $path
* Creates a new entry in the langsAvail .ini file.
* If the file doesn't exist yet, it is created.
* @param array $langData array('lang_id' => 'en',
* 'meta' => 'some meta info',
* 'error_text' => 'not available'
* 'encoding' => 'iso-8859-1',
* @return true|PEAR_Erroron failure
if (PEAR ::isError ($changed = $this->_updateLangData ($langData))) {
return $changed ? $this->_writeLangsAvailFile () : true;
* Add a new entry in the strings domain.
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param array $strings Associative array with string translations.
* Sample format: array('en' => 'sample', 'it' => 'esempio')
* @return true|PEAR_Erroron failure
function add($stringID, $pageID, $strings)
$pageID = $this->options['default_domain'];
foreach ($strings as $lang => $string) {
$this->_queue['add'][$pageID][$lang][$stringID] = $string;
foreach ($strings as $lang => $string) {
$add[$pageID][$lang][$stringID] = $string;
return $this->_add ($add);
* Remove an entry from the domain.
* @param string $stringID string ID
* @param string $pageID page/group ID
* @return true|PEAR_Erroron failure
function remove($stringID, $pageID)
$pageID = $this->options['default_domain'];
$this->_queue['remove'][$pageID][$stringID] = true;
$tmp = array ($pageID => array ($stringID => true ));
return $this->_remove ($tmp);
* Remove all the strings in the given page/group (domain)
* @param string $pageID page/group ID
* @param string $path path to gettext data dir
* @return mixed true on success, PEAR_Error on failure
$pageID = $this->options['default_domain'];
if (!empty ($this->_domains[$pageID])) {
$path = $this->_domains[$pageID];
$path = $this->_domains[$this->options['default_domain']];
if (PEAR ::isError ($e = $this->_removeDomain ($pageID))) {
foreach ($this->langs as $langID => $lang) {
$domain_file = $path . '/'. $langID . '/LC_MESSAGES/'. $pageID . '.';
if (!@unlink($domain_file. 'mo') || !@unlink($domain_file. 'po')) {
return $this->raiseError('Cannot delete page ' . $pageID. ' (file '. $domain_file. '.*)',
* Alias for Translation2_Admin_Container_gettext::add()
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param array $strings strings
function update($stringID, $pageID, $strings)
return $this->add($stringID, $pageID, $strings);
* @param string $langID language ID
* @param bool $force (unused)
* @return true|PEAR_Error
include_once 'System.php';
foreach ((array) $this->_domains as $domain => $path) {
if (is_dir($fp = $path . '/'. $langID)) {
if (PEAR ::isError ($e = System ::rm (array ('-rf', $fp))) || !$e) {
return $e ? $e : PEAR ::raiseError (sprintf(
'Could not remove language "%s" from domain "%s" '.
'in path "%s" (probably insufficient permissions)',
* Update the lang info in the langs_avail file
* @param array $langData language data
* @return mixed Returns true on success or PEAR_Error on failure.
if (PEAR ::isError ($changed = $this->_updateLangData ($langData))) {
return $changed ? $this->_writeLangsAvailFile () : true;
* Get a list of all the domains
* @return true|PEAR_Erroron failure.
if (isset ($this->_queue['remove'])) {
if (PEAR ::isError ($e = $this->_remove ($this->_queue['remove']))) {
if (isset ($this->_queue['add'])) {
if (PEAR ::isError ($e = $this->_add ($this->_queue['add']))) {
* @param array &$bulk array('pageID' => array([languages]))
* @return true|PEAR_Erroron failure.
include_once 'File/Gettext.php';
$gtFile = &File_Gettext ::factory ($this->options['file_type']);
foreach ((array) $bulk as $pageID => $languages) {
//create the new domain on demand
if (!isset ($this->_domains[$pageID])) {
if (PEAR ::isError ($e = $this->_addDomain ($pageID))) {
$path = $this->_domains[$pageID];
if ($path[strlen($path)-1 ] != '/' && $path[strlen($path)-1 ] != '\\') {
$file = '/LC_MESSAGES/'. $pageID . '.'. $this->options['file_type'];
foreach ($languages as $lang => $strings) {
if (is_file($path . $lang . $file)) {
if (PEAR ::isError ($e = $gtFile->load ($path . $lang . $file))) {
if (!isset ($gtFile->meta ['Content-Type'])) {
$gtFile->meta ['Content-Type'] = 'text/plain; charset=';
if (isset ($langs[$lang]['encoding'])) {
$gtFile->meta ['Content-Type'] .= $langs[$lang]['encoding'];
$gtFile->meta ['Content-Type'] .= $this->options['default_encoding'];
foreach ($strings as $stringID => $string) {
$gtFile->strings [$stringID] = $string;
if (PEAR ::isError ($e = $gtFile->save ($path . $lang . $file))) {
$this->cachedDomains[$lang][$pageID] = $gtFile->strings;
* @param array &$bulk array('pageID' => array([languages]))
* @return true|PEAR_Erroron failure.
include_once 'File/Gettext.php';
$gtFile = &File_Gettext ::factory ($this->options['file_type']);
foreach ($this->getLangs('ids') as $lang) {
foreach ((array) $bulk as $pageID => $stringIDs) {
'%s/%s/LC_MESSAGES/%s.%s',
$this->_domains[$pageID],
if (PEAR ::isError ($e = $gtFile->load ($file))) {
unset ($gtFile->strings [$stringID]);
if (PEAR ::isError ($e = $gtFile->save ($file))) {
$this->cachedDomains[$lang][$pageID] = $gtFile->strings;
* Add the path-to-the-new-domain to the domains-path-INI-file
* @param string $pageID domain name
* @return true|PEAR_Erroron failure
function _addDomain ($pageID)
$domain_path = count($this->_domains) ? reset($this->_domains) : 'locale/';
'Cannot write to domains path INI file "%s"',
$this->options['domains_path_file']
$CRLF = $this->options['carriage_return'];
if (@flock($f, LOCK_EX )) {
fwrite($f, $CRLF . $pageID . ' = ' . $domain_path . $CRLF);
$this->_domains[$pageID] = $domain_path;
* Remove the path-to-the-domain from the domains-path-INI-file
* @param string $pageID domain name
* @return true|PEAR_Erroron failure
function _removeDomain ($pageID)
$domain_path = count($this->_domains) ? reset($this->_domains) : 'locale/';
'Cannot write to domains path INI file "%s"',
$this->options['domains_path_file']
$CRLF = $this->options['carriage_return'];
if (@flock($f, LOCK_EX )) {
foreach ($pages as $page) {
unset ($this->_domains[$pageID]);
// {{{ _writeLangsAvailFile()
* Write the langs_avail INI file
* @return true|PEAR_Erroron failure.
function _writeLangsAvailFile ()
if (PEAR ::isError ($langs = $this->getLangs())) {
'Cannot write to available langs INI file "%s"',
$CRLF = $this->options['carriage_return'];
foreach ($langs as $id => $data) {
fwrite($f, '['. $id . ']'. $CRLF);
foreach ($this->_fields as $k) {
fwrite($f, $k . ' = ' . $data[$k] . $CRLF);
* @param array $langData language data
* @return true|PEAR_Erroron failure.
function _updateLangData ($langData)
if (PEAR ::isError ($langs = $this->getLangs())) {
$lang = &$langs[$langData['lang_id']];
foreach ($this->_fields as $k) {
if ( isset ($langData[$k]) &&
(!isset ($lang[$k]) || $langData[$k] != $lang[$k])) {
$lang[$k] = $langData[$k];
$lang['id'] = $langData['lang_id'];
Documentation generated on Fri, 14 Nov 2008 11:30:22 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|