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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Language Class |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Language |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/libraries/language.html |
| 37 | */ |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 38 | class CI_Lang { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 40 | /** |
| 41 | * List of translations |
| 42 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 43 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 44 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 45 | public $language = array(); |
Andrey Andreev | ce747c8 | 2012-04-27 11:54:40 +0300 | [diff] [blame] | 46 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 47 | /** |
| 48 | * List of loaded language files |
| 49 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 50 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 51 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 52 | public $is_loaded = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 54 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 55 | * Class constructor |
Andrey Andreev | ce747c8 | 2012-04-27 11:54:40 +0300 | [diff] [blame] | 56 | * |
| 57 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 58 | */ |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 59 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | { |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 61 | log_message('debug', 'Language Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // -------------------------------------------------------------------- |
| 65 | |
| 66 | /** |
| 67 | * Load a language file |
| 68 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 69 | * @param mixed $langfile Language file name |
| 70 | * @param string $idiom Language name (english, etc.) |
| 71 | * @param bool $return Whether to return the loaded array of translations |
| 72 | * @param bool $add_suffix Whether to add suffix to $langfile |
| 73 | * @param string $alt_path Alternative path to look for the language file |
| 74 | * |
| 75 | * @return void|string[] Array containing translations, if $return is set to TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | */ |
Andrey Andreev | f49c407 | 2012-05-24 14:57:33 +0300 | [diff] [blame] | 77 | public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 79 | $langfile = str_replace('.php', '', $langfile); |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 80 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 81 | if ($add_suffix === TRUE) |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 82 | { |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 83 | $langfile = str_replace('_lang', '', $langfile).'_lang'; |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 84 | } |
| 85 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 86 | $langfile .= '.php'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 88 | if (empty($idiom) OR ! ctype_alpha($idiom)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 90 | $config =& get_config(); |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 91 | $idiom = empty($config['language']) ? 'english' : $config['language']; |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 94 | 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] | 95 | { |
| 96 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 99 | // Load the base file, so any others found can override it |
| 100 | $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile; |
| 101 | if (($found = file_exists($basepath)) === TRUE) |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 102 | { |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 103 | include($basepath); |
| 104 | } |
| 105 | |
| 106 | // Do we have an alternative path to look in? |
| 107 | if ($alt_path !== '') |
| 108 | { |
| 109 | $alt_path .= 'language/'.$idiom.'/'.$langfile; |
| 110 | if (file_exists($alt_path)) |
| 111 | { |
| 112 | include($alt_path); |
| 113 | $found = TRUE; |
| 114 | } |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 115 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | else |
| 117 | { |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 118 | foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
Andrey Andreev | 719b60f | 2012-11-27 00:05:06 +0200 | [diff] [blame] | 120 | $package_path .= 'language/'.$idiom.'/'.$langfile; |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 121 | if ($basepath !== $package_path && file_exists($package_path)) |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 122 | { |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 123 | include($package_path); |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 124 | $found = TRUE; |
| 125 | break; |
| 126 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | } |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 128 | } |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 129 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 130 | if ($found !== TRUE) |
| 131 | { |
| 132 | show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Frank Michel | cb272b6 | 2011-08-25 10:59:55 -0400 | [diff] [blame] | 135 | if ( ! isset($lang) OR ! is_array($lang)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | { |
| 137 | log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile); |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 138 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 139 | if ($return === TRUE) |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 140 | { |
| 141 | return array(); |
| 142 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 146 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
| 148 | return $lang; |
| 149 | } |
| 150 | |
Andrey Andreev | 8d5b24a | 2012-01-27 14:37:38 +0200 | [diff] [blame] | 151 | $this->is_loaded[$langfile] = $idiom; |
Andrey Andreev | 4b13061 | 2012-01-10 16:09:55 +0200 | [diff] [blame] | 152 | $this->language = array_merge($this->language, $lang); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | |
| 154 | log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile); |
| 155 | return TRUE; |
| 156 | } |
| 157 | |
| 158 | // -------------------------------------------------------------------- |
| 159 | |
| 160 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 161 | * Language line |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 163 | * Fetches a single line of text from the language array |
| 164 | * |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 165 | * @param string $line Language line key |
| 166 | * @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] | 167 | * @return string Translation |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | */ |
Andrey Andreev | ca8ddad | 2014-01-03 18:16:27 +0200 | [diff] [blame] | 169 | public function line($line, $log_errors = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | { |
Andrey Andreev | d104f8b | 2014-02-21 19:05:35 +0200 | [diff] [blame^] | 171 | $value = isset($this->language[$line]) ? $this->language[$line] : FALSE; |
Eric Barnes | e3c41cf | 2011-03-21 22:07:53 -0400 | [diff] [blame] | 172 | |
| 173 | // Because killer robots like unicorns! |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 174 | if ($value === FALSE && $log_errors === TRUE) |
Eric Barnes | e3c41cf | 2011-03-21 22:07:53 -0400 | [diff] [blame] | 175 | { |
| 176 | log_message('error', 'Could not find the language line "'.$line.'"'); |
| 177 | } |
| 178 | |
patwork | 571023b | 2011-04-11 11:56:41 +0200 | [diff] [blame] | 179 | return $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | |
Derek Jones | 692c548 | 2010-03-02 13:39:48 -0600 | [diff] [blame] | 184 | /* End of file Lang.php */ |
Andrey Andreev | 870ad19 | 2012-03-20 15:43:15 +0200 | [diff] [blame] | 185 | /* Location: ./system/core/Lang.php */ |