You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.3 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * 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:
  17. *
  18. * 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
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Language Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Language
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/language.html
  47. */
  48. class CI_Lang {
  49. /**
  50. * List of translations
  51. *
  52. * @var array
  53. */
  54. public $language = array();
  55. /**
  56. * List of loaded language files
  57. *
  58. * @var array
  59. */
  60. public $is_loaded = array();
  61. /**
  62. * Class constructor
  63. *
  64. * @return void
  65. */
  66. public function __construct()
  67. {
  68. log_message('info', 'Language Class Initialized');
  69. }
  70. // --------------------------------------------------------------------
  71. /**
  72. * Load a language file
  73. *
  74. * @param mixed $langfile Language file name
  75. * @param string $idiom Language name (english, etc.)
  76. * @param bool $return Whether to return the loaded array of translations
  77. * @param bool $add_suffix Whether to add suffix to $langfile
  78. * @param string $alt_path Alternative path to look for the language file
  79. *
  80. * @return void|string[] Array containing translations, if $return is set to TRUE
  81. */
  82. public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
  83. {
  84. if (is_array($langfile))
  85. {
  86. foreach ($langfile as $value)
  87. {
  88. $this->load($value, $idiom, $return, $add_suffix, $alt_path);
  89. }
  90. return;
  91. }
  92. $langfile = str_replace('.php', '', $langfile);
  93. if ($add_suffix === TRUE)
  94. {
  95. $langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
  96. }
  97. $langfile .= '.php';
  98. if (empty($idiom) OR ! preg_match('/^[a-z_-]+$/i', $idiom))
  99. {
  100. $config =& get_config();
  101. $idiom = empty($config['language']) ? 'english' : $config['language'];
  102. }
  103. if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom)
  104. {
  105. return;
  106. }
  107. // Load the base file, so any others found can override it
  108. $basepath = BASEPATH.'language/'.$idiom.'/'.$langfile;
  109. if (($found = file_exists($basepath)) === TRUE)
  110. {
  111. include($basepath);
  112. }
  113. // Do we have an alternative path to look in?
  114. if ($alt_path !== '')
  115. {
  116. $alt_path .= 'language/'.$idiom.'/'.$langfile;
  117. if (file_exists($alt_path))
  118. {
  119. include($alt_path);
  120. $found = TRUE;
  121. }
  122. }
  123. else
  124. {
  125. foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
  126. {
  127. $package_path .= 'language/'.$idiom.'/'.$langfile;
  128. if ($basepath !== $package_path && file_exists($package_path))
  129. {
  130. include($package_path);
  131. $found = TRUE;
  132. break;
  133. }
  134. }
  135. }
  136. if ($found !== TRUE)
  137. {
  138. show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
  139. }
  140. if ( ! isset($lang) OR ! is_array($lang))
  141. {
  142. log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
  143. if ($return === TRUE)
  144. {
  145. return array();
  146. }
  147. return;
  148. }
  149. if ($return === TRUE)
  150. {
  151. return $lang;
  152. }
  153. $this->is_loaded[$langfile] = $idiom;
  154. $this->language = array_merge($this->language, $lang);
  155. log_message('info', 'Language file loaded: language/'.$idiom.'/'.$langfile);
  156. return TRUE;
  157. }
  158. // --------------------------------------------------------------------
  159. /**
  160. * Language line
  161. *
  162. * Fetches a single line of text from the language array
  163. *
  164. * @param string $line Language line key
  165. * @param bool $log_errors Whether to log an error message if the line is not found
  166. * @return string Translation
  167. */
  168. public function line($line, $log_errors = TRUE)
  169. {
  170. $value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
  171. // Because killer robots like unicorns!
  172. if ($value === FALSE && $log_errors === TRUE)
  173. {
  174. log_message('error', 'Could not find the language line "'.$line.'"');
  175. }
  176. return $value;
  177. }
  178. }