blob: 39fdd066864af2bfa0e8b8fad1039a9fd7e79d80 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/language.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
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 Andreev90726b82015-01-20 12:39:22 +020072 log_message('info', '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 }
Andrey Andreev32a077a2014-11-08 18:52:11 +020096
97 return;
Gabriel Potkány0e924ce2014-11-06 11:35:46 +010098 }
99
Greg Aker3a746652011-04-19 10:59:47 -0500100 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -0600101
Alex Bilbieed944a32012-06-02 11:07:47 +0100102 if ($add_suffix === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -0600103 {
Ivan Tcholakovb0ddf7f2014-05-31 17:01:34 +0300104 $langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -0600105 }
106
Greg Aker3a746652011-04-19 10:59:47 -0500107 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Andrey Andreev7a829722015-01-11 04:37:46 +0200109 if (empty($idiom) OR ! preg_match('/^[a-z_-]+$/i', $idiom))
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200111 $config =& get_config();
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300112 $idiom = empty($config['language']) ? 'english' : $config['language'];
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200113 }
114
Alex Bilbieed944a32012-06-02 11:07:47 +0100115 if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200116 {
117 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200120 // 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 Jones692c5482010-03-02 13:39:48 -0600123 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200124 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 Jones692c5482010-03-02 13:39:48 -0600136 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 else
138 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000139 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev719b60f2012-11-27 00:05:06 +0200141 $package_path .= 'language/'.$idiom.'/'.$langfile;
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200142 if ($basepath !== $package_path && file_exists($package_path))
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000143 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200144 include($package_path);
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000145 $found = TRUE;
146 break;
147 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200149 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000150
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200151 if ($found !== TRUE)
152 {
153 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
Frank Michelcb272b62011-08-25 10:59:55 -0400156 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200159
Alex Bilbieed944a32012-06-02 11:07:47 +0100160 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200161 {
162 return array();
163 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 return;
165 }
166
Alex Bilbieed944a32012-06-02 11:07:47 +0100167 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 return $lang;
170 }
171
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200172 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200173 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
Andrey Andreev90726b82015-01-20 12:39:22 +0200175 log_message('info', 'Language file loaded: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 return TRUE;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300182 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300184 * Fetches a single line of text from the language array
185 *
Andrey Andreevce0c9562012-11-22 17:26:29 +0200186 * @param string $line Language line key
187 * @param bool $log_errors Whether to log an error message if the line is not found
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300188 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 */
Andrey Andreevca8ddad2014-01-03 18:16:27 +0200190 public function line($line, $log_errors = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreevd104f8b2014-02-21 19:05:35 +0200192 $value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
Eric Barnese3c41cf2011-03-21 22:07:53 -0400193
194 // Because killer robots like unicorns!
Andrey Andreevce0c9562012-11-22 17:26:29 +0200195 if ($value === FALSE && $log_errors === TRUE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400196 {
197 log_message('error', 'Could not find the language line "'.$line.'"');
198 }
199
patwork571023b2011-04-11 11:56:41 +0200200 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203}