/default_www/frontend/core/engine/language.php

https://github.com/zakgrant/forkcms · PHP · 408 lines · 164 code · 69 blank · 175 comment · 19 complexity · c00f8cf0b5aa6db3741a797b53743a7e MD5 · raw file

  1. <?php
  2. /**
  3. * This class will store the language-dependant content for the frontend.
  4. *
  5. * @package frontend
  6. * @subpackage core
  7. *
  8. * @author Tijs Verkoyen <tijs@netlash.com>
  9. * @since 2.0
  10. */
  11. class FrontendLanguage
  12. {
  13. /**
  14. * Locale arrays
  15. *
  16. * @var array
  17. */
  18. private static $act = array(),
  19. $err = array(),
  20. $lbl = array(),
  21. $msg = array();
  22. /**
  23. * The possible languages
  24. *
  25. * @var array
  26. */
  27. private static $languages = array('active' => array(), 'possible_redirect' => array());
  28. /**
  29. * Build the language files
  30. *
  31. * @return void
  32. * @param string $language The language to build the locale-file for.
  33. * @param string $application The application to build the locale-file for.
  34. */
  35. public static function buildCache($language, $application)
  36. {
  37. // get db
  38. $db = FrontendModel::getDB();
  39. // get types
  40. $types = $db->getEnumValues('locale', 'type');
  41. // get locale for backend
  42. $locale = (array) $db->getRecords('SELECT type, module, name, value
  43. FROM locale
  44. WHERE language = ? AND application = ?
  45. ORDER BY type ASC, name ASC, module ASC',
  46. array((string) $language, (string) $application));
  47. // start generating PHP
  48. $value = '<?php' . "\n";
  49. $value .= '/**' . "\n";
  50. $value .= ' *' . "\n";
  51. $value .= ' * This file is generated by Fork CMS, it contains' . "\n";
  52. $value .= ' * more information about the locale. Do NOT edit.' . "\n";
  53. $value .= ' * ' . "\n";
  54. $value .= ' * @author Fork CMS' . "\n";
  55. $value .= ' * @generated ' . date('Y-m-d H:i:s') . "\n";
  56. $value .= ' */' . "\n";
  57. $value .= "\n";
  58. // loop types
  59. foreach($types as $type)
  60. {
  61. // default module
  62. $modules = array('core');
  63. // continue output
  64. $value .= "\n";
  65. $value .= '// init var' . "\n";
  66. $value .= '$' . $type . ' = array();' . "\n";
  67. $value .= '$' . $type . '[\'core\'] = array();' . "\n";
  68. // loop locale
  69. foreach($locale as $i => $item)
  70. {
  71. // types match
  72. if($item['type'] == $type)
  73. {
  74. // new module
  75. if(!in_array($item['module'], $modules))
  76. {
  77. $value .= '$' . $type . '[\'' . $item['module'] . '\'] = array();' . "\n";
  78. $modules[] = $item['module'];
  79. }
  80. // parse
  81. if($application == 'backend') $value .= '$' . $type . '[\'' . $item['module'] . '\'][\'' . $item['name'] . '\'] = \'' . str_replace('\"', '"', addslashes($item['value'])) . '\';' . "\n";
  82. else $value .= '$' . $type . '[\'' . $item['name'] . '\'] = \'' . str_replace('\"', '"', addslashes($item['value'])) . '\';' . "\n";
  83. // unset
  84. unset($locale[$i]);
  85. }
  86. }
  87. }
  88. // close php
  89. $value .= "\n";
  90. $value .= '?>';
  91. // store
  92. SpoonFile::setContent(constant(mb_strtoupper($application) . '_CACHE_PATH') . '/locale/' . $language . '.php', $value);
  93. }
  94. /**
  95. * Get an action from the language-file
  96. *
  97. * @return string
  98. * @param string $key The key to get.
  99. */
  100. public static function getAction($key)
  101. {
  102. // redefine
  103. $key = (string) $key;
  104. // if the action exists return it,
  105. if(isset(self::$act[$key])) return self::$act[$key];
  106. // otherwise return the key in label-format
  107. return '{$act' . $key . '}';
  108. }
  109. /**
  110. * Get all the actions
  111. *
  112. * @return array
  113. */
  114. public static function getActions()
  115. {
  116. return self::$act;
  117. }
  118. /**
  119. * Get the active languages
  120. *
  121. * @return array
  122. */
  123. public static function getActiveLanguages()
  124. {
  125. // validate the cache
  126. if(empty(self::$languages['active']))
  127. {
  128. // grab from settings
  129. $activeLanguages = (array) FrontendModel::getModuleSetting('core', 'active_languages');
  130. // store in cache
  131. self::$languages['active'] = $activeLanguages;
  132. }
  133. // return from cache
  134. return self::$languages['active'];
  135. }
  136. /**
  137. * Get the prefered language by using the browser-language
  138. *
  139. * @return string
  140. * @param bool[optional] $forRedirect Only look in the languages to redirect?
  141. */
  142. public static function getBrowserLanguage($forRedirect = true)
  143. {
  144. // browser language set
  145. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && strlen($_SERVER['HTTP_ACCEPT_LANGUAGE']) >= 2)
  146. {
  147. // get languages
  148. $redirectLanguages = self::getRedirectLanguages();
  149. // prefered languages
  150. $browserLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  151. // loop until result
  152. foreach($browserLanguages as $language)
  153. {
  154. // redefine language
  155. $language = substr($language, 0, 2); // first two characters
  156. // find possible language
  157. if($forRedirect)
  158. {
  159. // check in the redirect-languages
  160. if(in_array($language, $redirectLanguages)) return $language;
  161. }
  162. }
  163. }
  164. // fallback
  165. return SITE_DEFAULT_LANGUAGE;
  166. }
  167. /**
  168. * Get an error from the language-file
  169. *
  170. * @return string
  171. * @param string $key The key to get.
  172. */
  173. public static function getError($key)
  174. {
  175. // redefine
  176. $key = (string) $key;
  177. // if the error exists return it,
  178. if(isset(self::$err[$key])) return self::$err[$key];
  179. // otherwise return the key in label-format
  180. return '{$err' . $key . '}';
  181. }
  182. /**
  183. * Get all the errors
  184. *
  185. * @return array
  186. */
  187. public static function getErrors()
  188. {
  189. return self::$err;
  190. }
  191. /**
  192. * Get a label from the language-file
  193. *
  194. * @return string
  195. * @param string $key The key to get.
  196. */
  197. public static function getLabel($key)
  198. {
  199. // redefine
  200. $key = (string) $key;
  201. // if the error exists return it,
  202. if(isset(self::$lbl[$key])) return self::$lbl[$key];
  203. // otherwise return the key in label-format
  204. return '{$lbl' . $key . '}';
  205. }
  206. /**
  207. * Get all the labels
  208. *
  209. * @return array
  210. */
  211. public static function getLabels()
  212. {
  213. return self::$lbl;
  214. }
  215. /**
  216. * Get a message from the language-file
  217. *
  218. * @return string
  219. * @param string $key The key to get.
  220. */
  221. public static function getMessage($key)
  222. {
  223. // redefine
  224. $key = (string) $key;
  225. // if the error exists return it,
  226. if(isset(self::$msg[$key])) return self::$msg[$key];
  227. // otherwise return the key in label-format
  228. return '{$msg' . $key . '}';
  229. }
  230. /**
  231. * Get all the messages
  232. *
  233. * @return array
  234. */
  235. public static function getMessages()
  236. {
  237. return self::$msg;
  238. }
  239. /**
  240. * Get the redirect languages
  241. *
  242. * @return array
  243. */
  244. public static function getRedirectLanguages()
  245. {
  246. // validate the cache
  247. if(empty(self::$languages['possible_redirect']))
  248. {
  249. // grab from settings
  250. $redirectLanguages = (array) FrontendModel::getModuleSetting('core', 'redirect_languages');
  251. // store in cache
  252. self::$languages['possible_redirect'] = $redirectLanguages;
  253. }
  254. // return
  255. return self::$languages['possible_redirect'];
  256. }
  257. /**
  258. * Set locale
  259. *
  260. * @return void
  261. * @param string[optional] $language The language to load, if not provided we will load the language based on the URL.
  262. * @param bool[optional] $force Force the language, so don't check if the language is active.
  263. */
  264. public static function setLocale($language = null, $force = false)
  265. {
  266. // redefine
  267. $language = ($language !== null) ? (string) $language : FRONTEND_LANGUAGE;
  268. // validate language
  269. if(!$force && !in_array($language, self::getActiveLanguages())) throw new FrontendException('Invalid language (' . $language . ').');
  270. // validate file, generate it if needed
  271. if(!SpoonFile::exists(FRONTEND_CACHE_PATH . '/locale/' . $language . '.php')) self::buildCache($language, APPLICATION);
  272. // init vars
  273. $act = array();
  274. $err = array();
  275. $lbl = array();
  276. $msg = array();
  277. // require file
  278. require FRONTEND_CACHE_PATH . '/locale/' . $language . '.php';
  279. // set language specific labels
  280. self::$act = (array) $act;
  281. self::$err = (array) $err;
  282. self::$lbl = (array) $lbl;
  283. self::$msg = (array) $msg;
  284. }
  285. }
  286. /**
  287. * FL (some kind of alias for FrontendLanguage)
  288. *
  289. *
  290. * @package frontend
  291. * @subpackage core
  292. *
  293. * @author Tijs Verkoyen <tijs@netlash.com>
  294. * @since 2.0
  295. */
  296. class FL extends FrontendLanguage
  297. {
  298. /**
  299. * Get an action from the language-file
  300. *
  301. * @return string
  302. * @param string $key The key to get.
  303. */
  304. public static function act($key)
  305. {
  306. return FrontendLanguage::getAction($key);
  307. }
  308. /**
  309. * Get an error from the language-file
  310. *
  311. * @return string
  312. * @param string $key The key to get.
  313. */
  314. public static function err($key)
  315. {
  316. return FrontendLanguage::getError($key);
  317. }
  318. /**
  319. * Get a label from the language-file
  320. *
  321. * @return string
  322. * @param string $key The key to get.
  323. */
  324. public static function lbl($key)
  325. {
  326. return FrontendLanguage::getLabel($key);
  327. }
  328. /**
  329. * Get a message from the language-file
  330. *
  331. * @return string
  332. * @param string $key The key to get.
  333. */
  334. public static function msg($key)
  335. {
  336. return FrontendLanguage::getMessage($key);
  337. }
  338. }
  339. ?>