blob: 25d0af1ff022a53f1d6b6f31f4075804b2bfee93 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevf9938a22012-01-07 22:10:47 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevf9938a22012-01-07 22:10:47 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Language Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Language
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/language.html
48 */
Derek Jones692c5482010-03-02 13:39:48 -060049class CI_Lang {
Derek Allard2067d1a2008-11-13 22:59:24 +000050
David Behlercda768a2011-08-14 23:52:48 +020051 /**
52 * List of translations
53 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030054 * @var array
David Behlercda768a2011-08-14 23:52:48 +020055 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040056 public $language = array();
Andrey Andreevce747c82012-04-27 11:54:40 +030057
David Behlercda768a2011-08-14 23:52:48 +020058 /**
59 * List of loaded language files
60 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030061 * @var array
David Behlercda768a2011-08-14 23:52:48 +020062 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040063 public $is_loaded = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000064
Timothy Warrenad475052012-04-19 13:21:06 -040065 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030066 * Class constructor
Andrey Andreevce747c82012-04-27 11:54:40 +030067 *
68 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040069 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020070 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreevf9938a22012-01-07 22:10:47 +020072 log_message('debug', 'Language Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000073 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Load a language file
79 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030080 * @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 Allard2067d1a2008-11-13 22:59:24 +000087 */
Andrey Andreevf49c4072012-05-24 14:57:33 +030088 public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Greg Aker3a746652011-04-19 10:59:47 -050090 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -060091
Alex Bilbieed944a32012-06-02 11:07:47 +010092 if ($add_suffix === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -060093 {
Ivan Tcholakovb0ddf7f2014-05-31 17:01:34 +030094 $langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -060095 }
96
Greg Aker3a746652011-04-19 10:59:47 -050097 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +000098
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030099 if (empty($idiom) OR ! ctype_alpha($idiom))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200101 $config =& get_config();
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300102 $idiom = empty($config['language']) ? 'english' : $config['language'];
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200103 }
104
Alex Bilbieed944a32012-06-02 11:07:47 +0100105 if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200106 {
107 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200110 // Load the base file, so any others found can override it
111 $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile;
112 if (($found = file_exists($basepath)) === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -0600113 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200114 include($basepath);
115 }
116
117 // Do we have an alternative path to look in?
118 if ($alt_path !== '')
119 {
120 $alt_path .= 'language/'.$idiom.'/'.$langfile;
121 if (file_exists($alt_path))
122 {
123 include($alt_path);
124 $found = TRUE;
125 }
Derek Jones692c5482010-03-02 13:39:48 -0600126 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 else
128 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000129 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev719b60f2012-11-27 00:05:06 +0200131 $package_path .= 'language/'.$idiom.'/'.$langfile;
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200132 if ($basepath !== $package_path && file_exists($package_path))
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000133 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200134 include($package_path);
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000135 $found = TRUE;
136 break;
137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200139 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000140
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200141 if ($found !== TRUE)
142 {
143 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
Frank Michelcb272b62011-08-25 10:59:55 -0400146 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200149
Alex Bilbieed944a32012-06-02 11:07:47 +0100150 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200151 {
152 return array();
153 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 return;
155 }
156
Alex Bilbieed944a32012-06-02 11:07:47 +0100157 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
159 return $lang;
160 }
161
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200162 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200163 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000164
165 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
166 return TRUE;
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300172 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300174 * Fetches a single line of text from the language array
175 *
Andrey Andreevce0c9562012-11-22 17:26:29 +0200176 * @param string $line Language line key
177 * @param bool $log_errors Whether to log an error message if the line is not found
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300178 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 */
Andrey Andreevca8ddad2014-01-03 18:16:27 +0200180 public function line($line, $log_errors = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevd104f8b2014-02-21 19:05:35 +0200182 $value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
Eric Barnese3c41cf2011-03-21 22:07:53 -0400183
184 // Because killer robots like unicorns!
Andrey Andreevce0c9562012-11-22 17:26:29 +0200185 if ($value === FALSE && $log_errors === TRUE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400186 {
187 log_message('error', 'Could not find the language line "'.$line.'"');
188 }
189
patwork571023b2011-04-11 11:56:41 +0200190 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193}
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
Derek Jones692c5482010-03-02 13:39:48 -0600195/* End of file Lang.php */
Andrey Andreev870ad192012-03-20 15:43:15 +0200196/* Location: ./system/core/Lang.php */