Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Installation time string manager.
  19   *
  20   * @package    core
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Fetches minimum strings for installation
  30   *
  31   * Minimalistic string fetching implementation
  32   * that is used in installer before we fetch the wanted
  33   * language pack from moodle.org lang download site.
  34   *
  35   * @package    core
  36   * @copyright  2010 Petr Skoda (http://skodak.org)
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class core_string_manager_install implements core_string_manager {
  40      /** @var string location of pre-install packs for all langs */
  41      protected $installroot;
  42  
  43      /**
  44       * Crate new instance of install string manager
  45       */
  46      public function __construct() {
  47          global $CFG;
  48          $this->installroot = "$CFG->dirroot/install/lang";
  49      }
  50  
  51      /**
  52       * Load all strings for one component
  53       * @param string $component The module the string is associated with
  54       * @param string $lang
  55       * @param bool $disablecache Do not use caches, force fetching the strings from sources
  56       * @param bool $disablelocal Do not use customized strings in xx_local language packs
  57       * @return array of all string for given component and lang
  58       */
  59      public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) {
  60          // Not needed in installer.
  61          return array();
  62      }
  63  
  64      /**
  65       * Does the string actually exist?
  66       *
  67       * get_string() is throwing debug warnings, sometimes we do not want them
  68       * or we want to display better explanation of the problem.
  69       *
  70       * Use with care!
  71       *
  72       * @param string $identifier The identifier of the string to search for
  73       * @param string $component The module the string is associated with
  74       * @return boot true if exists
  75       */
  76      public function string_exists($identifier, $component) {
  77          // Simple old style hack ;).
  78          $str = get_string($identifier, $component);
  79          return (strpos($str, '[[') === false);
  80      }
  81  
  82      /**
  83       * Has string been deprecated?
  84       *
  85       * No deprecated string in installation, unused strings are simply removed.
  86       *
  87       * @param string $identifier The identifier of the string to search for
  88       * @param string $component The module the string is associated with
  89       * @return bool true if deprecated
  90       */
  91      public function string_deprecated($identifier, $component) {
  92          return false;
  93      }
  94  
  95      /**
  96       * Get String returns a requested string
  97       *
  98       * @param string $identifier The identifier of the string to search for
  99       * @param string $component The module the string is associated with
 100       * @param string|object|array $a An object, string or number that can be used
 101       *      within translation strings
 102       * @param string $lang moodle translation language, null means use current
 103       * @return string The String !
 104       */
 105      public function get_string($identifier, $component = '', $a = null, $lang = null) {
 106          if (!$component) {
 107              $component = 'moodle';
 108          }
 109  
 110          if ($lang === null) {
 111              $lang = current_language();
 112          }
 113  
 114          // Get parent lang.
 115          $parent = '';
 116          if ($lang !== 'en' and $identifier !== 'parentlanguage' and $component !== 'langconfig') {
 117              if (file_exists("$this->installroot/$lang/langconfig.php")) {
 118                  $string = array();
 119                  include("$this->installroot/$lang/langconfig.php");
 120                  if (isset($string['parentlanguage'])) {
 121                      $parent = $string['parentlanguage'];
 122                  }
 123              }
 124          }
 125  
 126          // Include en string first.
 127          if (!file_exists("$this->installroot/en/$component.php")) {
 128              return "[[$identifier]]";
 129          }
 130          $string = array();
 131          include("$this->installroot/en/$component.php");
 132  
 133          // Now override en with parent if defined.
 134          if ($parent and $parent !== 'en' and file_exists("$this->installroot/$parent/$component.php")) {
 135              include("$this->installroot/$parent/$component.php");
 136          }
 137  
 138          // Finally override with requested language.
 139          if ($lang !== 'en' and file_exists("$this->installroot/$lang/$component.php")) {
 140              include("$this->installroot/$lang/$component.php");
 141          }
 142  
 143          if (!isset($string[$identifier])) {
 144              return "[[$identifier]]";
 145          }
 146  
 147          $string = $string[$identifier];
 148  
 149          if ($a !== null) {
 150              if (is_object($a) or is_array($a)) {
 151                  $a = (array)$a;
 152                  $search = array();
 153                  $replace = array();
 154                  foreach ($a as $key => $value) {
 155                      if (is_int($key)) {
 156                          // We do not support numeric keys - sorry!
 157                          continue;
 158                      }
 159                      $search[] = '{$a->' . $key . '}';
 160                      $replace[] = (string)$value;
 161                  }
 162                  if ($search) {
 163                      $string = str_replace($search, $replace, $string);
 164                  }
 165              } else {
 166                  $string = str_replace('{$a}', (string)$a, $string);
 167              }
 168          }
 169  
 170          return $string;
 171      }
 172  
 173      /**
 174       * Returns a localised list of all country names, sorted by country keys.
 175       *
 176       * @param bool $returnall return all or just enabled
 177       * @param string $lang moodle translation language, null means use current
 178       * @return array two-letter country code => translated name.
 179       */
 180      public function get_list_of_countries($returnall = false, $lang = null) {
 181          // Not used in installer.
 182          return array();
 183      }
 184  
 185      /**
 186       * Returns a localised list of languages, sorted by code keys.
 187       *
 188       * @param string $lang moodle translation language, null means use current
 189       * @param string $standard language list standard
 190       *                     iso6392: three-letter language code (ISO 639-2/T) => translated name.
 191       * @return array language code => translated name
 192       */
 193      public function get_list_of_languages($lang = null, $standard = 'iso6392') {
 194          // Not used in installer.
 195          return array();
 196      }
 197  
 198      /**
 199       * Checks if the translation exists for the language
 200       *
 201       * @param string $lang moodle translation language code
 202       * @param bool $includeall include also disabled translations
 203       * @return bool true if exists
 204       */
 205      public function translation_exists($lang, $includeall = true) {
 206          return file_exists($this->installroot . '/' . $lang . '/langconfig.php');
 207      }
 208  
 209      /**
 210       * Returns localised list of installed translations
 211       * @param bool $returnall return all or just enabled
 212       * @return array moodle translation code => localised translation name
 213       */
 214      public function get_list_of_translations($returnall = false) {
 215          // Return all is ignored here - we need to know all langs in installer.
 216          $languages = array();
 217          // Get raw list of lang directories.
 218          $langdirs = get_list_of_plugins('install/lang');
 219          asort($langdirs);
 220          // Get some info from each lang.
 221          foreach ($langdirs as $lang) {
 222              if (file_exists($this->installroot . '/' . $lang . '/langconfig.php')) {
 223                  $string = array();
 224                  include($this->installroot . '/' . $lang . '/langconfig.php');
 225                  if (!empty($string['thislanguage'])) {
 226                      $languages[$lang] = $string['thislanguage'] . ' (' . $lang . ')';
 227                  }
 228              }
 229          }
 230          // Return array.
 231          return $languages;
 232      }
 233  
 234      /**
 235       * Returns localised list of currencies.
 236       *
 237       * @param string $lang moodle translation language, null means use current
 238       * @return array currency code => localised currency name
 239       */
 240      public function get_list_of_currencies($lang = null) {
 241          // Not used in installer.
 242          return array();
 243      }
 244  
 245      /**
 246       * This implementation does not use any caches.
 247       *
 248       * @param bool $phpunitreset true means called from our PHPUnit integration test reset
 249       */
 250      public function reset_caches($phpunitreset = false) {
 251          // Nothing to do.
 252      }
 253  
 254      /**
 255       * Returns string revision counter, this is incremented after any string cache reset.
 256       * @return int lang string revision counter, -1 if unknown
 257       */
 258      public function get_revision() {
 259          return -1;
 260      }
 261  }