Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Language Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Language |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/language.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 49 | class CI_Lang { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 51 | /** |
| 52 | * List of translations |
| 53 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 54 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 55 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 56 | public $language = array(); |
Andrey Andreev | ce747c8 | 2012-04-27 11:54:40 +0300 | [diff] [blame] | 57 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 58 | /** |
| 59 | * List of loaded language files |
| 60 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 61 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 62 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 63 | public $is_loaded = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 65 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 66 | * Class constructor |
Andrey Andreev | ce747c8 | 2012-04-27 11:54:40 +0300 | [diff] [blame] | 67 | * |
| 68 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 69 | */ |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 70 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | { |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 72 | log_message('info', 'Language Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // -------------------------------------------------------------------- |
| 76 | |
| 77 | /** |
| 78 | * Load a language file |
| 79 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 80 | * @param mixed $langfile Language file name |
| 81 | * @param string $idiom Language name (english, etc.) |
| 82 | * @param bool $return Whether to return the loaded array of translations |
| 83 | * @param bool $add_suffix Whether to add suffix to $langfile |
| 84 | * @param string $alt_path Alternative path to look for the language file |
| 85 | * |
| 86 | * @return void|string[] Array containing translations, if $return is set to TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | */ |
Andrey Andreev | f49c407 | 2012-05-24 14:57:33 +0300 | [diff] [blame] | 88 | public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
Gabriel Potkány | 0e924ce | 2014-11-06 11:35:46 +0100 | [diff] [blame] | 90 | if (is_array($langfile)) |
| 91 | { |
| 92 | foreach ($langfile as $value) |
| 93 | { |
Andrey Andreev | 5a7f77d | 2014-11-08 15:15:44 +0200 | [diff] [blame] | 94 | $this->load($value, $idiom, $return, $add_suffix, $alt_path); |
Gabriel Potkány | 0e924ce | 2014-11-06 11:35:46 +0100 | [diff] [blame] | 95 | } |
Andrey Andreev | 32a077a | 2014-11-08 18:52:11 +0200 | [diff] [blame] | 96 | |
| 97 | return; |
Gabriel Potkány | 0e924ce | 2014-11-06 11:35:46 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 100 | $langfile = str_replace('.php', '', $langfile); |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 101 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 102 | if ($add_suffix === TRUE) |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 103 | { |
Ivan Tcholakov | b0ddf7f | 2014-05-31 17:01:34 +0300 | [diff] [blame] | 104 | $langfile = preg_replace('/_lang$/', '', $langfile).'_lang'; |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 105 | } |
| 106 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 107 | $langfile .= '.php'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
Andrey Andreev | 7a82972 | 2015-01-11 04:37:46 +0200 | [diff] [blame] | 109 | if (empty($idiom) OR ! preg_match('/^[a-z_-]+$/i', $idiom)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 111 | $config =& get_config(); |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 112 | $idiom = empty($config['language']) ? 'english' : $config['language']; |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 115 | if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom) |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 116 | { |
| 117 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 120 | // Load the base file, so any others found can override it |
| 121 | $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile; |
| 122 | if (($found = file_exists($basepath)) === TRUE) |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 123 | { |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 124 | include($basepath); |
| 125 | } |
| 126 | |
| 127 | // Do we have an alternative path to look in? |
| 128 | if ($alt_path !== '') |
| 129 | { |
| 130 | $alt_path .= 'language/'.$idiom.'/'.$langfile; |
| 131 | if (file_exists($alt_path)) |
| 132 | { |
| 133 | include($alt_path); |
| 134 | $found = TRUE; |
| 135 | } |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 136 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | else |
| 138 | { |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 139 | foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
Andrey Andreev | 719b60f | 2012-11-27 00:05:06 +0200 | [diff] [blame] | 141 | $package_path .= 'language/'.$idiom.'/'.$langfile; |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 142 | if ($basepath !== $package_path && file_exists($package_path)) |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 143 | { |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 144 | include($package_path); |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 145 | $found = TRUE; |
| 146 | break; |
| 147 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | } |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 149 | } |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 150 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 151 | if ($found !== TRUE) |
| 152 | { |
| 153 | show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Frank Michel | cb272b6 | 2011-08-25 10:59:55 -0400 | [diff] [blame] | 156 | if ( ! isset($lang) OR ! is_array($lang)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
| 158 | log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile); |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 159 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 160 | if ($return === TRUE) |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 161 | { |
| 162 | return array(); |
| 163 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 167 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
| 169 | return $lang; |
| 170 | } |
| 171 | |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 172 | $this->is_loaded[$langfile] = $idiom; |
Andrey Andreev | 4b13061 | 2012-01-10 16:09:55 +0200 | [diff] [blame] | 173 | $this->language = array_merge($this->language, $lang); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 175 | log_message('info', 'Language file loaded: language/'.$idiom.'/'.$langfile); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | return TRUE; |
| 177 | } |
| 178 | |
| 179 | // -------------------------------------------------------------------- |
| 180 | |
| 181 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 182 | * Language line |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 184 | * Fetches a single line of text from the language array |
| 185 | * |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 186 | * @param string $line Language line key |
| 187 | * @param bool $log_errors Whether to log an error message if the line is not found |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 188 | * @return string Translation |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | */ |
Andrey Andreev | ca8ddad | 2014-01-03 18:16:27 +0200 | [diff] [blame] | 190 | public function line($line, $log_errors = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
Andrey Andreev | d104f8b | 2014-02-21 19:05:35 +0200 | [diff] [blame] | 192 | $value = isset($this->language[$line]) ? $this->language[$line] : FALSE; |
Eric Barnes | e3c41cf | 2011-03-21 22:07:53 -0400 | [diff] [blame] | 193 | |
| 194 | // Because killer robots like unicorns! |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 195 | if ($value === FALSE && $log_errors === TRUE) |
Eric Barnes | e3c41cf | 2011-03-21 22:07:53 -0400 | [diff] [blame] | 196 | { |
| 197 | log_message('error', 'Could not find the language line "'.$line.'"'); |
| 198 | } |
| 199 | |
patwork | 571023b | 2011-04-11 11:56:41 +0200 | [diff] [blame] | 200 | return $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | } |