PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/includes/modinstalllexicon.class.php

http://github.com/modxcms/revolution
PHP | 205 lines | 115 code | 14 blank | 76 comment | 25 complexity | ce189e6aa39b161d149221d2c4ac362c MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * modLexicon
  12. *
  13. * @package modx
  14. */
  15. /**
  16. * The lexicon handling class for setup.
  17. *
  18. * @package modx
  19. */
  20. class modInstallLexicon {
  21. /**
  22. * @var modInstall $install Reference to the modInstall instance.
  23. */
  24. public $install = null;
  25. /**
  26. * @var array $_lexicon The translated lexicon array
  27. */
  28. protected $_lexicon = array();
  29. function __construct(modInstall &$install,array $config = array()) {
  30. $this->install =& $install;
  31. $this->config = array_merge(array(
  32. 'lexiconPath' => dirname(__DIR__).'/lang/',
  33. ),$config);
  34. }
  35. /**
  36. * Gets and parses a lexicon entry.
  37. * @param string $key The key to grab
  38. * @param array $placeholders Any values to replace placeholders with
  39. * @return string The translated key.
  40. */
  41. public function get($key,array $placeholders = array()) {
  42. $v = '';
  43. if ($this->exists($key)) {
  44. $v = $this->parse($this->_lexicon[$key],$placeholders);
  45. }
  46. return $v;
  47. }
  48. /**
  49. * Sets a lexicon entry value.
  50. * @param string $key The key to set the value to.
  51. * @param string $value The value to set.
  52. * @return string The set value.
  53. */
  54. public function set($key,$value = '') {
  55. $this->_lexicon[$key] = $value;
  56. return $value;
  57. }
  58. /**
  59. * Parses a lexicon string for placeholder replacement
  60. * @param string $str
  61. * @param array $placeholders An array of placeholders
  62. * @return string
  63. */
  64. public function parse($str = '',array $placeholders = array()) {
  65. if (empty($str)) return '';
  66. if (empty($placeholders) || !is_array($placeholders)) return $str;
  67. foreach ($placeholders as $k => $v) {
  68. $str = str_replace('[[+'.$k.']]',$v,$str);
  69. }
  70. return $str;
  71. }
  72. /**
  73. * Checks if a key exists in the currently loaded lexicon
  74. *
  75. * @param string $key
  76. * @return boolean True if key is found
  77. */
  78. public function exists($key) {
  79. return array_key_exists($key,$this->_lexicon);
  80. }
  81. /**
  82. * Accessor method for the lexicon array.
  83. *
  84. * @access public
  85. * @param string $prefix If set, will only return the lexicon entries with this prefix.
  86. * @param boolean If true, will strip the prefix from the returned indexes
  87. * @return array The internal lexicon.
  88. */
  89. public function fetch($prefix = '',$removePrefix = false) {
  90. if (!empty($prefix)) {
  91. $lex = array();
  92. $lang = $this->_lexicon;
  93. foreach ($lang as $k => $v) {
  94. if (strpos($k,$prefix) !== false) {
  95. $key = $removePrefix ? str_replace($prefix,'',$k) : $k;
  96. $lex[$key] = $v;
  97. }
  98. }
  99. return $lex;
  100. }
  101. return $this->_lexicon;
  102. }
  103. /**
  104. * Returns the currently specified language.
  105. * @return string The IANA language code
  106. */
  107. public function getLanguage() {
  108. $language = 'en';
  109. if (isset ($_COOKIE['modx_setup_language'])) {
  110. $language= $_COOKIE['modx_setup_language'];
  111. } else {
  112. $availableLangs = $this->getLanguageList();
  113. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  114. // break up string into pieces (languages and q factors)
  115. preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
  116. if (count($lang_parse[1])) {
  117. // create a list like "en" => 0.8
  118. $acceptLangs = array_combine($lang_parse[1], $lang_parse[4]);
  119. // set default to 1 for any without q factor
  120. foreach ($acceptLangs as $lang => $q) {
  121. if ($q === '') $acceptLangs[$lang] = 1;
  122. }
  123. // sort list based on value
  124. arsort($acceptLangs, SORT_NUMERIC);
  125. foreach ($acceptLangs as $lang => $q) {
  126. $primary = explode('-', $lang);
  127. $primary = array_shift($primary);
  128. if (in_array($lang, $availableLangs)) {
  129. $language = $lang;
  130. break;
  131. } else if (in_array($primary, $availableLangs)) {
  132. $language = $primary;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. if (!empty($this->install) && !empty($this->install->settings) && is_object($this->install->settings)) {
  140. $language = $this->install->settings->get('language', $language);
  141. }
  142. return $language;
  143. }
  144. /**
  145. * Get a list of available languages.
  146. *
  147. * @return array An array of available languages
  148. */
  149. public function getLanguageList() {
  150. $path = dirname(__DIR__).'/lang/';
  151. $languages = array();
  152. /** @var DirectoryIterator $file */
  153. foreach (new DirectoryIterator($path) as $file) {
  154. $basename = $file->getFilename();
  155. if (!in_array($basename, array('.', '..','.htaccess','.svn','.git')) && $file->isDir()) {
  156. if (file_exists($file->getPathname().'/default.inc.php')) {
  157. $languages[] = $basename;
  158. }
  159. }
  160. }
  161. sort($languages);
  162. return $languages;
  163. }
  164. /**
  165. * Loads a lexicon topic.
  166. *
  167. * @param string/array $topics A string name of a topic (or an array of topic names)
  168. * @return boolean True if successful.
  169. */
  170. public function load($topics) {
  171. $loaded = false;
  172. $language = $this->getLanguage();
  173. if (!is_array($topics)) {
  174. $topics = array($topics);
  175. }
  176. foreach ($topics as $topic) {
  177. $topicFile = $this->config['lexiconPath'].$language.'/'.$topic.'.inc.php';
  178. if (file_exists($topicFile)) {
  179. $_lang = array();
  180. include $topicFile;
  181. if (is_array($_lang) && !empty($_lang)) {
  182. $this->_lexicon = array_merge($this->_lexicon,$_lang);
  183. $loaded = true;
  184. } else {
  185. $loaded = false;
  186. }
  187. }
  188. }
  189. return $loaded;
  190. }
  191. }