1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-09 09:54:26 +03:00
ifhub.club/engine/modules/lang/Lang.class.php

168 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2008-11-16 01:11:15 +02:00
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* Модуль поддержки языковых файлов
*
*/
class LsLang extends Module {
/**
* Текущий язык ресурса
*
* @var string
*/
protected $sCurrentLang;
/**
* Язык ресурса, используемый по умолчанию
*
* @var string
*/
protected $sDefaultLang;
/**
* Путь к языковым файлам
*
* @var string
*/
protected $sLangPath;
/**
* @var array
*/
2008-11-16 01:11:15 +02:00
protected $aLangMsg=array();
/**
* Инициализация модуля
*
* @return null
2008-11-16 01:11:15 +02:00
*/
public function Init() {
$this->sCurrentLang = Config::Get('lang.current');
$this->sDefaultLang = Config::Get('lang.default');
$this->sLangPath = Config::Get('lang.path');
$this->InitLang();
2008-11-16 01:11:15 +02:00
}
/**
* Инициализирует языковой файл
*
* @return null
2008-11-16 01:11:15 +02:00
*/
protected function InitLang() {
/**
* Если используется кеширование через memcaсhed, то сохраняем данные языкового файла в кеш
*/
if (Config::Get('sys.cache.type')=='memory') {
2008-11-16 01:11:15 +02:00
if (false === ($this->aLangMsg = $this->Cache_Get("lang_{$this->sCurrentLang}"))) {
2009-11-09 17:17:49 +02:00
$this->aLangMsg=array();
$this->LoadLangFiles($this->sDefaultLang);
2009-11-07 02:35:40 +02:00
if($this->sCurrentLang!=$this->sDefaultLang) $this->LoadLangFiles($this->sCurrentLang);
2008-11-16 01:11:15 +02:00
$this->Cache_Set($this->aLangMsg, "lang_{$this->sCurrentLang}", array(), 60*60);
}
2008-11-16 01:11:15 +02:00
} else {
$this->LoadLangFiles($this->sDefaultLang);
2009-11-07 02:35:40 +02:00
if($this->sCurrentLang!=$this->sDefaultLang) $this->LoadLangFiles($this->sCurrentLang);
}
2008-11-16 01:11:15 +02:00
}
/**
* Загружает текстовки из языковых файлов
*
* @return null
*/
protected function LoadLangFiles($sLangName) {
$sLangFilePath = $this->sLangPath.'/'.$sLangName.'.php';
if(file_exists($sLangFilePath)) {
$this->aLangMsg = (count($this->aLangMsg)==0)
? include($sLangFilePath)
: array_merge($this->aLangMsg,include($sLangFilePath));
}
/**
* Ищет конфиги языковых файлов и объединяет их с текущим
*/
$sDirConfig=$this->sLangPath.'/modules/';
if ($hDirConfig = opendir($sDirConfig)) {
while (false !== ($sDirModule = readdir($hDirConfig))) {
if ($sDirModule !='.' and $sDirModule !='..' and is_dir($sDirConfig.$sDirModule)) {
$sFileConfig=$sDirConfig.$sDirModule.'/'.$sLangName.'.php';
if (file_exists($sFileConfig)) {
$aLangModule=include($sFileConfig);
$this->aLangMsg=array_merge($this->aLangMsg,$aLangModule);
}
}
}
closedir($hDirConfig);
}
}
2008-11-16 01:11:15 +02:00
/**
* Установить текущий язык
*
* @param string $sLang
2008-11-16 01:11:15 +02:00
*/
public function SetLang($sLang) {
$this->sCurrentLang=$sLang;
$this->InitLang();
}
/**
* Получить текущий язык
*
* @return unknown
*/
public function GetLang() {
return $this->sCurrentLang;
}
2009-03-30 18:38:56 +03:00
/**
* Получить список текстовок
*
* @return unknown
*/
public function GetLangMsg() {
return $this->aLangMsg;
}
2008-11-16 01:11:15 +02:00
/**
* Получает текстовку по её имени
*
* @param string $sName
* @param array $aReplace
* @return string
2008-11-16 01:11:15 +02:00
*/
public function Get($sName,$aReplace=array()) {
2008-11-16 01:11:15 +02:00
if (isset($this->aLangMsg[$sName])) {
$sTranslate=$this->aLangMsg[$sName];
if(is_array($aReplace)&&count($aReplace)) {
foreach ($aReplace as $sFrom => $sTo) {
$aReplacePairs["%%{$sFrom}%%"]=$sTo;
}
$sTranslate=strtr($sTranslate,$aReplacePairs);
}
if(Config::Get('module.lang.delete_undefined')) {
$sTranslate=preg_replace("/\%\%[\S]+\%\%/U",'',$sTranslate);
}
return $sTranslate;
2008-11-16 01:11:15 +02:00
}
return 'NOT_FOUND_LANG_TEXT';
}
/**
* Завершаем работу модуля
*
*/
public function Shutdown() {
/**
* Загружаем в шаблон
*/
$this->Viewer_Assign('aLang',$this->aLangMsg);
}
2008-11-16 01:11:15 +02:00
}
?>