@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | $this->container = array(); |
| 37 | 37 | |
| 38 | - if(classSupernova::$cache->_MODE != CACHER_NO_CACHE && !classSupernova::$config->locale_cache_disable) { |
|
| 38 | + if (classSupernova::$cache->_MODE != CACHER_NO_CACHE && !classSupernova::$config->locale_cache_disable) { |
|
| 39 | 39 | $this->cache = classSupernova::$cache; |
| 40 | 40 | classSupernova::log_file('locale.__constructor: Cache is present'); |
| 41 | 41 | //$this->cache->unset_by_prefix($this->cache_prefix); // TODO - remove? 'cause debug! |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if($enable_stat_usage && empty($this->stat_usage)) { |
|
| 44 | + if ($enable_stat_usage && empty($this->stat_usage)) { |
|
| 45 | 45 | $this->enable_stat_usage = $enable_stat_usage; |
| 46 | 46 | $this->usage_stat_load(); |
| 47 | 47 | // TODO shutdown function |
@@ -67,18 +67,18 @@ discard block |
||
| 67 | 67 | unset($fallback[$this->active]); |
| 68 | 68 | |
| 69 | 69 | // Проходим по оставшимся локалям |
| 70 | - foreach($fallback as $try_language) { |
|
| 70 | + foreach ($fallback as $try_language) { |
|
| 71 | 71 | // Если нет такой строки - пытаемся вытащить из кэша |
| 72 | - if(!isset($this->container[$try_language][$offset]) && $this->cache) { |
|
| 73 | - $this->container[$try_language][$offset] = $this->cache->__get($this->cache_prefix . $try_language . '_' . $offset); |
|
| 72 | + if (!isset($this->container[$try_language][$offset]) && $this->cache) { |
|
| 73 | + $this->container[$try_language][$offset] = $this->cache->__get($this->cache_prefix.$try_language.'_'.$offset); |
|
| 74 | 74 | // Записываем результат работы кэша |
| 75 | 75 | $locale_cache_statistic['queries']++; |
| 76 | 76 | isset($this->container[$try_language][$offset]) ? $locale_cache_statistic['hits']++ : $locale_cache_statistic['misses']++; |
| 77 | -!isset($this->container[$try_language][$offset]) ? $locale_cache_statistic['missed_str'][] = $this->cache_prefix . $try_language . '_' . $offset : false; |
|
| 77 | +!isset($this->container[$try_language][$offset]) ? $locale_cache_statistic['missed_str'][] = $this->cache_prefix.$try_language.'_'.$offset : false; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // Если мы как-то где-то нашли строку... |
| 81 | - if(isset($this->container[$try_language][$offset])) { |
|
| 81 | + if (isset($this->container[$try_language][$offset])) { |
|
| 82 | 82 | // ...значит она получена в результате фоллбэка и записываем её в кэш и контейнер |
| 83 | 83 | $this[$offset] = $this->container[$try_language][$offset]; |
| 84 | 84 | $locale_cache_statistic['fallbacks']++; |
@@ -94,16 +94,16 @@ discard block |
||
| 94 | 94 | $this->container[$this->active][] = $value; |
| 95 | 95 | } else { |
| 96 | 96 | $this->container[$this->active][$offset] = $value; |
| 97 | - if($this->cache) { |
|
| 98 | - $this->cache->__set($this->cache_prefix_lang . $offset, $value); |
|
| 97 | + if ($this->cache) { |
|
| 98 | + $this->cache->__set($this->cache_prefix_lang.$offset, $value); |
|
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | public function offsetExists($offset) { |
| 103 | 103 | // Шорткат если у нас уже есть строка в памяти PHP |
| 104 | - if(!isset($this->container[$this->active][$offset])) { |
|
| 104 | + if (!isset($this->container[$this->active][$offset])) { |
|
| 105 | 105 | // pdump($this->cache_prefix_lang . $offset); |
| 106 | - if(!$this->cache || !($this->container[$this->active][$offset] = $this->cache->__get($this->cache_prefix_lang . $offset))) { |
|
| 106 | + if (!$this->cache || !($this->container[$this->active][$offset] = $this->cache->__get($this->cache_prefix_lang.$offset))) { |
|
| 107 | 107 | // pdump($this->cache_prefix_lang . $offset); |
| 108 | 108 | // Если нету такой строки - делаем фоллбэк |
| 109 | 109 | $this->locale_string_fallback($offset); |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | } |
| 120 | 120 | public function offsetGet($offset) { |
| 121 | 121 | $value = $this->offsetExists($offset) ? $this->container[$this->active][$offset] : null; |
| 122 | - if($this->enable_stat_usage) { |
|
| 122 | + if ($this->enable_stat_usage) { |
|
| 123 | 123 | $this->usage_stat_log($offset, $value); |
| 124 | 124 | } |
| 125 | 125 | return $value; |
@@ -134,25 +134,25 @@ discard block |
||
| 134 | 134 | |
| 135 | 135 | |
| 136 | 136 | public function usage_stat_load() { |
| 137 | - $this->stat_usage = classSupernova::$cache->lng_stat_usage = array(); // TODO for debug |
|
| 138 | - if(empty($this->stat_usage)) { |
|
| 137 | + $this->stat_usage = classSupernova::$cache->lng_stat_usage = array(); // TODO for debug |
|
| 138 | + if (empty($this->stat_usage)) { |
|
| 139 | 139 | $query = doquery("SELECT * FROM {{lng_usage_stat}}"); |
| 140 | - while($row = db_fetch($query)) { |
|
| 141 | - $this->stat_usage[$row['lang_code'] . ':' . $row['string_id'] . ':' . $row['file'] . ':' . $row['line']] = $row['is_empty']; |
|
| 140 | + while ($row = db_fetch($query)) { |
|
| 141 | + $this->stat_usage[$row['lang_code'].':'.$row['string_id'].':'.$row['file'].':'.$row['line']] = $row['is_empty']; |
|
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | public function usage_stat_save() { |
| 146 | - if(!empty($this->stat_usage_new)) { |
|
| 146 | + if (!empty($this->stat_usage_new)) { |
|
| 147 | 147 | classSupernova::$cache->lng_stat_usage = $this->stat_usage; |
| 148 | 148 | doquery("SELECT 1 FROM {{lng_usage_stat}} LIMIT 1"); |
| 149 | - foreach($this->stat_usage_new as &$value) { |
|
| 150 | - foreach($value as &$value2) { |
|
| 151 | - $value2 = '"' . db_escape($value2) . '"'; |
|
| 149 | + foreach ($this->stat_usage_new as &$value) { |
|
| 150 | + foreach ($value as &$value2) { |
|
| 151 | + $value2 = '"'.db_escape($value2).'"'; |
|
| 152 | 152 | } |
| 153 | - $value = '(' . implode(',', $value) .')'; |
|
| 153 | + $value = '('.implode(',', $value).')'; |
|
| 154 | 154 | } |
| 155 | - doquery("REPLACE INTO {{lng_usage_stat}} (lang_code,string_id,`file`,line,is_empty,locale) VALUES " . implode(',', $this->stat_usage_new)); |
|
| 155 | + doquery("REPLACE INTO {{lng_usage_stat}} (lang_code,string_id,`file`,line,is_empty,locale) VALUES ".implode(',', $this->stat_usage_new)); |
|
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | public function usage_stat_log(&$offset, &$value) { |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | |
| 163 | 163 | $file = str_replace('\\', '/', substr($trace[1]['file'], strlen(SN_ROOT_PHYSICAL) - 1)); |
| 164 | 164 | |
| 165 | - $string_id = $this->active . ':' . $offset . ':' . $file . ':' . $trace[1]['line']; |
|
| 166 | - if(!isset($this->stat_usage[$string_id]) || $this->stat_usage[$string_id] != empty($value)) { |
|
| 165 | + $string_id = $this->active.':'.$offset.':'.$file.':'.$trace[1]['line']; |
|
| 166 | + if (!isset($this->stat_usage[$string_id]) || $this->stat_usage[$string_id] != empty($value)) { |
|
| 167 | 167 | $this->stat_usage[$string_id] = empty($value); |
| 168 | 168 | $this->stat_usage_new[] = array( |
| 169 | 169 | 'lang_code' => $this->active, |
@@ -171,14 +171,14 @@ discard block |
||
| 171 | 171 | 'file' => $file, |
| 172 | 172 | 'line' => $trace[1]['line'], |
| 173 | 173 | 'is_empty' => intval(empty($value)), |
| 174 | - 'locale' => '' . $value, |
|
| 174 | + 'locale' => ''.$value, |
|
| 175 | 175 | ); |
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | protected function lng_try_filepath($path, $file_path_relative) { |
| 181 | - $file_path = SN_ROOT_PHYSICAL . ($path && file_exists(SN_ROOT_PHYSICAL . $path . $file_path_relative) ? $path : '') . $file_path_relative; |
|
| 181 | + $file_path = SN_ROOT_PHYSICAL.($path && file_exists(SN_ROOT_PHYSICAL.$path.$file_path_relative) ? $path : '').$file_path_relative; |
|
| 182 | 182 | return file_exists($file_path) ? $file_path : false; |
| 183 | 183 | } |
| 184 | 184 | |
@@ -200,14 +200,14 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | classSupernova::log_file("locale.include: Loading data from domain '{$filename}'", 1); |
| 202 | 202 | |
| 203 | - $cache_file_key = $this->cache_prefix_lang . '__' . $filename; |
|
| 203 | + $cache_file_key = $this->cache_prefix_lang.'__'.$filename; |
|
| 204 | 204 | |
| 205 | 205 | // Подключен ли внешний кэш? |
| 206 | - if($this->cache) { |
|
| 206 | + if ($this->cache) { |
|
| 207 | 207 | // Загружен ли уже данный файл? |
| 208 | 208 | $cache_file_status = $this->cache->__get($cache_file_key); |
| 209 | - classSupernova::log_file("locale.include: Cache - '{$filename}' has key '{$cache_file_key}' and is " . ($cache_file_status ? 'already loaded - EXIT' : 'EMPTY'), $cache_file_status ? -1 : 0); |
|
| 210 | - if($cache_file_status) { |
|
| 209 | + classSupernova::log_file("locale.include: Cache - '{$filename}' has key '{$cache_file_key}' and is ".($cache_file_status ? 'already loaded - EXIT' : 'EMPTY'), $cache_file_status ? -1 : 0); |
|
| 210 | + if ($cache_file_status) { |
|
| 211 | 211 | // Если да - повторять загрузку нет смысла |
| 212 | 212 | return null; |
| 213 | 213 | } |
@@ -221,47 +221,47 @@ discard block |
||
| 221 | 221 | $this->make_fallback($language); |
| 222 | 222 | |
| 223 | 223 | $file_path = ''; |
| 224 | - foreach($this->fallback as $lang_try) { |
|
| 225 | - if(!$lang_try /* || isset($language_tried[$lang_try]) */) { |
|
| 224 | + foreach ($this->fallback as $lang_try) { |
|
| 225 | + if (!$lang_try /* || isset($language_tried[$lang_try]) */) { |
|
| 226 | 226 | continue; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - if($file_path = $this->lng_try_filepath($path, "language/{$lang_try}/{$filename_ext}")) { |
|
| 229 | + if ($file_path = $this->lng_try_filepath($path, "language/{$lang_try}/{$filename_ext}")) { |
|
| 230 | 230 | break; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - if($file_path = $this->lng_try_filepath($path, "language/{$filename}_{$lang_try}{$ext}")) { |
|
| 233 | + if ($file_path = $this->lng_try_filepath($path, "language/{$filename}_{$lang_try}{$ext}")) { |
|
| 234 | 234 | break; |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | $file_path = ''; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - if($file_path) { |
|
| 240 | + if ($file_path) { |
|
| 241 | 241 | $a_lang_array = array(); |
| 242 | 242 | include($file_path); |
| 243 | 243 | |
| 244 | - if(!empty($a_lang_array)) { |
|
| 244 | + if (!empty($a_lang_array)) { |
|
| 245 | 245 | $this->merge($a_lang_array); |
| 246 | 246 | |
| 247 | 247 | // Загрузка данных из файла в кэш |
| 248 | - if($this->cache) { |
|
| 248 | + if ($this->cache) { |
|
| 249 | 249 | classSupernova::log_file("Locale: loading '{$filename}' into cache"); |
| 250 | - foreach($a_lang_array as $key => $value) { |
|
| 251 | - $value_cache_key = $this->cache_prefix_lang . $key; |
|
| 252 | - if($this->cache->__isset($value_cache_key)) { |
|
| 253 | - if(is_array($value)) { |
|
| 250 | + foreach ($a_lang_array as $key => $value) { |
|
| 251 | + $value_cache_key = $this->cache_prefix_lang.$key; |
|
| 252 | + if ($this->cache->__isset($value_cache_key)) { |
|
| 253 | + if (is_array($value)) { |
|
| 254 | 254 | $alt_value = $this->cache->__get($value_cache_key); |
| 255 | 255 | $value = array_replace_recursive($alt_value, $value); |
| 256 | 256 | // pdump($alt_value, $alt_value); |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | - $this->cache->__set($this->cache_prefix_lang . $key, $value); |
|
| 259 | + $this->cache->__set($this->cache_prefix_lang.$key, $value); |
|
| 260 | 260 | } |
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | - if($this->cache) { |
|
| 264 | + if ($this->cache) { |
|
| 265 | 265 | $this->cache->__set($cache_file_key, true); |
| 266 | 266 | } |
| 267 | 267 | |
@@ -274,14 +274,14 @@ discard block |
||
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | public function lng_load_i18n($i18n) { |
| 277 | - if(!isset($i18n)) { |
|
| 277 | + if (!isset($i18n)) { |
|
| 278 | 278 | return; |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | - foreach($i18n as $i18n_data) { |
|
| 282 | - if(is_string($i18n_data)) { |
|
| 281 | + foreach ($i18n as $i18n_data) { |
|
| 282 | + if (is_string($i18n_data)) { |
|
| 283 | 283 | $this->lng_include($i18n_data); |
| 284 | - } elseif(is_array($i18n_data)) { |
|
| 284 | + } elseif (is_array($i18n_data)) { |
|
| 285 | 285 | $this->lng_include($i18n_data['file'], $i18n_data['path']); |
| 286 | 286 | } |
| 287 | 287 | } |
@@ -299,27 +299,27 @@ discard block |
||
| 299 | 299 | |
| 300 | 300 | classSupernova::log_file("locale.switch: Trying to switch language to '{$language_new}'"); |
| 301 | 301 | |
| 302 | - if($language_new == $this->active) { |
|
| 302 | + if ($language_new == $this->active) { |
|
| 303 | 303 | classSupernova::log_file("locale.switch: New language '{$language_new}' is equal to current language '{$this->active}' - EXIT", -1); |
| 304 | 304 | return false; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | $this->active = $language = $language_new; |
| 308 | - $this->cache_prefix_lang = $this->cache_prefix . $this->active . '_'; |
|
| 308 | + $this->cache_prefix_lang = $this->cache_prefix.$this->active.'_'; |
|
| 309 | 309 | |
| 310 | 310 | $this['LANG_INFO'] = $this->lng_get_info($this->active); |
| 311 | 311 | $this->make_fallback($this->active); |
| 312 | 312 | |
| 313 | - if($this->cache) { |
|
| 314 | - $cache_lang_init_status = $this->cache->__get($this->cache_prefix_lang . '__INIT'); |
|
| 315 | - classSupernova::log_file("locale.switch: Cache for '{$this->active}' prefixed '{$this->cache_prefix_lang}' is " . ($cache_lang_init_status ? 'already loaded. Doing nothing - EXIT' : 'EMPTY'), $cache_lang_init_status ? -1 : 0); |
|
| 316 | - if($cache_lang_init_status) { |
|
| 313 | + if ($this->cache) { |
|
| 314 | + $cache_lang_init_status = $this->cache->__get($this->cache_prefix_lang.'__INIT'); |
|
| 315 | + classSupernova::log_file("locale.switch: Cache for '{$this->active}' prefixed '{$this->cache_prefix_lang}' is ".($cache_lang_init_status ? 'already loaded. Doing nothing - EXIT' : 'EMPTY'), $cache_lang_init_status ? -1 : 0); |
|
| 316 | + if ($cache_lang_init_status) { |
|
| 317 | 317 | return false; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | // Чистим текущие локализации из кэша. Достаточно почистить только флаги инициализации языкового кэша и загрузки файлов - они начинаются с '__' |
| 321 | 321 | classSupernova::log_file("locale.switch: Cache - invalidating data"); |
| 322 | - $this->cache->unset_by_prefix($this->cache_prefix_lang . '__'); |
|
| 322 | + $this->cache->unset_by_prefix($this->cache_prefix_lang.'__'); |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | $this->lng_include('system'); |
@@ -329,9 +329,9 @@ discard block |
||
| 329 | 329 | // Loading global language files |
| 330 | 330 | $this->lng_load_i18n(classSupernova::$sn_mvc['i18n']['']); |
| 331 | 331 | |
| 332 | - if($this->cache) { |
|
| 333 | - classSupernova::log_file("locale.switch: Cache - setting flag " . $this->cache_prefix_lang . '__INIT'); |
|
| 334 | - $this->cache->__set($this->cache_prefix_lang . '__INIT', true); |
|
| 332 | + if ($this->cache) { |
|
| 333 | + classSupernova::log_file("locale.switch: Cache - setting flag ".$this->cache_prefix_lang.'__INIT'); |
|
| 334 | + $this->cache->__set($this->cache_prefix_lang.'__INIT', true); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | classSupernova::log_file("locale.switch: Complete - EXIT"); |
@@ -341,9 +341,9 @@ discard block |
||
| 341 | 341 | |
| 342 | 342 | |
| 343 | 343 | public function lng_get_info($entry) { |
| 344 | - $file_name = SN_ROOT_PHYSICAL . 'language/' . $entry . '/language.mo.php'; |
|
| 344 | + $file_name = SN_ROOT_PHYSICAL.'language/'.$entry.'/language.mo.php'; |
|
| 345 | 345 | $lang_info = array(); |
| 346 | - if(file_exists($file_name)) { |
|
| 346 | + if (file_exists($file_name)) { |
|
| 347 | 347 | include($file_name); |
| 348 | 348 | } |
| 349 | 349 | |
@@ -351,15 +351,15 @@ discard block |
||
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | public function lng_get_list() { |
| 354 | - if(empty($this->lang_list)) { |
|
| 354 | + if (empty($this->lang_list)) { |
|
| 355 | 355 | $this->lang_list = array(); |
| 356 | 356 | |
| 357 | - $path = SN_ROOT_PHYSICAL . 'language/'; |
|
| 357 | + $path = SN_ROOT_PHYSICAL.'language/'; |
|
| 358 | 358 | $dir = dir($path); |
| 359 | - while(false !== ($entry = $dir->read())) { |
|
| 360 | - if(is_dir($path . $entry) && $entry[0] != '.') { |
|
| 359 | + while (false !== ($entry = $dir->read())) { |
|
| 360 | + if (is_dir($path.$entry) && $entry[0] != '.') { |
|
| 361 | 361 | $lang_info = $this->lng_get_info($entry); |
| 362 | - if($lang_info['LANG_NAME_ISO2'] == $entry) { |
|
| 362 | + if ($lang_info['LANG_NAME_ISO2'] == $entry) { |
|
| 363 | 363 | $this->lang_list[$lang_info['LANG_NAME_ISO2']] = $lang_info; |
| 364 | 364 | } |
| 365 | 365 | } |