/components/com_jcomments/libraries/joomlatune/language.php

https://github.com/Shigaru/shigaru · PHP · 219 lines · 99 code · 20 blank · 100 comment · 13 complexity · 6824b8cd40b08ddf85aa82d1a77adb9e MD5 · raw file

  1. <?php
  2. /**
  3. * Languages/translation handler class
  4. *
  5. * @version 1.0
  6. * @package JoomlaTune.Framework
  7. * @subpackage Language
  8. * @author Dmitry M. Litvinov
  9. * @copyright 2008
  10. * @access public
  11. */
  12. // Check for double include
  13. if (!defined('JOOMLATUNE_LANGUAGE')) {
  14. define('JOOMLATUNE_LANGUAGE', 1);
  15. /**
  16. * Languages/translation handler class
  17. *
  18. * @access public
  19. * @package JoomlaTune.Framework
  20. * @subpackage Language
  21. */
  22. class JoomlaTuneLanguage
  23. {
  24. var $currentLanguage = null;
  25. var $root = null;
  26. var $languages = null;
  27. /**
  28. * A hack to support __construct() on PHP 4
  29. *
  30. * @access public
  31. * @return JoomlaTuneLanguage
  32. */
  33. function JoomlaTuneLanguage()
  34. {
  35. $args = func_get_args();
  36. call_user_func_array(array(&$this, '__construct'), $args);
  37. }
  38. /**
  39. * Class constructor, overridden in descendant classes.
  40. *
  41. * @access protected
  42. */
  43. function __construct()
  44. {
  45. $this->root = dirname(__FILE__);
  46. $this->languages = array(array());
  47. }
  48. /**
  49. * Returns a reference to the global JoomlaTuneLanguage object, only creating it
  50. * if it doesn't already exist.
  51. *
  52. * This method must be invoked as:
  53. * <pre> $lang =& JoomlaTuneLanguage::getInstance();</pre>
  54. *
  55. * @static
  56. * @param string $language
  57. * @return JoomlaTuneLanguage $instance
  58. */
  59. function &getInstance( $language = null )
  60. {
  61. /**
  62. * $instance JoomlaTuneLanguage
  63. */
  64. static $instance = null;
  65. if (!is_object($instance)) {
  66. $instance = new JoomlaTuneLanguage();
  67. }
  68. if (isset($language)) {
  69. $instance->load($language, '');
  70. $instance->setLanguage($language);
  71. }
  72. return $instance;
  73. }
  74. /**
  75. * Return current language
  76. *
  77. * @access public
  78. * @return string
  79. */
  80. function getLanguage()
  81. {
  82. return $this->currentLanguage;
  83. }
  84. /**
  85. * Set current language
  86. *
  87. * @param string $language
  88. * @access public
  89. * @return void
  90. */
  91. function setLanguage( $language )
  92. {
  93. $this->currentLanguage = trim($language);
  94. }
  95. /**
  96. * Return root path to language files
  97. *
  98. * @access public
  99. * @return string
  100. */
  101. function getRoot()
  102. {
  103. return $this->root;
  104. }
  105. /**
  106. * Sets root path to language files
  107. *
  108. * @access public
  109. * @param string $directory
  110. * @return void
  111. */
  112. function setRoot( $directory )
  113. {
  114. $this->root = trim($directory);
  115. }
  116. /**
  117. * Unload language variables for given language
  118. *
  119. * @access public
  120. * @param string $language
  121. * @return void
  122. */
  123. function unload( $language )
  124. {
  125. $this->languages[$language] = array();
  126. }
  127. /**
  128. * Load all languages files from languages' root directory
  129. *
  130. * @access public
  131. * @param string $language
  132. * @return void
  133. */
  134. function loadAll( $language )
  135. {
  136. $fileNames = glob($this->getRoot . "*.ini");
  137. foreach ($fileNames as $fileName) {
  138. if (preg_match('/' . $language . '/', $fileName)) {
  139. $this->load($fileName);
  140. }
  141. }
  142. }
  143. /**
  144. * Translates a string into the current language
  145. *
  146. * @static
  147. * @access public
  148. * @param string $string The string to translate
  149. * @param boolean $jsSafe Make the result javascript safe
  150. * @return string
  151. */
  152. function _( $string, $jsSafe = false )
  153. {
  154. if (isset($this->languages[$this->currentLanguage])) {
  155. $key = trim($string);
  156. $key = strtoupper($key);
  157. if (isset($this->languages[$this->currentLanguage][$key])) {
  158. $string = $this->languages[$this->currentLanguage][$key];
  159. }
  160. }
  161. if ($jsSafe) {
  162. $string = addslashes($string);
  163. }
  164. return $string;
  165. }
  166. /**
  167. * Loads a single language file and appends the results to the existing strings
  168. *
  169. * @access public
  170. * @param string $language The language to load
  171. * @param string $section The language file name suffix
  172. * @return void
  173. */
  174. function load( $language, $section = '' )
  175. {
  176. $fileName = $language . ($section != '' ? '.' . $section : '') . '.ini';
  177. $pathFile = $this->root . DS . $fileName;
  178. if (is_file($pathFile)) {
  179. if (!isset($this->languages[$language])) {
  180. $this->languages[$language] = array();
  181. $this->setLanguage($language);
  182. }
  183. $lines = file($pathFile);
  184. foreach ($lines as $line) {
  185. if (!preg_match('/^(#|;)/', $line)) {
  186. $s = split('=', $line, 2);
  187. if (count($s) >= 2) {
  188. $this->languages[$language][strtoupper(trim($s[0]))] = trim($s[1]);
  189. }
  190. }
  191. }
  192. unset($lines);
  193. } else {
  194. echo 'File "' . $fileName . '" not found in "' . $this->root . '"';
  195. }
  196. }
  197. }
  198. }
  199. ?>