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.

Differences Between: [Versions 310 and 401]

   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   * Helper class for the language import tool.
  19   *
  20   * @package    tool_langimport
  21   * @copyright  2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_langimport;
  26  
  27  use coding_exception;
  28  
  29  defined('MOODLE_INTERNAL') || die;
  30  
  31  /**
  32   * Helper class for the language import tool.
  33   *
  34   * @copyright  2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class locale {
  38      /**
  39       * Checks availability of locale on current operating system.
  40       *
  41       * @param string $langpackcode E.g.: en, es, fr, de.
  42       * @return bool TRUE if the locale is available on OS.
  43       * @throws coding_exception when $langpackcode parameter is a non-empty string.
  44       */
  45      public function check_locale_availability(string $langpackcode) : bool {
  46          global $CFG;
  47  
  48          if (empty($langpackcode)) {
  49              throw new coding_exception('Invalid language pack code in \\'.__METHOD__.'() call, only non-empty string is allowed');
  50          }
  51  
  52          // Fetch the correct locale based on ostype.
  53          if ($CFG->ostype === 'WINDOWS') {
  54              $stringtofetch = 'localewin';
  55          } else {
  56              $stringtofetch = 'locale';
  57          }
  58  
  59          // Store current locale.
  60          $currentlocale = $this->set_locale(LC_ALL, 0);
  61  
  62          $locale = get_string_manager()->get_string($stringtofetch, 'langconfig', $a = null, $langpackcode);
  63  
  64          // Try to set new locale.
  65          $return = $this->set_locale(LC_ALL, $locale);
  66  
  67          // Restore current locale.
  68          $this->set_locale(LC_ALL, $currentlocale);
  69  
  70          // If $return is not equal to false, it means that setlocale() succeed to change locale.
  71          return $return !== false;
  72      }
  73  
  74      /**
  75       * Wrap for the native PHP function setlocale().
  76       *
  77       * @param int $category Specifying the category of the functions affected by the locale setting.
  78       * @param string $locale E.g.: en_AU.utf8, en_GB.utf8, es_ES.utf8, fr_FR.utf8, de_DE.utf8.
  79       * @return string|false Returns the new current locale, or FALSE on error.
  80       */
  81      protected function set_locale(int $category = LC_ALL, string $locale = '0') {
  82          if (strlen($locale) <= 255 || PHP_OS_FAMILY === 'BSD' || PHP_OS_FAMILY === 'Darwin') {
  83              // We can set the whole locale all together.
  84              return setlocale($category, $locale);
  85          }
  86  
  87          // Too long locale with linux or windows, let's split it into known and supported categories.
  88          $split = explode(';', $locale);
  89          foreach ($split as $element) {
  90              [$category, $value] = explode('=', $element);
  91              if (defined($category)) { // Only if the category exists, there are OS differences.
  92                  setlocale(constant($category), $value);
  93              }
  94          }
  95          return setlocale(LC_ALL, 0); // Finally, return the complete configured locale.
  96      }
  97  }