blob: bdd6da8e9b9c00024cf6c29dda419b755f9d36c7 [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 {
Gabriel Potkány0e924ce2014-11-06 11:35:46 +010090 if (is_array($langfile))
91 {
92 foreach ($langfile as $value)
93 {
Andrey Andreev5a7f77d2014-11-08 15:15:44 +020094 $this->load($value, $idiom, $return, $add_suffix, $alt_path);
Gabriel Potkány0e924ce2014-11-06 11:35:46 +010095 }
96 }
97
Greg Aker3a746652011-04-19 10:59:47 -050098 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -060099
Alex Bilbieed944a32012-06-02 11:07:47 +0100100 if ($add_suffix === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -0600101 {
Ivan Tcholakovb0ddf7f2014-05-31 17:01:34 +0300102 $langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -0600103 }
104
Greg Aker3a746652011-04-19 10:59:47 -0500105 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300107 if (empty($idiom) OR ! ctype_alpha($idiom))
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200109 $config =& get_config();
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300110 $idiom = empty($config['language']) ? 'english' : $config['language'];
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200111 }
112
Alex Bilbieed944a32012-06-02 11:07:47 +0100113 if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200114 {
115 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200118 // Load the base file, so any others found can override it
119 $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile;
120 if (($found = file_exists($basepath)) === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -0600121 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200122 include($basepath);
123 }
124
125 // Do we have an alternative path to look in?
126 if ($alt_path !== '')
127 {
128 $alt_path .= 'language/'.$idiom.'/'.$langfile;
129 if (file_exists($alt_path))
130 {
131 include($alt_path);
132 $found = TRUE;
133 }
Derek Jones692c5482010-03-02 13:39:48 -0600134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 else
136 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000137 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreev719b60f2012-11-27 00:05:06 +0200139 $package_path .= 'language/'.$idiom.'/'.$langfile;
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200140 if ($basepath !== $package_path && file_exists($package_path))
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000141 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200142 include($package_path);
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000143 $found = TRUE;
144 break;
145 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200147 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000148
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200149 if ($found !== TRUE)
150 {
151 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
Frank Michelcb272b62011-08-25 10:59:55 -0400154 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200157
Alex Bilbieed944a32012-06-02 11:07:47 +0100158 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200159 {
160 return array();
161 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 return;
163 }
164
Alex Bilbieed944a32012-06-02 11:07:47 +0100165 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 return $lang;
168 }
169
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200170 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200171 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
173 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
174 return TRUE;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300180 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300182 * Fetches a single line of text from the language array
183 *
Andrey Andreevce0c9562012-11-22 17:26:29 +0200184 * @param string $line Language line key
185 * @param bool $log_errors Whether to log an error message if the line is not found
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300186 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 */
Andrey Andreevca8ddad2014-01-03 18:16:27 +0200188 public function line($line, $log_errors = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreevd104f8b2014-02-21 19:05:35 +0200190 $value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
Eric Barnese3c41cf2011-03-21 22:07:53 -0400191
192 // Because killer robots like unicorns!
Andrey Andreevce0c9562012-11-22 17:26:29 +0200193 if ($value === FALSE && $log_errors === TRUE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400194 {
195 log_message('error', 'Could not find the language line "'.$line.'"');
196 }
197
patwork571023b2011-04-11 11:56:41 +0200198 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201}
Derek Allard2067d1a2008-11-13 22:59:24 +0000202
Derek Jones692c5482010-03-02 13:39:48 -0600203/* End of file Lang.php */
Andrey Andreev870ad192012-03-20 15:43:15 +0200204/* Location: ./system/core/Lang.php */