Lang.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Language Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Language
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/language.html
  24. */
  25. class CI_Lang {
  26. /**
  27. * List of translations
  28. *
  29. * @var array
  30. */
  31. var $language = array();
  32. /**
  33. * List of loaded language files
  34. *
  35. * @var array
  36. */
  37. var $is_loaded = array();
  38. /**
  39. * Constructor
  40. *
  41. * @access public
  42. */
  43. function __construct()
  44. {
  45. log_message('debug', "Language Class Initialized");
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Load a language file
  50. *
  51. * @access public
  52. * @param mixed the name of the language file to be loaded. Can be an array
  53. * @param string the language (english, etc.)
  54. * @param bool return loaded array of translations
  55. * @param bool add suffix to $langfile
  56. * @param string alternative path to look for language file
  57. * @return mixed
  58. */
  59. function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
  60. {
  61. $langfile = str_replace('.php', '', $langfile);
  62. if ($add_suffix == TRUE)
  63. {
  64. $langfile = str_replace('_lang.', '', $langfile).'_lang';
  65. }
  66. $langfile .= '.php';
  67. if (in_array($langfile, $this->is_loaded, TRUE))
  68. {
  69. return;
  70. }
  71. $config =& get_config();
  72. if ($idiom == '')
  73. {
  74. $deft_lang = ( ! isset($config['language'])) ? 'english' : $config['language'];
  75. $idiom = ($deft_lang == '') ? 'english' : $deft_lang;
  76. }
  77. // Determine where the language file is and load it
  78. if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
  79. {
  80. include($alt_path.'language/'.$idiom.'/'.$langfile);
  81. }
  82. else
  83. {
  84. $found = FALSE;
  85. foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
  86. {
  87. if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
  88. {
  89. include($package_path.'language/'.$idiom.'/'.$langfile);
  90. $found = TRUE;
  91. break;
  92. }
  93. }
  94. if ($found !== TRUE)
  95. {
  96. show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
  97. }
  98. }
  99. if ( ! isset($lang))
  100. {
  101. log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
  102. return;
  103. }
  104. if ($return == TRUE)
  105. {
  106. return $lang;
  107. }
  108. $this->is_loaded[] = $langfile;
  109. $this->language = array_merge($this->language, $lang);
  110. unset($lang);
  111. log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
  112. return TRUE;
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Fetch a single line of text from the language array
  117. *
  118. * @access public
  119. * @param string $line the language line
  120. * @return string
  121. */
  122. function line($line = '')
  123. {
  124. $value = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
  125. // Because killer robots like unicorns!
  126. if ($value === FALSE)
  127. {
  128. log_message('error', 'Could not find the language line "'.$line.'"');
  129. }
  130. return $value;
  131. }
  132. }
  133. // END Language Class
  134. /* End of file Lang.php */
  135. /* Location: ./system/core/Lang.php */