Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 598 lines (546 sloc) 17.497 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
* Contains the Translation2 base class
*
* PHP versions 4 and 5
*
* 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
* @package Translation2
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2004-2008 Lorenzo Alberton
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id$
* @link http://pear.php.net/package/Translation2
*/

/**
* require PEAR base class
*/
require_once 'PEAR.php';

/**
* Allows redefinition of the default pageID.
* This constant is needed to allow both NULL and EMPTY pageID values
* and to have them match
*/
if (!defined('TRANSLATION2_DEFAULT_PAGEID')) {
    define('TRANSLATION2_DEFAULT_PAGEID', 'translation2_default_pageID');
}
/**
* Class Error codes
*/
define('TRANSLATION2_ERROR', -1);
define('TRANSLATION2_ERROR_METHOD_NOT_SUPPORTED', -2);
define('TRANSLATION2_ERROR_CANNOT_CONNECT', -3);
define('TRANSLATION2_ERROR_CANNOT_FIND_FILE', -4);
define('TRANSLATION2_ERROR_DOMAIN_NOT_SET', -5);
define('TRANSLATION2_ERROR_INVALID_PATH', -6);
define('TRANSLATION2_ERROR_CANNOT_CREATE_DIR', -7);
define('TRANSLATION2_ERROR_CANNOT_WRITE_FILE', -8);
define('TRANSLATION2_ERROR_UNKNOWN_LANG', -9);
define('TRANSLATION2_ERROR_ENCODING_CONVERSION', -10);
define('TRANSLATION2_ERROR_UNSUPPORTED', -11);

/**
* Translation2 base class
*
* This class provides an easy way to retrieve all the strings
* for a multilingual site or application from a data source
* (i.e. a db, an xml file or a gettext file).
*
* @category Internationalization
* @package Translation2
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2004-2008 Lorenzo Alberton
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @link http://pear.php.net/package/Translation2
*/
class Translation2
{
    // {{{ class vars

    /**
* Storage object
* @var object
* @access protected
*/
    var $storage = '';

    /**
* Class options
* @var array
*/
    var $options = array();

    /**
* Default lang
* @var array
* @access protected
*/
    var $lang = array();

    /**
* Current pageID
* @var string
* @access protected
*/
    var $currentPageID = null;

    /**
* Array of parameters for the adapter class
* @var array
* @access protected
*/
    var $params = array();

    // }}}
    // {{{ Constructor

    /**
* Constructor
*/
    function Translation2()
    {
        if (func_num_args()) {
            $msg = '<b>Translation2 error:</b>'
                  .' Don\'t use the constructor - use factory()';
            trigger_error($msg, E_USER_ERROR);
        }
    }

    // }}}
    // {{{ factory()

    /**
* Return a Translation2 instance already initialized
*
* @param string $driver Type of the storage driver
* @param mixed $options Additional options for the storage driver
* (example: if you are using DB as the storage
* driver, you have to pass the dsn string here)
* @param array $params Array of parameters for the adapter class
* (i.e. you can set here the mappings between your
* table/field names and the ones used by this class)
*
* @return object Translation2 instance or PEAR_Error on failure
* @static
*/
    function & factory($driver, $options = '', $params = array())
    {
        $tr = new Translation2;
        $tr->storage = Translation2::_storageFactory($driver, $options);
        if (PEAR::isError($tr->storage)) {
            return $tr->storage;
        }
        $tr->_setDefaultOptions();
        $tr->_parseOptions($params);
        $tr->storage->_parseOptions($params);
        return $tr;
    }

    // }}}
    // {{{ _storageFactory()

    /**
* Return a storage driver based on $driver and $options
*
* @param string $driver Type of storage class to return
* @param string $options Optional parameters for the storage class
*
* @return object Object Storage object
* @static
* @access private
*/
    function & _storageFactory($driver, $options = '')
    {
        $storage_path = 'Translation2/Container/'.strtolower($driver).'.php';
        $storage_class = 'Translation2_Container_'.strtolower($driver);
        include_once $storage_path;
        $storage = new $storage_class;
        $err = $storage->init($options);
        if (PEAR::isError($err)) {
            return $err;
        }
        return $storage;
    }

    // }}}
    // {{{ setContainerOptions()

    /**
* Set some storage driver options
*
* @param array $options array of options
*
* @return void
* @access protected
*/
    function setContainerOptions($options)
    {
        $this->storage->_parseOptions($options);
    }

    // }}}
    // {{{ _setDefaultOptions()

    /**
* Set some default options
*
* @return void
* @access private
*/
    function _setDefaultOptions()
    {
        $this->options['ParameterPrefix'] = '&&';
        $this->options['ParameterPostfix'] = '&&';
        $this->options['ParameterAutoFree'] = true;
        $this->options['prefetch'] = true;
    }

    // }}}
    // {{{ _parseOptions()

    /**
* Parse options passed to the base class
*
* @param array $array options
*
* @return void
* @access private
*/
    function _parseOptions($array)
    {
        foreach ($array as $key => $value) {
            if (isset($this->options[$key])) {
                $this->options[$key] = $value;
            }
        }
    }

    // }}}
    // {{{ getDecorator()

    /**
* Return an instance of a decorator
*
* This method is used to get a decorator instance.
* A decorator can be seen as a filter, i.e. something that can change
* or handle the values of the objects/vars that pass through.
*
* @param string $decorator Name of the decorator
*
* @return object Decorator object reference
*/
    function & getDecorator($decorator)
    {
        $decorator_path = 'Translation2/Decorator/'.$decorator.'.php';
        $decorator_class = 'Translation2_Decorator_'.$decorator;
        include_once $decorator_path;
        if (func_num_args() > 1) {
            $obj = func_get_arg(1);
            $new_decorator = new $decorator_class($obj);
        } else {
            $new_decorator = new $decorator_class($this);
        }
        return $new_decorator;
    }

    // }}}
    // {{{ setCharset()

    /**
* Set charset used to read/store the translations
*
* @param string $charset character set (encoding)
*
* @return void|PEAR_Error
*/
    function setCharset($charset)
    {
        $res = $this->storage->setCharset($charset);
        if (PEAR::isError($res)) {
            return $res;
        }
    }

    // }}}
    // {{{ setLang()

    /**
* Set default lang
*
* Set the language that shall be used when retrieving strings.
*
* @param string $langID language code (for instance, 'en' or 'it')
*
* @return true|PEAR_Error
*/
    function setLang($langID)
    {
        $res = $this->storage->setLang($langID);
        if (PEAR::isError($res)) {
            return $res;
        }
        $this->lang = $res;
        return true;
    }

    // }}}
    // {{{ setPageID($pageID)

    /**
* Set default page
*
* Set the page (aka "group of strings") that shall be used when retrieving strings.
* If you set it, you don't have to state it in each get() call.
*
* @param string $pageID ID of the default page
*
* @return self
*/
    function setPageID($pageID = null)
    {
        $this->currentPageID = $pageID;
        return $this;
    }

    // }}}
    // {{{ getLang()

    /**
* get lang info
*
* Get some extra information about the language (its full name,
* the localized error text, ...)
*
* @param string $langID language ID
* @param string $format ['name', 'meta', 'error_text', 'array']
*
* @return mixed [string | array], depending on $format
*/
    function getLang($langID = null, $format = 'name')
    {
        if (is_null($langID)) {
            if (!isset($this->lang['id'])) {
                $msg = 'Translation2::getLang(): unknown language "'.$langID.'".'
                      .' Use Translation2::setLang() to set a default language.';
                return $this->storage->raiseError($msg, TRANSLATION2_ERROR_UNKNOWN_LANG);
            }
            $langID = $this->lang['id'];
        }
        $lang = $this->storage->getLangData($langID);
        if ($format == 'array') {
            return $lang;
        } elseif (isset($lang[$format])) {
            return $lang[$format];
        } elseif (isset($lang['name'])) {
            return $lang['name'];
        }
        $msg = 'Translation2::getLang(): unknown language "'.$langID.'".'
              .' Use Translation2::setLang() to set a default language.';
        return $this->storage->raiseError($msg, TRANSLATION2_ERROR_UNKNOWN_LANG);
    }

    // }}}
    // {{{ getLangs()

    /**
* get langs
*
* Get some extra information about the languages (their full names,
* the localized error text, their codes, ...)
*
* @param string $format ['ids', 'names', 'array']
*
* @return array|PEAR_Error
*/
    function getLangs($format = 'name')
    {
        return $this->storage->getLangs($format);
    }

    // }}}
    // {{{ setParams()

    /**
* Set parameters for next string
*
* Set the replacement for the parameters in the string(s).
* Parameter delimiters are customizable.
*
* @param array $params array of replacement parameters
*
* @return self
*/
    function setParams($params = null)
    {
        if (empty($params)) {
            $this->params = array();
        } elseif (is_array($params)) {
            $this->params = $params;
        } else {
            $this->params = array($params);
        }
        return $this;
    }

    // }}}
    // {{{ _replaceParams()

    /**
* Replace parameters in strings
*
* @param mixed $strings strings where the replacements must occur
*
* @return mixed
* @access protected
*/
    function _replaceParams($strings)
    {
        if (empty($strings) || is_object($strings) || !count($this->params)) {
            return $strings;
        }
        if (is_array($strings)) {
            foreach ($strings as $key => $string) {
                $strings[$key] = $this->_replaceParams($string);
            }
        } else {
            if (strpos($strings, $this->options['ParameterPrefix']) !== false) {
                foreach ($this->params as $name => $value) {
                    $strings = str_replace($this->options['ParameterPrefix']
                                           . $name . $this->options['ParameterPostfix'],
                                           $value,
                                           $strings);
                }
                if ($this->options['ParameterAutoFree']) {
                    $this->params = array();
                }
            }
        }
        return $strings;
    }

    // }}}
    // {{{ replaceEmptyStringsWithKeys()

    /**
* Replace empty strings with their stringID
*
* @param array $strings array of strings to be replaced if empty
*
* @return array
* @static
*/
    function replaceEmptyStringsWithKeys($strings)
    {
        if (!is_array($strings)) {
            return $strings;
        }
        foreach ($strings as $key => $string) {
            if (empty($string)) {
                $strings[$key] = $key;
            }
        }
        return $strings;
    }

    // }}}
    // {{{ getRaw()

    /**
* Get translated string (as-is)
*
* @param string $stringID ID of the string to be translated
* @param string $pageID ID of the page/group containing the string
* @param string $langID ID of the language
* @param string $defaultText Text to display when the string is empty
*
* @return string|PEAR_Error
*/
    function getRaw($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
    {
        $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID);
        $str = $this->storage->getOne($stringID, $pageID, $langID);
        if (empty($str)) {
            $str = $defaultText;
        }
        return $str;
    }

    // }}}
    // {{{ get()

    /**
* Get translated string
*
* First check if the string is cached, if not => fetch the page
* from the container and cache it for later use.
* If the string is empty, check the fallback language; if
* the latter is empty too, then return the $defaultText.
*
* @param string $stringID ID of the string
* @param string $pageID ID of the page/group containing the string
* @param string $langID ID of the language
* @param string $defaultText Text to display when the string is empty
* NB: This parameter is only used in the DefaultText decorator
*
* @return string
*/
    function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
    {
        $str = $this->getRaw($stringID, $pageID, $langID);
        if (PEAR::isError($str)) {
            return $str;
        }
        return $this->_replaceParams($str);
    }

    // }}}
    // {{{ getRawPage()

    /**
* Get the array of strings in a page
*
* Fetch the page (aka "group of strings) from the container,
* without applying any formatting and without replacing the parameters
*
* @param string $pageID ID of the page/group containing the string
* @param string $langID ID of the language
*
* @return array
*/
    function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
    {
        $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID);
        return $this->storage->getPage($pageID, $langID);
    }

    // }}}
    // {{{ getPage()

    /**
* Get an entire group of strings
*
* Same as getRawPage, but resort to fallback language and
* replace parameters when needed
*
* @param string $pageID ID of the page/group containing the string
* @param string $langID ID of the language
*
* @return array
*/
    function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
    {
        $pageData = $this->getRawPage($pageID, $langID);
        return $this->_replaceParams($pageData);
    }

    // }}}
    // {{{ getStringID()

    /**
* Get the stringID for the given string. This method is the reverse of get().
*
* @param string $string This is NOT the stringID, this is a real string.
* The method will search for its matching stringID, and then
* it will return the associate string in the selected language.
* @param string $pageID ID of the page/group containing the string
*
* @return string
*/
    function getStringID($string, $pageID = TRANSLATION2_DEFAULT_PAGEID)
    {
        $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID);
        return $this->storage->getStringID($string, $pageID);
    }

    // }}}
    // {{{ __clone()

    /**
* Clone internal object references
*
* This method is called automatically by PHP5
*
* @return void
* @access protected
*/
    function __clone()
    {
        $this->storage = clone($this->storage);
    }

    // }}}
}
?>
Something went wrong with that request. Please try again.