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   * @package Translation
   4   *
   5   * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file LICENSE for license information (LGPL). If you
   8   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   9   */
  10  
  11  /**
  12   * The Horde_Translation_Handler_Gettext provides translations through the
  13   * gettext extension, but fails gracefully if gettext is not installed.
  14   *
  15   * @author  Jan Schneider <jan@horde.org>
  16   * @package Translation
  17   */
  18  class Horde_Translation_Handler_Gettext implements Horde_Translation_Handler
  19  {
  20      /**
  21       * The translation domain, e.g. package name.
  22       *
  23       * @var string
  24       */
  25      protected $_domain;
  26  
  27      /**
  28       * Whether the gettext extension is installed.
  29       *
  30       * @var boolean
  31       */
  32      protected $_gettext;
  33  
  34      /**
  35       * Constructor.
  36       *
  37       * @param string $domain  The translation domain, e.g. package name.
  38       * @param string $path    The path to the gettext catalog.
  39       */
  40      public function __construct($domain, $path)
  41      {
  42          if (!is_dir($path)) {
  43              throw new InvalidArgumentException("$path is not a directory");
  44          }
  45          $this->_gettext = function_exists('_');
  46          if (!$this->_gettext) {
  47              return;
  48          }
  49          $this->_domain = $domain;
  50          bindtextdomain($this->_domain, $path);
  51      }
  52  
  53      /**
  54       * Returns the translation of a message.
  55       *
  56       * @param string $message  The string to translate.
  57       *
  58       * @return string  The string translation, or the original string if no
  59       *                 translation exists.
  60       */
  61      public function t($message)
  62      {
  63          return $this->_gettext ? dgettext($this->_domain, $message) : $message;
  64      }
  65  
  66      /**
  67       * Returns the plural translation of a message.
  68       *
  69       * @param string $singular  The singular version to translate.
  70       * @param string $plural    The plural version to translate.
  71       * @param integer $number   The number that determines singular vs. plural.
  72       *
  73       * @return string  The string translation, or the original string if no
  74       *                 translation exists.
  75       */
  76      public function ngettext($singular, $plural, $number)
  77      {
  78          return $this->_gettext
  79            ? dngettext($this->_domain, $singular, $plural, $number)
  80            : ($number > 1 ? $plural : $singular);
  81      }
  82  }