|
| 1 | +<?php |
| 2 | +/* |
| 3 | +########################################################################## |
| 4 | +# # |
| 5 | +# Version 4 / / / # |
| 6 | +# -----------__---/__---__------__----__---/---/- # |
| 7 | +# | /| / /___) / ) (_ ` / ) /___) / / # |
| 8 | +# _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___ # |
| 9 | +# Free Content / Management System # |
| 10 | +# / # |
| 11 | +# # |
| 12 | +# # |
| 13 | +# Copyright 2005-2015 by webspell.org # |
| 14 | +# # |
| 15 | +# visit webSPELL.org, webspell.info to get webSPELL for free # |
| 16 | +# - Script runs under the GNU GENERAL PUBLIC LICENSE # |
| 17 | +# - It's NOT allowed to remove this copyright-tag # |
| 18 | +# -- http://www.fsf.org/licensing/licenses/gpl.html # |
| 19 | +# # |
| 20 | +# Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at), # |
| 21 | +# Far Development by Development Team - webspell.org # |
| 22 | +# # |
| 23 | +# visit webspell.org # |
| 24 | +# # |
| 25 | +########################################################################## |
| 26 | +*/ |
| 27 | + |
| 28 | +namespace webspell_ng; |
| 29 | + |
| 30 | +use Noodlehaus\Config; |
| 31 | + |
| 32 | +class Language |
| 33 | +{ |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string $language |
| 37 | + */ |
| 38 | + public $language = 'en'; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string $language_path |
| 42 | + */ |
| 43 | + private $language_path = 'languages/'; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string $language_path |
| 47 | + */ |
| 48 | + private $default_language_path = __DIR__ . '/../languages/'; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var array<string> $module |
| 52 | + */ |
| 53 | + public $module = array(); |
| 54 | + |
| 55 | + /** |
| 56 | + * @var array<string> $module |
| 57 | + */ |
| 58 | + private $module_array = array(); |
| 59 | + |
| 60 | + public function setLanguage(string $to, bool $admin=false): bool |
| 61 | + { |
| 62 | + |
| 63 | + if ($admin) { |
| 64 | + $this->language_path = '../'.$this->language_path; |
| 65 | + } |
| 66 | + |
| 67 | + if (in_array($to, $this->getLanguages())) { |
| 68 | + $this->language = $to; |
| 69 | + $this->language_path = 'languages/'; |
| 70 | + return true; |
| 71 | + } else { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return array<string> |
| 79 | + */ |
| 80 | + private function getLanguages(): array |
| 81 | + { |
| 82 | + $languages = array(); |
| 83 | + foreach (new \DirectoryIterator($this->default_language_path) as $fileInfo) { |
| 84 | + if ($fileInfo->isDot() === false && $fileInfo->isDir() === true) { |
| 85 | + $languages[] = $fileInfo->getFilename(); |
| 86 | + } |
| 87 | + } |
| 88 | + return $languages; |
| 89 | + } |
| 90 | + |
| 91 | + public function getRootPath(): string |
| 92 | + { |
| 93 | + return $this->language_path; |
| 94 | + } |
| 95 | + |
| 96 | + public function readModule(string $module, bool $add=false, bool $admin=false, bool $pluginpath=false, bool $installpath=false): bool |
| 97 | + { |
| 98 | + global $default_language; |
| 99 | + |
| 100 | + $module = str_replace(array('\\', '/', '.'), '', $module); |
| 101 | + |
| 102 | + if ($admin && !$pluginpath) { |
| 103 | + $langFolder = '../' . $this->language_path; |
| 104 | + $folderPath = '%s%s/admin/%s.php'; |
| 105 | + } else if ($admin && $pluginpath) { |
| 106 | + $langFolder = '../' . $pluginpath . $this->language_path; |
| 107 | + $folderPath = '%s%s/admin/%s.php'; |
| 108 | + } else if ($pluginpath) { |
| 109 | + $langFolder = $pluginpath . $this->language_path; |
| 110 | + $folderPath = '%s%s/%s.php'; |
| 111 | + } else if ($installpath) { |
| 112 | + $langFolder = '../install/' . $this->language_path; |
| 113 | + $folderPath = '%s%s/%s.php'; |
| 114 | + } else if (!$admin && is_dir('../languages/')) { |
| 115 | + $langFolder = '../' . $this->language_path; |
| 116 | + $folderPath = '%s%s/%s.php'; |
| 117 | + } else { |
| 118 | + $langFolder = $this->language_path; |
| 119 | + $folderPath = '%s%s/%s.php'; |
| 120 | + } |
| 121 | + |
| 122 | + $languageFallbackTable = array(); |
| 123 | + if (!empty($this->language)) { |
| 124 | + $languageFallbackTable[] = $this->language; |
| 125 | + } |
| 126 | + if (!empty($default_language)) { |
| 127 | + $languageFallbackTable[] = $default_language; |
| 128 | + } |
| 129 | + if (!in_array('en', $languageFallbackTable)) { |
| 130 | + $languageFallbackTable[] = 'en'; |
| 131 | + } |
| 132 | + |
| 133 | + foreach ($languageFallbackTable as $folder) { |
| 134 | + |
| 135 | + if (empty($folder)) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + $path = sprintf($folderPath, $langFolder, $folder, $module); |
| 140 | + if (file_exists($path)) { |
| 141 | + $module_file = $path; |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + if (!isset($module_file)) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + $this->module_array[] = $module_file; |
| 152 | + |
| 153 | + include($module_file); |
| 154 | + |
| 155 | + if (!$add) { |
| 156 | + $this->module = array(); |
| 157 | + } |
| 158 | + |
| 159 | + foreach ($language_array as $key => $val) { |
| 160 | + $this->module[ $key ] = $val; |
| 161 | + } |
| 162 | + |
| 163 | + $formvalidation = 'formvalidation'; |
| 164 | + if (!in_array($formvalidation, $this->module_array) && ($module != $formvalidation)) { |
| 165 | + $this->readModule($formvalidation, true, false, false); |
| 166 | + } |
| 167 | + |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + public function replace(string $template): string |
| 172 | + { |
| 173 | + foreach ($this->module as $key => $val) { |
| 174 | + $template = str_replace('%' . $key . '%', $val, $template); |
| 175 | + } |
| 176 | + return $template; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @return array<string> |
| 181 | + */ |
| 182 | + public function getTranslationTable(): array |
| 183 | + { |
| 184 | + $map = array(); |
| 185 | + foreach ($this->module as $key => $val) { |
| 186 | + $newKey = '%' . $key . '%'; |
| 187 | + $map[ $newKey ] = $val; |
| 188 | + } |
| 189 | + return $map; |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments