Merge branch 'MDL-42957-26' of https://github.com/jamiepratt/moodle into MOODLE_26_STABLE
[moodle.git] / lib / classes / string_manager_install.php
blob353e5072daecf6d0a46f818fba8c90bf9377cd61
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.
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/>.
17 /**
18 * Installation time string manager.
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
25 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Fetches minimum strings for installation
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.
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
39 class core_string_manager_install implements core_string_manager {
40 /** @var string location of pre-install packs for all langs */
41 protected $installroot;
43 /**
44 * Crate new instance of install string manager
46 public function __construct() {
47 global $CFG;
48 $this->installroot = "$CFG->dirroot/install/lang";
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
59 public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) {
60 // Not needed in installer.
61 return array();
64 /**
65 * Does the string actually exist?
67 * get_string() is throwing debug warnings, sometimes we do not want them
68 * or we want to display better explanation of the problem.
70 * Use with care!
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
76 public function string_exists($identifier, $component) {
77 // Simple old style hack ;).
78 $str = get_string($identifier, $component);
79 return (strpos($str, '[[') === false);
82 /**
83 * Get String returns a requested string
85 * @param string $identifier The identifier of the string to search for
86 * @param string $component The module the string is associated with
87 * @param string|object|array $a An object, string or number that can be used
88 * within translation strings
89 * @param string $lang moodle translation language, null means use current
90 * @return string The String !
92 public function get_string($identifier, $component = '', $a = null, $lang = null) {
93 if (!$component) {
94 $component = 'moodle';
97 if ($lang === null) {
98 $lang = current_language();
101 // Get parent lang.
102 $parent = '';
103 if ($lang !== 'en' and $identifier !== 'parentlanguage' and $component !== 'langconfig') {
104 if (file_exists("$this->installroot/$lang/langconfig.php")) {
105 $string = array();
106 include("$this->installroot/$lang/langconfig.php");
107 if (isset($string['parentlanguage'])) {
108 $parent = $string['parentlanguage'];
113 // Include en string first.
114 if (!file_exists("$this->installroot/en/$component.php")) {
115 return "[[$identifier]]";
117 $string = array();
118 include("$this->installroot/en/$component.php");
120 // Now override en with parent if defined.
121 if ($parent and $parent !== 'en' and file_exists("$this->installroot/$parent/$component.php")) {
122 include("$this->installroot/$parent/$component.php");
125 // Finally override with requested language.
126 if ($lang !== 'en' and file_exists("$this->installroot/$lang/$component.php")) {
127 include("$this->installroot/$lang/$component.php");
130 if (!isset($string[$identifier])) {
131 return "[[$identifier]]";
134 $string = $string[$identifier];
136 if ($a !== null) {
137 if (is_object($a) or is_array($a)) {
138 $a = (array)$a;
139 $search = array();
140 $replace = array();
141 foreach ($a as $key => $value) {
142 if (is_int($key)) {
143 // We do not support numeric keys - sorry!
144 continue;
146 $search[] = '{$a->' . $key . '}';
147 $replace[] = (string)$value;
149 if ($search) {
150 $string = str_replace($search, $replace, $string);
152 } else {
153 $string = str_replace('{$a}', (string)$a, $string);
157 return $string;
161 * Returns a localised list of all country names, sorted by country keys.
163 * @param bool $returnall return all or just enabled
164 * @param string $lang moodle translation language, null means use current
165 * @return array two-letter country code => translated name.
167 public function get_list_of_countries($returnall = false, $lang = null) {
168 // Not used in installer.
169 return array();
173 * Returns a localised list of languages, sorted by code keys.
175 * @param string $lang moodle translation language, null means use current
176 * @param string $standard language list standard
177 * iso6392: three-letter language code (ISO 639-2/T) => translated name.
178 * @return array language code => translated name
180 public function get_list_of_languages($lang = null, $standard = 'iso6392') {
181 // Not used in installer.
182 return array();
186 * Checks if the translation exists for the language
188 * @param string $lang moodle translation language code
189 * @param bool $includeall include also disabled translations
190 * @return bool true if exists
192 public function translation_exists($lang, $includeall = true) {
193 return file_exists($this->installroot . '/' . $lang . '/langconfig.php');
197 * Returns localised list of installed translations
198 * @param bool $returnall return all or just enabled
199 * @return array moodle translation code => localised translation name
201 public function get_list_of_translations($returnall = false) {
202 // Return all is ignored here - we need to know all langs in installer.
203 $languages = array();
204 // Get raw list of lang directories.
205 $langdirs = get_list_of_plugins('install/lang');
206 asort($langdirs);
207 // Get some info from each lang.
208 foreach ($langdirs as $lang) {
209 if (file_exists($this->installroot . '/' . $lang . '/langconfig.php')) {
210 $string = array();
211 include($this->installroot . '/' . $lang . '/langconfig.php');
212 if (!empty($string['thislanguage'])) {
213 $languages[$lang] = $string['thislanguage'] . ' (' . $lang . ')';
217 // Return array.
218 return $languages;
222 * Returns localised list of currencies.
224 * @param string $lang moodle translation language, null means use current
225 * @return array currency code => localised currency name
227 public function get_list_of_currencies($lang = null) {
228 // Not used in installer.
229 return array();
233 * This implementation does not use any caches.
235 * @param bool $phpunitreset true means called from our PHPUnit integration test reset
237 public function reset_caches($phpunitreset = false) {
238 // Nothing to do.
242 * Returns string revision counter, this is incremented after any string cache reset.
243 * @return int lang string revision counter, -1 if unknown
245 public function get_revision() {
246 return -1;