PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/lang.php

https://bitbucket.org/codeyash/bootstrap
PHP | 256 lines | 157 code | 31 blank | 68 comment | 18 complexity | 5b074bc666c02f39ab8b544f76624736 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class LangException extends \FuelException { }
  14. class Lang
  15. {
  16. /**
  17. * @var array $loaded_files array of loaded files
  18. */
  19. public static $loaded_files = array();
  20. /**
  21. * @var array language lines
  22. */
  23. public static $lines = array();
  24. /**
  25. * @var array language(s) to fall back on when loading a file from the current lang fails
  26. */
  27. public static $fallback;
  28. public static function _init()
  29. {
  30. static::$fallback = (array) \Config::get('language_fallback', 'en');
  31. }
  32. /**
  33. * Loads a language file.
  34. *
  35. * @param mixed $file string file | language array | Lang_Interface instance
  36. * @param mixed $group null for no group, true for group is filename, false for not storing in the master lang
  37. * @param string|null $language name of the language to load, null for the configurated language
  38. * @param bool $overwrite true for array_merge, false for \Arr::merge
  39. * @param bool $reload true to force a reload even if the file is already loaded
  40. * @return array the (loaded) language array
  41. */
  42. public static function load($file, $group = null, $language = null, $overwrite = false, $reload = false)
  43. {
  44. // get the active language and all fallback languages
  45. $language or $language = \Config::get('language');
  46. $languages = static::$fallback;
  47. // make sure we don't have the active language in the fallback array
  48. if (in_array($language, $languages))
  49. {
  50. unset($languages[array_search($language, $languages)]);
  51. }
  52. // stick the active language to the front of the list
  53. array_unshift($languages, $language);
  54. if ( ! $reload and
  55. ! is_array($file) and
  56. ! is_object($file) and
  57. array_key_exists($file, static::$loaded_files))
  58. {
  59. $group === true and $group = $file;
  60. if ($group === null or $group === false or ! isset(static::$lines[$language][$group]))
  61. {
  62. return false;
  63. }
  64. return static::$lines[$language][$group];
  65. }
  66. $lang = array();
  67. if (is_array($file))
  68. {
  69. $lang = $file;
  70. }
  71. elseif (is_string($file))
  72. {
  73. $info = pathinfo($file);
  74. $type = 'php';
  75. if (isset($info['extension']))
  76. {
  77. $type = $info['extension'];
  78. // Keep extension when it's an absolute path, because the finder won't add it
  79. if ($file[0] !== '/' and $file[1] !== ':')
  80. {
  81. $file = substr($file, 0, -(strlen($type) + 1));
  82. }
  83. }
  84. $class = '\\Lang_'.ucfirst($type);
  85. if (class_exists($class))
  86. {
  87. static::$loaded_files[$file] = true;
  88. $file = new $class($file, $languages);
  89. }
  90. else
  91. {
  92. throw new \FuelException(sprintf('Invalid lang type "%s".', $type));
  93. }
  94. }
  95. if ($file instanceof Lang_Interface)
  96. {
  97. try
  98. {
  99. $lang = $file->load($overwrite);
  100. }
  101. catch (\LangException $e)
  102. {
  103. $lang = array();
  104. }
  105. $group = $group === true ? $file->group() : $group;
  106. }
  107. if ($group === null)
  108. {
  109. isset(static::$lines[$language]) or static::$lines[$language] = array();
  110. static::$lines[$language] = $overwrite ? array_merge(static::$lines[$language], $lang) : \Arr::merge(static::$lines[$language], $lang);
  111. }
  112. else
  113. {
  114. $group = ($group === true) ? $file : $group;
  115. isset(static::$lines[$language][$group]) or static::$lines[$language][$group] = array();
  116. static::$lines[$language][$group] = $overwrite ? array_merge(static::$lines[$language][$group], $lang) : \Arr::merge(static::$lines[$language][$group], $lang);
  117. }
  118. return $lang;
  119. }
  120. /**
  121. * Save a language array to disk.
  122. *
  123. * @param string $file desired file name
  124. * @param string|array $lang master language array key or language array
  125. * @param string|null $language name of the language to load, null for the configurated language
  126. * @return bool false when language is empty or invalid else \File::update result
  127. */
  128. public static function save($file, $lang, $language = null)
  129. {
  130. if ($language === null)
  131. {
  132. $languages = static::$fallback;
  133. array_unshift($languages, $language ?: \Config::get('language'));
  134. $language = reset($languages);
  135. }
  136. // prefix the file with the language
  137. if ( ! is_null($language))
  138. {
  139. $file = explode('::', $file);
  140. end($file);
  141. $file[key($file)] = $language.DS.end($file);
  142. $file = implode('::', $file);
  143. }
  144. if ( ! is_array($lang))
  145. {
  146. if ( ! isset(static::$lines[$language][$lang]))
  147. {
  148. return false;
  149. }
  150. $lang = static::$lines[$language][$lang];
  151. }
  152. $type = pathinfo($file, PATHINFO_EXTENSION);
  153. if( ! $type)
  154. {
  155. $type = 'php';
  156. $file .= '.'.$type;
  157. }
  158. $class = '\\Lang_'.ucfirst($type);
  159. if( ! class_exists($class, true))
  160. {
  161. throw new \LangException('Cannot save a language file of type: '.$type);
  162. }
  163. $driver = new $class;
  164. return $driver->save($file, $lang);
  165. }
  166. /**
  167. * Returns a (dot notated) language string
  168. *
  169. * @param string $line key for the line
  170. * @param array $params array of params to str_replace
  171. * @param mixed $default default value to return
  172. * @param string|null $language name of the language to get, null for the configurated language
  173. * @return mixed either the line or default when not found
  174. */
  175. public static function get($line, array $params = array(), $default = null, $language = null)
  176. {
  177. if ($language === null)
  178. {
  179. $languages = static::$fallback;
  180. array_unshift($languages, $language ?: \Config::get('language'));
  181. $language = reset($languages);
  182. }
  183. return isset(static::$lines[$language]) ? \Str::tr(\Fuel::value(\Arr::get(static::$lines[$language], $line, $default)), $params) : $default;
  184. }
  185. /**
  186. * Sets a (dot notated) language string
  187. *
  188. * @param string $line a (dot notated) language key
  189. * @param mixed $value the language string
  190. * @param string $group group
  191. * @param string|null $language name of the language to set, null for the configurated language
  192. * @return void the \Arr::set result
  193. */
  194. public static function set($line, $value, $group = null, $language = null)
  195. {
  196. $group === null or $line = $group.'.'.$line;
  197. if ($language === null)
  198. {
  199. $languages = static::$fallback;
  200. array_unshift($languages, $language ?: \Config::get('language'));
  201. $language = reset($languages);
  202. }
  203. isset(static::$lines[$language]) or static::$lines[$language] = array();
  204. return \Arr::set(static::$lines[$language], $line, \Fuel::value($value));
  205. }
  206. /**
  207. * Deletes a (dot notated) language string
  208. *
  209. * @param string $item a (dot notated) language key
  210. * @param string $group group
  211. * @param string|null $language name of the language to set, null for the configurated language
  212. * @return array|bool the \Arr::delete result, success boolean or array of success booleans
  213. */
  214. public static function delete($item, $group = null, $language = null)
  215. {
  216. $group === null or $line = $group.'.'.$line;
  217. if ($language === null)
  218. {
  219. $languages = static::$fallback;
  220. array_unshift($languages, $language ?: \Config::get('language'));
  221. $language = reset($languages);
  222. }
  223. return isset(static::$lines[$language]) ? \Arr::delete(static::$lines[$language], $item) : false;
  224. }
  225. }