blob: 5d824cee6e5db1ea5a3f85fa0091c519ccf173c0 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevf9938a22012-01-07 22:10:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevf9938a22012-01-07 22:10:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Language Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Language
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/language.html
37 */
Derek Jones692c5482010-03-02 13:39:48 -060038class CI_Lang {
Derek Allard2067d1a2008-11-13 22:59:24 +000039
David Behlercda768a2011-08-14 23:52:48 +020040 /**
41 * List of translations
42 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030043 * @var array
David Behlercda768a2011-08-14 23:52:48 +020044 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040045 public $language = array();
Andrey Andreevce747c82012-04-27 11:54:40 +030046
David Behlercda768a2011-08-14 23:52:48 +020047 /**
48 * List of loaded language files
49 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030050 * @var array
David Behlercda768a2011-08-14 23:52:48 +020051 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040052 public $is_loaded = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Timothy Warrenad475052012-04-19 13:21:06 -040054 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030055 * Class constructor
Andrey Andreevce747c82012-04-27 11:54:40 +030056 *
57 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040058 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020059 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
Andrey Andreevf9938a22012-01-07 22:10:47 +020061 log_message('debug', 'Language Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Load a language file
68 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030069 * @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 Allard2067d1a2008-11-13 22:59:24 +000076 */
Andrey Andreevf49c4072012-05-24 14:57:33 +030077 public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
Greg Aker3a746652011-04-19 10:59:47 -050079 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -060080
Alex Bilbieed944a32012-06-02 11:07:47 +010081 if ($add_suffix === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -060082 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020083 $langfile = str_replace('_lang', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -060084 }
85
Greg Aker3a746652011-04-19 10:59:47 -050086 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030088 if (empty($idiom) OR ! ctype_alpha($idiom))
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020090 $config =& get_config();
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030091 $idiom = empty($config['language']) ? 'english' : $config['language'];
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020092 }
93
Alex Bilbieed944a32012-06-02 11:07:47 +010094 if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020095 {
96 return;
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
98
99 // Determine where the language file is and load it
Alex Bilbieed944a32012-06-02 11:07:47 +0100100 if ($alt_path !== '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
Derek Jones692c5482010-03-02 13:39:48 -0600101 {
102 include($alt_path.'language/'.$idiom.'/'.$langfile);
103 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 else
105 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000106 $found = FALSE;
107
108 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000110 if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
111 {
112 include($package_path.'language/'.$idiom.'/'.$langfile);
113 $found = TRUE;
114 break;
115 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000117
118 if ($found !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Derek Allardea9e4e02009-08-17 17:40:48 +0000120 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
122 }
123
Frank Michelcb272b62011-08-25 10:59:55 -0400124 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200127
Alex Bilbieed944a32012-06-02 11:07:47 +0100128 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200129 {
130 return array();
131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return;
133 }
134
Alex Bilbieed944a32012-06-02 11:07:47 +0100135 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 return $lang;
138 }
139
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200140 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200141 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
144 return TRUE;
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300150 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300152 * Fetches a single line of text from the language array
153 *
Andrey Andreevce0c9562012-11-22 17:26:29 +0200154 * @param string $line Language line key
155 * @param bool $log_errors Whether to log an error message if the line is not found
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300156 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Andrey Andreevce0c9562012-11-22 17:26:29 +0200158 public function line($line = '', $log_errors = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100160 $value = ($line === '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
Eric Barnese3c41cf2011-03-21 22:07:53 -0400161
162 // Because killer robots like unicorns!
Andrey Andreevce0c9562012-11-22 17:26:29 +0200163 if ($value === FALSE && $log_errors === TRUE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400164 {
165 log_message('error', 'Could not find the language line "'.$line.'"');
166 }
167
patwork571023b2011-04-11 11:56:41 +0200168 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171}
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
Derek Jones692c5482010-03-02 13:39:48 -0600173/* End of file Lang.php */
Andrey Andreev870ad192012-03-20 15:43:15 +0200174/* Location: ./system/core/Lang.php */