blob: 251cf6ef17cb0a8301f4d7ece2e2c92ecef547be [file] [log] [blame]
Andrey Andreevf9938a22012-01-07 22:10:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Language Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Language
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/language.html
36 */
Derek Jones692c5482010-03-02 13:39:48 -060037class CI_Lang {
Derek Allard2067d1a2008-11-13 22:59:24 +000038
David Behlercda768a2011-08-14 23:52:48 +020039 /**
40 * List of translations
41 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030042 * @var array
David Behlercda768a2011-08-14 23:52:48 +020043 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040044 public $language = array();
Andrey Andreevce747c82012-04-27 11:54:40 +030045
David Behlercda768a2011-08-14 23:52:48 +020046 /**
47 * List of loaded language files
48 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030049 * @var array
David Behlercda768a2011-08-14 23:52:48 +020050 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040051 public $is_loaded = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000052
Timothy Warrenad475052012-04-19 13:21:06 -040053 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030054 * Class constructor
Andrey Andreevce747c82012-04-27 11:54:40 +030055 *
56 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040057 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020058 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
Andrey Andreevf9938a22012-01-07 22:10:47 +020060 log_message('debug', 'Language Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000061 }
62
63 // --------------------------------------------------------------------
64
65 /**
66 * Load a language file
67 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030068 * @param mixed $langfile Language file name
69 * @param string $idiom Language name (english, etc.)
70 * @param bool $return Whether to return the loaded array of translations
71 * @param bool $add_suffix Whether to add suffix to $langfile
72 * @param string $alt_path Alternative path to look for the language file
73 *
74 * @return void|string[] Array containing translations, if $return is set to TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +000075 */
Andrey Andreevf49c4072012-05-24 14:57:33 +030076 public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
Greg Aker3a746652011-04-19 10:59:47 -050078 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -060079
Alex Bilbieed944a32012-06-02 11:07:47 +010080 if ($add_suffix === TRUE)
Derek Jones692c5482010-03-02 13:39:48 -060081 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020082 $langfile = str_replace('_lang', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -060083 }
84
Greg Aker3a746652011-04-19 10:59:47 -050085 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +000086
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030087 if (empty($idiom) OR ! ctype_alpha($idiom))
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020089 $config =& get_config();
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030090 $idiom = empty($config['language']) ? 'english' : $config['language'];
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020091 }
92
Alex Bilbieed944a32012-06-02 11:07:47 +010093 if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020094 {
95 return;
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
97
98 // Determine where the language file is and load it
Alex Bilbieed944a32012-06-02 11:07:47 +010099 if ($alt_path !== '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
Derek Jones692c5482010-03-02 13:39:48 -0600100 {
101 include($alt_path.'language/'.$idiom.'/'.$langfile);
102 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 else
104 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000105 $found = FALSE;
106
107 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000109 if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
110 {
111 include($package_path.'language/'.$idiom.'/'.$langfile);
112 $found = TRUE;
113 break;
114 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000116
117 if ($found !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Derek Allardea9e4e02009-08-17 17:40:48 +0000119 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121 }
122
Frank Michelcb272b62011-08-25 10:59:55 -0400123 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
125 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200126
Alex Bilbieed944a32012-06-02 11:07:47 +0100127 if ($return === TRUE)
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200128 {
129 return array();
130 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 return;
132 }
133
Alex Bilbieed944a32012-06-02 11:07:47 +0100134 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 return $lang;
137 }
138
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200139 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200140 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
142 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
143 return TRUE;
144 }
145
146 // --------------------------------------------------------------------
147
148 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300149 * Language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300151 * Fetches a single line of text from the language array
152 *
153 * @param string $line Language line key
154 * @return string Translation
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200156 public function line($line = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100158 $value = ($line === '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
Eric Barnese3c41cf2011-03-21 22:07:53 -0400159
160 // Because killer robots like unicorns!
patwork571023b2011-04-11 11:56:41 +0200161 if ($value === FALSE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400162 {
163 log_message('error', 'Could not find the language line "'.$line.'"');
164 }
165
patwork571023b2011-04-11 11:56:41 +0200166 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169}
Derek Allard2067d1a2008-11-13 22:59:24 +0000170
Derek Jones692c5482010-03-02 13:39:48 -0600171/* End of file Lang.php */
Andrey Andreev870ad192012-03-20 15:43:15 +0200172/* Location: ./system/core/Lang.php */