Merge branch 'MDL-72490-m400' of https://github.com/sammarshallou/moodle into MOODLE_...
[moodle.git] / lib / classes / string_manager_install.php
blobd970c73d59bf7077beda610777072ddedf796294
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 * Has string been deprecated?
85 * No deprecated string in installation, unused strings are simply removed.
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
91 public function string_deprecated($identifier, $component) {
92 return false;
95 /**
96 * Get String returns a requested string
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 !
105 public function get_string($identifier, $component = '', $a = null, $lang = null) {
106 if (!$component) {
107 $component = 'moodle';
110 if ($lang === null) {
111 $lang = current_language();
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'];
126 // Include en string first.
127 if (!file_exists("$this->installroot/en/$component.php")) {
128 return "[[$identifier]]";
130 $string = array();
131 include("$this->installroot/en/$component.php");
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");
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");
143 if (!isset($string[$identifier])) {
144 return "[[$identifier]]";
147 $string = $string[$identifier];
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;
159 $search[] = '{$a->' . $key . '}';
160 $replace[] = (string)$value;
162 if ($search) {
163 $string = str_replace($search, $replace, $string);
165 } else {
166 $string = str_replace('{$a}', (string)$a, $string);
170 return $string;
174 * Returns a localised list of all country names, sorted by country keys.
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.
180 public function get_list_of_countries($returnall = false, $lang = null) {
181 // Not used in installer.
182 return array();
186 * Returns a localised list of languages, sorted by code keys.
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
193 public function get_list_of_languages($lang = null, $standard = 'iso6392') {
194 // Not used in installer.
195 return array();
199 * Checks if the translation exists for the language
201 * @param string $lang moodle translation language code
202 * @param bool $includeall include also disabled translations
203 * @return bool true if exists
205 public function translation_exists($lang, $includeall = true) {
206 return file_exists($this->installroot . '/' . $lang . '/langconfig.php');
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
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 . ')';
230 // Return array.
231 return $languages;
235 * Returns localised list of currencies.
237 * @param string $lang moodle translation language, null means use current
238 * @return array currency code => localised currency name
240 public function get_list_of_currencies($lang = null) {
241 // Not used in installer.
242 return array();
246 * This implementation does not use any caches.
248 * @param bool $phpunitreset true means called from our PHPUnit integration test reset
250 public function reset_caches($phpunitreset = false) {
251 // Nothing to do.
255 * Returns string revision counter, this is incremented after any string cache reset.
256 * @return int lang string revision counter, -1 if unknown
258 public function get_revision() {
259 return -1;