Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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          return setlocale($category, $locale);
  83      }
  84  }