Subversion-Projekte lars-tiefland.ci

Revision

Revision 2242 | Revision 2257 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2254 lars 9
 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
68 lars 10
 *
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:
17
 *
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
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2254 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Language Class
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Libraries
45
 * @category	Language
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/libraries/language.html
48
 */
49
class CI_Lang {
50
 
51
	/**
52
	 * List of translations
53
	 *
54
	 * @var	array
55
	 */
56
	public $language =	array();
57
 
58
	/**
59
	 * List of loaded language files
60
	 *
61
	 * @var	array
62
	 */
63
	public $is_loaded =	array();
64
 
65
	/**
66
	 * Class constructor
67
	 *
68
	 * @return	void
69
	 */
70
	public function __construct()
71
	{
72
		log_message('info', 'Language Class Initialized');
73
	}
74
 
75
	// --------------------------------------------------------------------
76
 
77
	/**
78
	 * Load a language file
79
	 *
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
87
	 */
88
	public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
89
	{
90
		if (is_array($langfile))
91
		{
92
			foreach ($langfile as $value)
93
			{
94
				$this->load($value, $idiom, $return, $add_suffix, $alt_path);
95
			}
96
 
97
			return;
98
		}
99
 
100
		$langfile = str_replace('.php', '', $langfile);
101
 
102
		if ($add_suffix === TRUE)
103
		{
104
			$langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
105
		}
106
 
107
		$langfile .= '.php';
108
 
109
		if (empty($idiom) OR ! preg_match('/^[a-z_-]+$/i', $idiom))
110
		{
111
			$config =& get_config();
112
			$idiom = empty($config['language']) ? 'english' : $config['language'];
113
		}
114
 
115
		if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
116
		{
117
			return;
118
		}
119
 
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)
123
		{
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
			}
136
		}
137
		else
138
		{
139
			foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
140
			{
141
				$package_path .= 'language/'.$idiom.'/'.$langfile;
142
				if ($basepath !== $package_path && file_exists($package_path))
143
				{
144
					include($package_path);
145
					$found = TRUE;
146
					break;
147
				}
148
			}
149
		}
150
 
151
		if ($found !== TRUE)
152
		{
153
			show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
154
		}
155
 
156
		if ( ! isset($lang) OR ! is_array($lang))
157
		{
158
			log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
159
 
160
			if ($return === TRUE)
161
			{
162
				return array();
163
			}
164
			return;
165
		}
166
 
167
		if ($return === TRUE)
168
		{
169
			return $lang;
170
		}
171
 
172
		$this->is_loaded[$langfile] = $idiom;
173
		$this->language = array_merge($this->language, $lang);
174
 
175
		log_message('info', 'Language file loaded: language/'.$idiom.'/'.$langfile);
176
		return TRUE;
177
	}
178
 
179
	// --------------------------------------------------------------------
180
 
181
	/**
182
	 * Language line
183
	 *
184
	 * Fetches a single line of text from the language array
185
	 *
186
	 * @param	string	$line		Language line key
187
	 * @param	bool	$log_errors	Whether to log an error message if the line is not found
188
	 * @return	string	Translation
189
	 */
190
	public function line($line, $log_errors = TRUE)
191
	{
192
		$value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
193
 
194
		// Because killer robots like unicorns!
195
		if ($value === FALSE && $log_errors === TRUE)
196
		{
197
			log_message('error', 'Could not find the language line "'.$line.'"');
198
		}
199
 
200
		return $value;
201
	}
202
 
203
}