Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE 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-2017 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-2017 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      protected static $_domain;
  34  
  35      /**
  36       * The relative path to the translations for the default gettext handler.
  37       *
  38       * @var string
  39       */
  40      protected static $_directory;
  41  
  42      /**
  43       * The handlers providing the actual translations.
  44       *
  45       * @var array
  46       */
  47      protected static $_handlers = array();
  48  
  49      /**
  50       * Loads a translation handler class pointing to the library's translations
  51       * and assigns it to $_handler.
  52       *
  53       * @param string $handlerClass  The name of a class implementing the
  54       *                              Horde_Translation_Handler interface.
  55       */
  56      public static function loadHandler($handlerClass)
  57      {
  58          if (!static::$_domain || !static::$_directory) {
  59              throw new Horde_Translation_Exception('The domain and directory properties must be set by the class that extends Horde_Translation.');
  60          }
  61          static::setHandler(static::$_domain, new $handlerClass(static::$_domain, static::$_directory));
  62      }
  63  
  64      /**
  65       * Assigns a translation handler object to $_handlers.
  66       *
  67       * Type hinting isn't used on purpose. You should extend a custom
  68       * translation handler passed here from the Horde_Translation interface,
  69       * but technically it's sufficient if you provide the API of that
  70       * interface.
  71       *
  72       * @param string $domain                      The translation domain.
  73       * @param Horde_Translation_Handler $handler  An object implementing the
  74       *                                            Horde_Translation_Handler
  75       *                                            interface.
  76       */
  77      public static function setHandler($domain, $handler)
  78      {
  79          static::$_handlers[$domain] = $handler;
  80      }
  81  
  82      /**
  83       * Returns the translation of a message.
  84       *
  85       * @var string $message  The string to translate.
  86       *
  87       * @return string  The string translation, or the original string if no
  88       *                 translation exists.
  89       */
  90      public static function t($message)
  91      {
  92          if (!isset(static::$_handlers[static::$_domain])) {
  93              static::loadHandler('Horde_Translation_Handler_Gettext');
  94          }
  95          return static::$_handlers[static::$_domain]->t($message);
  96      }
  97  
  98      /**
  99       * Returns the plural translation of a message.
 100       *
 101       * @param string $singular  The singular version to translate.
 102       * @param string $plural    The plural version to translate.
 103       * @param integer $number   The number that determines singular vs. plural.
 104       *
 105       * @return string  The string translation, or the original string if no
 106       *                 translation exists.
 107       */
 108      public static function ngettext($singular, $plural, $number)
 109      {
 110          if (!isset(static::$_handlers[static::$_domain])) {
 111              static::loadHandler('Horde_Translation_Handler_Gettext');
 112          }
 113          return static::$_handlers[static::$_domain]->ngettext($singular, $plural, $number);
 114      }
 115  
 116      /**
 117       * Allows a gettext string to be defined and recognized as a string by
 118       * the horde translation utilities, but no translation is actually
 119       * performed (raw gettext = r()).
 120       *
 121       * @since 2.1.0
 122       *
 123       * @param string $message  The raw string to mark for translation.
 124       *
 125       * @return string  The raw string.
 126       */
 127      public static function r($message)
 128      {
 129          return $message;
 130      }
 131  
 132  }