Overview

Packages

  • Translation

Classes

  • Horde_Translation
  • Horde_Translation_Handler_Gettext

Interfaces

  • Horde_Translation_Handler

Exceptions

  • Horde_Translation_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @package Translation
  4:  *
  5:  * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  */
 10: 
 11: /**
 12:  * Horde_Translation is the base class for any translation wrapper classes in
 13:  * libraries that want to utilize the Horde_Translation library for
 14:  * translations.
 15:  *
 16:  * @author  Jan Schneider <jan@horde.org>
 17:  * @package Translation
 18:  */
 19: abstract class Horde_Translation
 20: {
 21:     /**
 22:      * The translation domain, e.g. the library name, for the default gettext
 23:      * handler.
 24:      *
 25:      * @var string
 26:      */
 27:     static protected $_domain;
 28: 
 29:     /**
 30:      * The relative path to the translations for the default gettext handler.
 31:      *
 32:      * This path is relative to the
 33:      *
 34:      * @var string
 35:      */
 36:     static protected $_directory;
 37: 
 38:     /**
 39:      * The handlers providing the actual translations.
 40:      *
 41:      * @var array
 42:      */
 43:     static protected $_handlers = array();
 44: 
 45:     /**
 46:      * Loads a translation handler class pointing to the library's translations
 47:      * and assigns it to $_handler.
 48:      *
 49:      * @param string $handlerClass  The name of a class implementing the
 50:      *                              Horde_Translation_Handler interface.
 51:      */
 52:     static public function loadHandler($handlerClass)
 53:     {
 54:         if (!self::$_domain || !self::$_directory) {
 55:             throw new Horde_Translation_Exception('The domain and directory properties must be set by the class that extends Horde_Translation.');
 56:         }
 57:         self::setHandler(self::$_domain, new $handlerClass(self::$_domain, self::$_directory));
 58:     }
 59: 
 60:     /**
 61:      * Assigns a translation handler object to $_handlers.
 62:      *
 63:      * Type hinting isn't used on purpose. You should extend a custom
 64:      * translation handler passed here from the Horde_Translation interface,
 65:      * but technically it's sufficient if you provide the API of that
 66:      * interface.
 67:      *
 68:      * @param string $domain                      The translation domain.
 69:      * @param Horde_Translation_Handler $handler  An object implementing the
 70:      *                                            Horde_Translation_Handler
 71:      *                                            interface.
 72:      */
 73:     static public function setHandler($domain, $handler)
 74:     {
 75:         self::$_handlers[$domain] = $handler;
 76:     }
 77: 
 78:     /**
 79:      * Returns the translation of a message.
 80:      *
 81:      * @var string $message  The string to translate.
 82:      *
 83:      * @return string  The string translation, or the original string if no
 84:      *                 translation exists.
 85:      */
 86:     static public function t($message)
 87:     {
 88:         if (!isset(self::$_handlers[self::$_domain])) {
 89:             self::loadHandler('Horde_Translation_Handler_Gettext');
 90:         }
 91:         return self::$_handlers[self::$_domain]->t($message);
 92:     }
 93: 
 94:     /**
 95:      * Returns the plural translation of a message.
 96:      *
 97:      * @param string $singular  The singular version to translate.
 98:      * @param string $plural    The plural version to translate.
 99:      * @param integer $number   The number that determines singular vs. plural.
100:      *
101:      * @return string  The string translation, or the original string if no
102:      *                 translation exists.
103:      */
104:     static public function ngettext($singular, $plural, $number)
105:     {
106:         if (!isset(self::$_handlers[self::$_domain])) {
107:             self::loadHandler('Horde_Translation_Handler_Gettext');
108:         }
109:         return self::$_handlers[self::$_domain]->ngettext($singular, $plural, $number);
110:     }
111: }
112: 
API documentation generated by ApiGen