blob: ae0a4d6422ed85e3e4f30fb5e8a09ffacffb9a05 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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
Andrey Andreevb11b9f32012-11-26 23:01:24 +020099 // Load the base file, so any others found can override it
100 $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile;
101 if (($found = file_exists($basepath)) === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -0600102 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200103 include($basepath);
104 }
105
106 // Do we have an alternative path to look in?
107 if ($alt_path !== '')
108 {
109 $alt_path .= 'language/'.$idiom.'/'.$langfile;
110 if (file_exists($alt_path))
111 {
112 include($alt_path);
113 $found = TRUE;
114 }
Derek Jones692c5482010-03-02 13:39:48 -0600115 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 else
117 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000118 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Andrey Andreev719b60f2012-11-27 00:05:06 +0200120 $package_path .= 'language/'.$idiom.'/'.$langfile;
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200121 if ($basepath !== $package_path && file_exists($package_path))
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000122 {
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200123 include($package_path);
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000124 $found = TRUE;
125 break;
126 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200128 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000129
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200130 if ($found !== TRUE)
131 {
132 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134
Frank Michelcb272b62011-08-25 10:59:55 -0400135 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200138
Alex Bilbieed944a32012-06-02 11:07:47 +0100139 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200140 {
141 return array();
142 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 return;
144 }
145
Alex Bilbieed944a32012-06-02 11:07:47 +0100146 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 return $lang;
149 }
150
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200151 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200152 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153
154 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
155 return TRUE;
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300161 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300163 * Fetches a single line of text from the language array
164 *
Andrey Andreevce0c9562012-11-22 17:26:29 +0200165 * @param string $line Language line key
166 * @param bool $log_errors Whether to log an error message if the line is not found
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300167 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
Andrey Andreevca8ddad2014-01-03 18:16:27 +0200169 public function line($line, $log_errors = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreevd104f8b2014-02-21 19:05:35 +0200171 $value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
Eric Barnese3c41cf2011-03-21 22:07:53 -0400172
173 // Because killer robots like unicorns!
Andrey Andreevce0c9562012-11-22 17:26:29 +0200174 if ($value === FALSE && $log_errors === TRUE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400175 {
176 log_message('error', 'Could not find the language line "'.$line.'"');
177 }
178
patwork571023b2011-04-11 11:56:41 +0200179 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181
182}
Derek Allard2067d1a2008-11-13 22:59:24 +0000183
Derek Jones692c5482010-03-02 13:39:48 -0600184/* End of file Lang.php */
Andrey Andreev870ad192012-03-20 15:43:15 +0200185/* Location: ./system/core/Lang.php */