[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/ -> Translation.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1