blob: 711ccab70020747a49594e4e1d134b9e01e655cb [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 *
42 * @var array
43 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020044 public $language = array();
David Behlercda768a2011-08-14 23:52:48 +020045 /**
46 * List of loaded language files
47 *
48 * @var array
49 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020050 public $is_loaded = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000051
Andrey Andreevf9938a22012-01-07 22:10:47 +020052 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Andrey Andreevf9938a22012-01-07 22:10:47 +020054 log_message('debug', 'Language Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000055 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Load a language file
61 *
Derek Allard2067d1a2008-11-13 22:59:24 +000062 * @param mixed the name of the language file to be loaded. Can be an array
63 * @param string the language (english, etc.)
David Behlercda768a2011-08-14 23:52:48 +020064 * @param bool return loaded array of translations
65 * @param bool add suffix to $langfile
66 * @param string alternative path to look for language file
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * @return mixed
68 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020069 public function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Greg Aker3a746652011-04-19 10:59:47 -050071 $langfile = str_replace('.php', '', $langfile);
Derek Jones692c5482010-03-02 13:39:48 -060072
73 if ($add_suffix == TRUE)
74 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020075 $langfile = str_replace('_lang', '', $langfile).'_lang';
Derek Jones692c5482010-03-02 13:39:48 -060076 }
77
Greg Aker3a746652011-04-19 10:59:47 -050078 $langfile .= '.php';
Derek Allard2067d1a2008-11-13 22:59:24 +000079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 if ($idiom == '')
81 {
Andrey Andreev8d5b24a2012-01-27 14:37:38 +020082 $config =& get_config();
83 $idiom = ( ! empty($config['language'])) ? $config['language'] : 'english';
84 }
85
86 if ($return == FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
87 {
88 return;
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90
91 // Determine where the language file is and load it
Derek Jones692c5482010-03-02 13:39:48 -060092 if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
93 {
94 include($alt_path.'language/'.$idiom.'/'.$langfile);
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096 else
97 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +000098 $found = FALSE;
99
100 foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000102 if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
103 {
104 include($package_path.'language/'.$idiom.'/'.$langfile);
105 $found = TRUE;
106 break;
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000109
110 if ($found !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Derek Allardea9e4e02009-08-17 17:40:48 +0000112 show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115
Derek Jones692c5482010-03-02 13:39:48 -0600116
Frank Michelcb272b62011-08-25 10:59:55 -0400117 if ( ! isset($lang) OR ! is_array($lang))
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200120
121 if ($return == TRUE)
122 {
123 return array();
124 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 return;
126 }
127
128 if ($return == TRUE)
129 {
130 return $lang;
131 }
132
Andrey Andreev8d5b24a2012-01-27 14:37:38 +0200133 $this->is_loaded[$langfile] = $idiom;
Andrey Andreev4b130612012-01-10 16:09:55 +0200134 $this->language = array_merge($this->language, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136 log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
137 return TRUE;
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Fetch a single line of text from the language array
144 *
Barry Mienydd671972010-10-04 16:33:58 +0200145 * @param string $line the language line
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @return string
147 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200148 public function line($line = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
patwork571023b2011-04-11 11:56:41 +0200150 $value = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
Eric Barnese3c41cf2011-03-21 22:07:53 -0400151
152 // Because killer robots like unicorns!
patwork571023b2011-04-11 11:56:41 +0200153 if ($value === FALSE)
Eric Barnese3c41cf2011-03-21 22:07:53 -0400154 {
155 log_message('error', 'Could not find the language line "'.$line.'"');
156 }
157
patwork571023b2011-04-11 11:56:41 +0200158 return $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161}
Derek Allard2067d1a2008-11-13 22:59:24 +0000162
Derek Jones692c5482010-03-02 13:39:48 -0600163/* End of file Lang.php */
patwork571023b2011-04-11 11:56:41 +0200164/* Location: ./system/core/Lang.php */