Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * @since     2.2.0
  13   */
  14  
  15  /**
  16   * The Horde_Translation_Autodetect auto detects the locale directory location
  17   * for the class implementing it.
  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   * @since     2.2.0
  25   */
  26  abstract class Horde_Translation_Autodetect extends Horde_Translation
  27  {
  28      /**
  29       * The absolute PEAR path to the translations for the default gettext handler.
  30       *
  31       * This value is automatically set by PEAR Replace Tasks.
  32       *
  33       * @var string
  34       */
  35      protected static $_pearDirectory;
  36  
  37      /**
  38       * Auto detects the locale directory location.
  39       *
  40       * @param string $handlerClass  The name of a class implementing the
  41       *                              Horde_Translation_Handler interface.
  42       */
  43      public static function loadHandler($handlerClass)
  44      {
  45          if (!static::$_domain) {
  46              throw new Horde_Translation_Exception('The domain property must be set by the class that extends Horde_Translation_Autodetect.');
  47          }
  48  
  49          $directory = static::_searchLocaleDirectory();
  50          if (!$directory) {
  51              throw new Horde_Translation_Exception(sprintf('Could not found find any locale directory for %s domain.', static::$_domain));
  52          }
  53  
  54          static::$_directory = $directory;
  55          parent::loadHandler($handlerClass);
  56      }
  57  
  58      /**
  59       * Search for the locale directory for different installations methods (eg: PEAR, Composer).
  60       *
  61       * @var boolean|string The directory if found, or false when no valid directory is found
  62       */
  63      protected static function _searchLocaleDirectory()
  64      {
  65          if (static::$_pearDirectory !== '@data_dir@') {
  66              $directory = static::$_pearDirectory . '/' . static::$_domain . '/locale';
  67              if (is_dir($directory)) {
  68                  return $directory;
  69              }
  70          }
  71  
  72          $directories = static::_getSearchDirectories();
  73          foreach ($directories as $directory) {
  74              if (is_dir($directory)) {
  75                  return $directory;
  76              }
  77          }
  78  
  79          return false;
  80      }
  81  
  82      /**
  83       * Get potential locations for the locale directory.
  84       *
  85       * @var array List of directories
  86       */
  87      protected static function _getSearchDirectories()
  88      {
  89          $className = get_called_class();
  90          $class = new ReflectionClass($className);
  91          $basedir = dirname($class->getFilename());
  92          $depth = substr_count($className, '\\')
  93              ?: substr_count($className, '_');
  94  
  95          return array(
  96              /* Composer */
  97              $basedir . str_repeat('/..', $depth) . '/data/locale',
  98              /* Source */
  99              $basedir . str_repeat('/..', $depth + 1) . '/locale'
 100          );
 101      }
 102  
 103  }