Web multiple languages
-
Sveiki. Reik trupucio pagalbos. Notiu kad vietoj tbox saraso man butu mygtukai language, kuri paspausi ta ir ijungs. Taciau padarius neveikia. Originalas:
$languageList = explode(',', e_LANLIST);
sort($languageList);
$action = (e_QUERY && ! $_GET['elan']) ? e_SELF.'?'.e_QUERY : e_SELF;
$text = '
<form method="post" action="'.$action.'">
<div class="center">
<select name="sitelanguage" class="tbox form-control">';
foreach($languageList as $languageFolder)
{
$selected = ($languageFolder == e_LANGUAGE) ? ' selected="selected"' : '';
$text .= '
<option value="'.$languageFolder.'"'.$selected.'>'.$languageFolder.'</option>'.$selected.'
'.$languageFolder.'';
}
$text .= '
</select>
<br />
<br />
<button class="btn btn-default button" type="submit" name="setlanguage" value="no-value"><span>'.UTHEME_MENU_L1.'</span></button>';
$text .= '
</div>
</form>';
Mano variantas:
$action = (e_QUERY && ! $_GET['elan']) ? e_SELF.'?'.e_QUERY : e_SELF;
$text = '
<form method="post" action="'.$action.'">
<div class="center">';
foreach($languageList as $languageFolder)
{
$text .= '
<button class="btn btn-default button" type="submit" name="setlanguage" value="'.$languageFolder.'">'.$languageFolder.'</button>
';
}
$text .= '
</div>
</form>';
Ant mygtuku rodomi pavadinimai, taciau paspaudus niekas nevyksta. Gal bus zinantis ko dar truksta. Dekui is anksto ! :) Paskutinį kartą redagavo: 1 Lapkričio 2016 - 18:37
1 Lapkričio 2016 - 18:36 / #1
-
Sveikas, Jeigu vadovaujantis originalu, tai pas tave nėra nustatytas <select>
, <option>
elementai, t.y $_POST['sitelanguage']
. Tikriausiai yra tikrinamas isset($_POST['sitelanguage'])
, o jeigu jis nėra nurodytas, skriptas nebus vykdomas.
11 Lapkričio 2016 - 21:06 / #2
-
Greisis7, Hmm, nu padarius toki button visvien neveikia. <button class="btn btn-default button" type="submit" name="sitelanguage" value="Lithuanian"><span>'Lithuanian'</span></button>
12 Lapkričio 2016 - 21:33 / #3
-
Parašyk skriptą pilnai ir pataisysiu. Dabar nematau tikrinimų. O padarius ankstesnį pakeitimą, tai tada $_POST['setlanguage'] nėra nurodytas, skriptas nebus vykdomas. Ištrink isset($_POST['setlanguage']) ir veiks. O nenori naudoti $_GET metodą kalbos pasirinkimui?
15 Lapkričio 2016 - 09:35 / #4
-
Sunkiai man su php dar :D require_once(e_HANDLER.'language_class.php');
$slng = new language;
$languageList = explode(',', e_LANLIST);
sort($languageList);
if(varset($pref['multilanguage_subdomain']))
{
$action = (e_QUERY) ? e_SELF.'?'.e_QUERY : e_SELF;
$text = '
<div style="text-align:center">
<select class="tbox form-control" name="lang_select" style="width:95%" onchange="location.href=this.options[selectedIndex].value">';
foreach($languageList as $languageFolder)
{
$selected = ($languageFolder == e_LANGUAGE) ? ' selected="selected"' : '';
$urlval = $slng->subdomainUrl($languageFolder);
$text .= '
<option value="'.$urlval.'"'.$selected.'>'.$languageFolder.'</option>';
}
$text .= '
</select>
</div>';
}
else
{
//FIXME may not work with session
$action = (e_QUERY && ! $_GET['elan']) ? e_SELF.'?'.e_QUERY : e_SELF;
$text = '
<form method="post" action="'.$action.'">
<div class="center">
<select name="sitelanguage" class="tbox form-control">';
foreach($languageList as $languageFolder)
{
$selected = ($languageFolder == e_LANGUAGE) ? ' selected="selected"' : '';
$text .= '
<option value="'.$languageFolder.'"'.$selected.'>'.$languageFolder.'</option>'.$selected.'
'.$languageFolder.'';
}
$text .= '
</select>
<br />
<br />
<button class="btn btn-default button" type="submit" name="setlanguage" value="no-value"><span>'.UTHEME_MENU_L1.'</span></button>';
$text .= '
</div>
</form>';
}
$ns->tablerender(UTHEME_MENU_L2, $text, 'user_lan');
15 Lapkričio 2016 - 19:30 / #5
-
Trūksta language_class.php kodo.
15 Lapkričio 2016 - 20:28 / #6
-
Va :) class language{
// http://www.loc.gov/standards/iso639-2/php/code_list.php
// Valid Language Pack Names are shown directly below on the right.
public $detect = false;
public $e_language = 'English'; // replaced later with $pref
public $_cookie_domain = '';
/**
* Cached list of Installed Language Packs
* @var array
*/
protected $lanlist = null; // null is important!!!
protected $list = array(
"na" => "Nauru",
"nv" => "Navajo",
"zu" => "Zulu"
);
protected $names = array(
"Vietnamese" => "Tiếng Việt",
"Welsh" => "Cymraeg"
);
/**
* Converts iso to language-name and visa-versa.
* @param object $data
* @return
*/
function convert($data){
if(strlen($data) > 2)
{
$tmp = array_flip($this->list);
return isset($tmp[$data]) ? $tmp[$data] : false;
}
else
{
return (isset($this->list[$data])) ? $this->list[$data] : false;
}
}
// -------------------------------------------------------------------
/**
* Check if a Language is installed and valid
* @param object $lang - Language to check. eg. 'es' or 'Spanish'
* @return false or the name of the valid Language
*/
function isValid($lang='')
{
if(empty($lang))
{
return false;
}
global $pref;
if(!$lang)
{
return (ADMIN_AREA && vartrue($pref['adminlanguage'])) ? $pref['adminlanguage'] : $pref['sitelanguage'];
}
if(strpos($lang,"debug")!==false)
{
return false;
}
if($lang == 'E_SITELANGUAGE') // allows for overriding language using a scripted 'define' before class2.php is loaded.
{
$lang = $pref['sitelanguage'];
}
if($lang == 'E_ADMINLANGUAGE')
{
$lang = $pref['adminlanguage'];
}
if(strlen($lang)== 2)
{
$iso = $lang;
$lang = $this->convert($lang);
}
else
{
$iso = $this->convert($lang);
}
if($iso==false || $lang==false)
{
$diz = ($lang) ? $lang : $iso;
trigger_error("The selected language (".$diz.") is invalid. See e107_handlers/language_class.php for a list of valid languages. ", E_USER_ERROR);
return false;
}
if(is_readable(e_LANGUAGEDIR.$lang.'/'.$lang.'.php'))
{
return $lang;
}
else
{
trigger_error("The selected language (".$lang.") was not found.", E_USER_ERROR);
return false;
}
}
/**
* Check if the specified domain has multi-language subdomains enabled.
* @return
*/
function isLangDomain($domain='')
{
if(!$domain)
{
return false;
}
global $pref;
$mtmp = explode("\n", $pref['multilanguage_subdomain']);
foreach($mtmp as $val)
{
if($domain == trim($val))
{
return true;
}
}
if(!empty($pref['multilanguage_domain']) && is_array($pref['multilanguage_domain']))
{
foreach($pref['multilanguage_domain'] as $lng=>$val)
{
if($domain == trim($val))
{
return $lng;
}
}
}
return false;
}
/**
* Generic variable translator for LAN definitions.
* @example $lng->translate("My name is [x] and I own a [y]", array('x'=>"John", 'y'=>"Cat"));
* @deprecated Use $tp->lanVars() instead.
*/
function translate($lan, $array= array())
{
foreach($array as $k=>$v)
{
$search[] = "[".$k."]";
$replace[] = "<b>".$v."</b>";
}
return str_replace($search, $replace, $lan);
}
/**
* Return a list of Installed Language Packs
* @param str $type - English or Native.
* @example type = english: array(0=>'English', 1=>'French' ...)
* @example type = native: array('English'=>'English', 'French'=>'Francais'...)
* @return array
*/
function installed($type='english')
{
if(null == $this->lanlist)
{
$fl = e107::getFile();
$dirArray = $fl->get_dirs(e_LANGUAGEDIR);
// $handle = opendir(e_LANGUAGEDIR);
$lanlist = array();
// while ($file = readdir($handle))
foreach($dirArray as $file)
{
if ($file != '.' && $file != '..' && is_readable(e_LANGUAGEDIR.$file.'/'.$file.'.php'))
{
$lanlist[] = $file;
}
}
// closedir($handle);
$this->lanlist = array_intersect($lanlist,$this->list);
}
if($type == 'native')
{
$natList = array();
foreach($this->lanlist as $lang)
{
$natList[$lang] = $this->toNative($lang);
}
natsort($natList);
return $natList;
}
return $this->lanlist;
}
/**
* Convert a Language to its Native title. eg. 'Spanish' becomes 'Español'
* @param string $lang
* @return string
*/
function toNative($lang)
{
return (!empty($this->names[$lang])) ? $this->names[$lang] : $lang;
}
/**
* Convert the current URL to a multi-lang for the specified language.
* eg. 'http://www.mydomain.com' becomes 'http://es.mydomain.com'
* @param string $language eg. 'Spanish'
* @return URL
*/
function subdomainUrl($language, $url=e_REQUEST_URL)
{
global $pref;
$iso = (strlen($language) == 2) ? $language : $this->convert($language);
$codelnk = ($language == $pref['sitelanguage']) ? "www" : $iso;
if($codelnk == '')
{
$codelnk = 'www';
}
// $urlval = str_replace($_SERVER['HTTP_HOST'],$codelnk.".".e_DOMAIN,e_SELF);
/* $urlval = (e_QUERY)
? str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF).'?'.e_QUERY
: str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF);
*/
$domain = deftrue('e_DOMAIN','example.com');
$urlval = str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.$domain, $url) ;
return (string) $urlval;
}
/**
* Detect a Language Change
* 1. Scripted Definition eg. define('e_PAGE_LANGUAGE', 'English');
* 2. Parked Domain eg. http://mylanguagedomain.com
* 3. Parked subDomain eg. http://es.mydomain.com (Preferred for SEO)
* 4. e_MENU Query eg. /index.php?[es]
* 5. $_GET['elan'] eg. /index.php?elan=es
* 6. $_POST['sitelanguage'] eg. <input type='hidden' name='sitelanguage' value='Spanish' />
* 7. $GLOBALS['elan'] eg. <?php $GLOBALS['elan']='es' (deprecated)
*
* @param boolean $force force detection, don't use cached value
*/
function detect($force = false)
{
global $pref;
if(false !== $this->detect && !$force) return $this->detect;
$this->_cookie_domain = '';
if(defined('e_PAGE_LANGUAGE') && ($detect_language = $this->isValid(e_PAGE_LANGUAGE))) // page specific override.
{
// Do nothing as $detect_language is set.
}
elseif(vartrue($pref['multilanguage_subdomain']) && $this->isLangDomain(e_DOMAIN) && (defset('MULTILANG_SUBDOMAIN') !== false))
{
$detect_language = (e_SUBDOMAIN) ? $this->isValid(e_SUBDOMAIN) : $pref['sitelanguage'];
// Done in session handler now, based on MULTILANG_SUBDOMAIN value
//e107_ini_set("session.cookie_domain", ".".e_DOMAIN); // Must be before session_start()
$this->_cookie_domain = ".".e_DOMAIN;
define('MULTILANG_SUBDOMAIN',true);
}
elseif(!empty($pref['multilanguage_domain']) && ($newLang = $this->isLangDomain(e_DOMAIN)))
{
$detect_language = $this->isValid($newLang);
$this->_cookie_domain = ".".e_DOMAIN;
}
elseif(e_MENU && ($detect_language = $this->isValid(e_MENU))) //
{
define("e_LANCODE",true);
}
elseif(isset($_GET['elan']) && ($detect_language = $this->isValid($_GET['elan']))) // eg: /index.php?elan=Spanish
{
// Do nothing
}
elseif(isset($_POST['setlanguage']) && ($detect_language = $this->isValid($_POST['sitelanguage'])))
{
// Do nothing
}
elseif(isset($GLOBALS['elan']) && ($detect_language = $this->isValid($GLOBALS['elan'])))
{
// Do nothing
}
else
{
$detect_language = false; // ie. No Change.
}
// Done in session handler now
// e107_ini_set("session.cookie_path", e_HTTP);
$this->detect = $detect_language;
return $detect_language;
}
/**
* Get domain to be used in cookeis (e.g. .domain.com), or empty
* if multi-language subdomain settings not enabled
* Available after self::detect()
* @return string
*/
public function getCookieDomain()
{
return $this->_cookie_domain;
}
/**
* Set the Language (Constants, $_SESSION and $_COOKIE) for the current page.
* @param string $language force set
* @return void
*/
function set($language = null)
{
$pref = e107::getPref();
$session = e107::getSession(); // default core session namespace
if($language && ($language = $this->isValid($language))) // force set
{
$this->detect = $language;
}
if($this->detect) // Language-Change Trigger Detected.
{
// new - e_language moved to e107 namespace - $_SESSION['e107']['e_language']
$oldlan = $session->get('e_language');
if(!$session->has('e_language') || (($session->get('e_language') != $this->detect) && $this->isValid($this->detect)))
{
$session->set('e_language', $this->detect);
}
if(varset($_COOKIE['e107_language'])!=$this->detect && (defset('MULTILANG_SUBDOMAIN') != TRUE))
{
setcookie('e107_language', $this->detect, time() + 86400, e_HTTP);
$_COOKIE['e107_language'] = $this->detect; // Used only when a user returns to the site. Not used during this session.
}
else // Multi-lang SubDomains should ignore cookies and remove old ones if they exist.
{
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
$user_language = $this->detect;
// new system trigger 'lanset'
if($oldlan && $oldlan !== $this->detect)
{
e107::getEvent()->trigger('lanset', array('new' => $this->detect, 'old' => $oldlan));
}
}
else // No Language-change Trigger Detected.
{
if($session->has('e_language'))
{
$user_language = $session->get('e_language');
}
elseif(isset($_COOKIE['e107_language']) && ($user_language = $this->isValid($_COOKIE['e107_language'])))
{
$session->set('e_language', $user_language);
}
else
{
$user_language = $pref['sitelanguage'];
if($session->is('e_language'))
{
$session->clear('e_language');
}
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
}
$this->e_language = $user_language;
$this->setDefs();
return;
}
/**
* Set Language-specific Constants
* FIXME - language detection is a mess - db handler, mysql handler, session handler and language handler + constants invlolved,
* SIMPLIFY, test, get feedback
* @param string $language
* @return
*/
function setDefs()
{
global $pref;
$language = $this->e_language;
//$session = e107::getSession();
// SecretR - don't register lanlist in session, confusions, save it as class property (lan class is singleton)
e107::getSession()->set('language-list', null); // cleanup test installs, will be removed soon
/*if(!$session->is('language-list'))
{
$session->set('language-list', implode(',',$this->installed()));
}*/
//define('e_LANLIST', $session->get('language-list'));
define('e_LANLIST', implode(',', $this->installed()));
define('e_LANGUAGE', $language);
define('USERLAN', $language); // Keep USERLAN for backward compatibility
$iso = $this->convert($language);
define("e_LAN", $iso);
// Below is for BC
if(defined('e_LANCODE') && varset($pref['multilanguage']) && ($language != $pref['sitelanguage']))
{
define("e_LANQRY", "[".$iso."]");
}
else
{
define("e_LANCODE", '');
define("e_LANQRY", false);
}
}
public function getLanSelectArray($force = false)
{
if($force ||null === $this->_select_array)
{
$lanlist = explode(',', e_LANLIST);
$this->_select_array = array();
foreach ($lanlist as $lan)
{
$this->_select_array[$this->convert($lan)] = $this->toNative($lan);
}
}
return $this->_select_array;
}
/**
* Return an array of all language types.
*/
public function getList()
{
return $this->list;
}
/**
* Define Legacy LAN constants based on a supplied array.
* @param null $bcList
*/
public function bcDefs($bcList = null)
{
if(empty($bcList))
{
$bcList = array(
'LAN_180' => 'LAN_SEARCH'
);
}
foreach($bcList as $old => $new)
{
if(!defined($old) && defined($new))
{
define($old, constant($new));
}
}
}
}
15 Lapkričio 2016 - 20:45 / #7
-
Oho kiek dėmesio skirta kalbos pasirinkimui. Pataisiau, dabar turėtų veikti.
<?php
class language{
// http://www.loc.gov/standards/iso639-2/php/code_list.php
// Valid Language Pack Names are shown directly below on the right.
public $detect = false;
public $e_language = 'English'; // replaced later with $pref
public $_cookie_domain = '';
/**
* Cached list of Installed Language Packs
* @var array
*/
protected $lanlist = null; // null is important!!!
protected $list = array(
"na" => "Nauru",
"nv" => "Navajo",
"zu" => "Zulu"
);
protected $names = array(
"Vietnamese" => "Tiếng Việt",
"Welsh" => "Cymraeg"
);
/**
* Converts iso to language-name and visa-versa.
* @param object $data
* @return
*/
function convert($data){
if(strlen($data) > 2)
{
$tmp = array_flip($this->list);
return isset($tmp[$data]) ? $tmp[$data] : false;
}
else
{
return (isset($this->list[$data])) ? $this->list[$data] : false;
}
}
// -------------------------------------------------------------------
/**
* Check if a Language is installed and valid
* @param object $lang - Language to check. eg. 'es' or 'Spanish'
* @return false or the name of the valid Language
*/
function isValid($lang='')
{
if(empty($lang))
{
return false;
}
global $pref;
if(!$lang)
{
return (ADMIN_AREA && vartrue($pref['adminlanguage'])) ? $pref['adminlanguage'] : $pref['sitelanguage'];
}
if(strpos($lang,"debug")!==false)
{
return false;
}
if($lang == 'E_SITELANGUAGE') // allows for overriding language using a scripted 'define' before class2.php is loaded.
{
$lang = $pref['sitelanguage'];
}
if($lang == 'E_ADMINLANGUAGE')
{
$lang = $pref['adminlanguage'];
}
if(strlen($lang)== 2)
{
$iso = $lang;
$lang = $this->convert($lang);
}
else
{
$iso = $this->convert($lang);
}
if($iso==false || $lang==false)
{
$diz = ($lang) ? $lang : $iso;
trigger_error("The selected language (".$diz.") is invalid. See e107_handlers/language_class.php for a list of valid languages. ", E_USER_ERROR);
return false;
}
if(is_readable(e_LANGUAGEDIR.$lang.'/'.$lang.'.php'))
{
return $lang;
}
else
{
trigger_error("The selected language (".$lang.") was not found.", E_USER_ERROR);
return false;
}
}
/**
* Check if the specified domain has multi-language subdomains enabled.
* @return
*/
function isLangDomain($domain='')
{
if(!$domain)
{
return false;
}
global $pref;
$mtmp = explode("\n", $pref['multilanguage_subdomain']);
foreach($mtmp as $val)
{
if($domain == trim($val))
{
return true;
}
}
if(!empty($pref['multilanguage_domain']) && is_array($pref['multilanguage_domain']))
{
foreach($pref['multilanguage_domain'] as $lng=>$val)
{
if($domain == trim($val))
{
return $lng;
}
}
}
return false;
}
/**
* Generic variable translator for LAN definitions.
* @example $lng->translate("My name is [x] and I own a [y]", array('x'=>"John", 'y'=>"Cat"));
* @deprecated Use $tp->lanVars() instead.
*/
function translate($lan, $array= array())
{
foreach($array as $k=>$v)
{
$search[] = "[".$k."]";
$replace[] = "<b>".$v."</b>";
}
return str_replace($search, $replace, $lan);
}
/**
* Return a list of Installed Language Packs
* @param str $type - English or Native.
* @example type = english: array(0=>'English', 1=>'French' ...)
* @example type = native: array('English'=>'English', 'French'=>'Francais'...)
* @return array
*/
function installed($type='english')
{
if(null == $this->lanlist)
{
$fl = e107::getFile();
$dirArray = $fl->get_dirs(e_LANGUAGEDIR);
// $handle = opendir(e_LANGUAGEDIR);
$lanlist = array();
// while ($file = readdir($handle))
foreach($dirArray as $file)
{
if ($file != '.' && $file != '..' && is_readable(e_LANGUAGEDIR.$file.'/'.$file.'.php'))
{
$lanlist[] = $file;
}
}
// closedir($handle);
$this->lanlist = array_intersect($lanlist,$this->list);
}
if($type == 'native')
{
$natList = array();
foreach($this->lanlist as $lang)
{
$natList[$lang] = $this->toNative($lang);
}
natsort($natList);
return $natList;
}
return $this->lanlist;
}
/**
* Convert a Language to its Native title. eg. 'Spanish' becomes 'Español'
* @param string $lang
* @return string
*/
function toNative($lang)
{
return (!empty($this->names[$lang])) ? $this->names[$lang] : $lang;
}
/**
* Convert the current URL to a multi-lang for the specified language.
* eg. 'http://www.mydomain.com' becomes 'http://es.mydomain.com'
* @param string $language eg. 'Spanish'
* @return URL
*/
function subdomainUrl($language, $url=e_REQUEST_URL)
{
global $pref;
$iso = (strlen($language) == 2) ? $language : $this->convert($language);
$codelnk = ($language == $pref['sitelanguage']) ? "www" : $iso;
if($codelnk == '')
{
$codelnk = 'www';
}
// $urlval = str_replace($_SERVER['HTTP_HOST'],$codelnk.".".e_DOMAIN,e_SELF);
/* $urlval = (e_QUERY)
? str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF).'?'.e_QUERY
: str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF);
*/
$domain = deftrue('e_DOMAIN','example.com');
$urlval = str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.$domain, $url) ;
return (string) $urlval;
}
/**
* Detect a Language Change
* 1. Scripted Definition eg. define('e_PAGE_LANGUAGE', 'English');
* 2. Parked Domain eg. http://mylanguagedomain.com
* 3. Parked subDomain eg. http://es.mydomain.com (Preferred for SEO)
* 4. e_MENU Query eg. /index.php?[es]
* 5. $_GET['elan'] eg. /index.php?elan=es
* 6. $_POST['sitelanguage'] eg. <input type='hidden' name='sitelanguage' value='Spanish' />
* 7. $GLOBALS['elan'] eg. <?php $GLOBALS['elan']='es' (deprecated)
*
* @param boolean $force force detection, don't use cached value
*/
function detect($force = false)
{
global $pref;
if(false !== $this->detect && !$force) return $this->detect;
$this->_cookie_domain = '';
if(defined('e_PAGE_LANGUAGE') && ($detect_language = $this->isValid(e_PAGE_LANGUAGE))) // page specific override.
{
// Do nothing as $detect_language is set.
}
elseif(vartrue($pref['multilanguage_subdomain']) && $this->isLangDomain(e_DOMAIN) && (defset('MULTILANG_SUBDOMAIN') !== false))
{
$detect_language = (e_SUBDOMAIN) ? $this->isValid(e_SUBDOMAIN) : $pref['sitelanguage'];
// Done in session handler now, based on MULTILANG_SUBDOMAIN value
//e107_ini_set("session.cookie_domain", ".".e_DOMAIN); // Must be before session_start()
$this->_cookie_domain = ".".e_DOMAIN;
define('MULTILANG_SUBDOMAIN',true);
}
elseif(!empty($pref['multilanguage_domain']) && ($newLang = $this->isLangDomain(e_DOMAIN)))
{
$detect_language = $this->isValid($newLang);
$this->_cookie_domain = ".".e_DOMAIN;
}
elseif(e_MENU && ($detect_language = $this->isValid(e_MENU))) //
{
define("e_LANCODE",true);
}
elseif(isset($_GET['elan']) && ($detect_language = $this->isValid($_GET['elan']))) // eg: /index.php?elan=Spanish
{
// Do nothing
}
elseif(isset($_POST['sitelanguage']) && ($detect_language = $this->isValid($_POST['sitelanguage'])))
{
$detect_language = (e_SUBDOMAIN) ? $this->isValid(e_SUBDOMAIN) : $pref['sitelanguage'];
// Done in session handler now, based on MULTILANG_SUBDOMAIN value
//e107_ini_set("session.cookie_domain", ".".e_DOMAIN); // Must be before session_start()
$this->_cookie_domain = ".".e_DOMAIN;
define('MULTILANG_SUBDOMAIN',true);
}
elseif(isset($GLOBALS['elan']) && ($detect_language = $this->isValid($GLOBALS['elan'])))
{
// Do nothing
}
else
{
$detect_language = false; // ie. No Change.
}
// Done in session handler now
// e107_ini_set("session.cookie_path", e_HTTP);
$this->detect = $detect_language;
return $detect_language;
}
/**
* Get domain to be used in cookeis (e.g. .domain.com), or empty
* if multi-language subdomain settings not enabled
* Available after self::detect()
* @return string
*/
public function getCookieDomain()
{
return $this->_cookie_domain;
}
/**
* Set the Language (Constants, $_SESSION and $_COOKIE) for the current page.
* @param string $language force set
* @return void
*/
function set($language = null)
{
$pref = e107::getPref();
$session = e107::getSession(); // default core session namespace
if($language && ($language = $this->isValid($language))) // force set
{
$this->detect = $language;
}
if($this->detect) // Language-Change Trigger Detected.
{
// new - e_language moved to e107 namespace - $_SESSION['e107']['e_language']
$oldlan = $session->get('e_language');
if(!$session->has('e_language') || (($session->get('e_language') != $this->detect) && $this->isValid($this->detect)))
{
$session->set('e_language', $this->detect);
}
if(varset($_COOKIE['e107_language'])!=$this->detect && (defset('MULTILANG_SUBDOMAIN') != TRUE))
{
setcookie('e107_language', $this->detect, time() + 86400, e_HTTP);
$_COOKIE['e107_language'] = $this->detect; // Used only when a user returns to the site. Not used during this session.
}
else // Multi-lang SubDomains should ignore cookies and remove old ones if they exist.
{
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
$user_language = $this->detect;
// new system trigger 'lanset'
if($oldlan && $oldlan !== $this->detect)
{
e107::getEvent()->trigger('lanset', array('new' => $this->detect, 'old' => $oldlan));
}
}
else // No Language-change Trigger Detected.
{
if($session->has('e_language'))
{
$user_language = $session->get('e_language');
}
elseif(isset($_COOKIE['e107_language']) && ($user_language = $this->isValid($_COOKIE['e107_language'])))
{
$session->set('e_language', $user_language);
}
else
{
$user_language = $pref['sitelanguage'];
if($session->is('e_language'))
{
$session->clear('e_language');
}
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
}
$this->e_language = $user_language;
$this->setDefs();
return;
}
/**
* Set Language-specific Constants
* FIXME - language detection is a mess - db handler, mysql handler, session handler and language handler + constants invlolved,
* SIMPLIFY, test, get feedback
* @param string $language
* @return
*/
function setDefs()
{
global $pref;
$language = $this->e_language;
//$session = e107::getSession();
// SecretR - don't register lanlist in session, confusions, save it as class property (lan class is singleton)
e107::getSession()->set('language-list', null); // cleanup test installs, will be removed soon
/*if(!$session->is('language-list'))
{
$session->set('language-list', implode(',',$this->installed()));
}*/
//define('e_LANLIST', $session->get('language-list'));
define('e_LANLIST', implode(',', $this->installed()));
define('e_LANGUAGE', $language);
define('USERLAN', $language); // Keep USERLAN for backward compatibility
$iso = $this->convert($language);
define("e_LAN", $iso);
// Below is for BC
if(defined('e_LANCODE') && varset($pref['multilanguage']) && ($language != $pref['sitelanguage']))
{
define("e_LANQRY", "[".$iso."]");
}
else
{
define("e_LANCODE", '');
define("e_LANQRY", false);
}
}
public function getLanSelectArray($force = false)
{
if($force ||null === $this->_select_array)
{
$lanlist = explode(',', e_LANLIST);
$this->_select_array = array();
foreach ($lanlist as $lan)
{
$this->_select_array[$this->convert($lan)] = $this->toNative($lan);
}
}
return $this->_select_array;
}
/**
* Return an array of all language types.
*/
public function getList()
{
return $this->list;
}
/**
* Define Legacy LAN constants based on a supplied array.
* @param null $bcList
*/
public function bcDefs($bcList = null)
{
if(empty($bcList))
{
$bcList = array(
'LAN_180' => 'LAN_SEARCH'
);
}
foreach($bcList as $old => $new)
{
if(!defined($old) && defined($new))
{
define($old, constant($new));
}
}
}
}
?>
16 Lapkričio 2016 - 08:46 / #8
-
Skubėjimo klaida. Šis veiks. <?php
class language{
// http://www.loc.gov/standards/iso639-2/php/code_list.php
// Valid Language Pack Names are shown directly below on the right.
public $detect = false;
public $e_language = 'English'; // replaced later with $pref
public $_cookie_domain = '';
/**
* Cached list of Installed Language Packs
* @var array
*/
protected $lanlist = null; // null is important!!!
protected $list = array(
"na" => "Nauru",
"nv" => "Navajo",
"zu" => "Zulu"
);
protected $names = array(
"Vietnamese" => "Tiếng Việt",
"Welsh" => "Cymraeg"
);
/**
* Converts iso to language-name and visa-versa.
* @param object $data
* @return
*/
function convert($data){
if(strlen($data) > 2)
{
$tmp = array_flip($this->list);
return isset($tmp[$data]) ? $tmp[$data] : false;
}
else
{
return (isset($this->list[$data])) ? $this->list[$data] : false;
}
}
// -------------------------------------------------------------------
/**
* Check if a Language is installed and valid
* @param object $lang - Language to check. eg. 'es' or 'Spanish'
* @return false or the name of the valid Language
*/
function isValid($lang='')
{
if(empty($lang))
{
return false;
}
global $pref;
if(!$lang)
{
return (ADMIN_AREA && vartrue($pref['adminlanguage'])) ? $pref['adminlanguage'] : $pref['sitelanguage'];
}
if(strpos($lang,"debug")!==false)
{
return false;
}
if($lang == 'E_SITELANGUAGE') // allows for overriding language using a scripted 'define' before class2.php is loaded.
{
$lang = $pref['sitelanguage'];
}
if($lang == 'E_ADMINLANGUAGE')
{
$lang = $pref['adminlanguage'];
}
if(strlen($lang)== 2)
{
$iso = $lang;
$lang = $this->convert($lang);
}
else
{
$iso = $this->convert($lang);
}
if($iso==false || $lang==false)
{
$diz = ($lang) ? $lang : $iso;
trigger_error("The selected language (".$diz.") is invalid. See e107_handlers/language_class.php for a list of valid languages. ", E_USER_ERROR);
return false;
}
if(is_readable(e_LANGUAGEDIR.$lang.'/'.$lang.'.php'))
{
return $lang;
}
else
{
trigger_error("The selected language (".$lang.") was not found.", E_USER_ERROR);
return false;
}
}
/**
* Check if the specified domain has multi-language subdomains enabled.
* @return
*/
function isLangDomain($domain='')
{
if(!$domain)
{
return false;
}
global $pref;
$mtmp = explode("\n", $pref['multilanguage_subdomain']);
foreach($mtmp as $val)
{
if($domain == trim($val))
{
return true;
}
}
if(!empty($pref['multilanguage_domain']) && is_array($pref['multilanguage_domain']))
{
foreach($pref['multilanguage_domain'] as $lng=>$val)
{
if($domain == trim($val))
{
return $lng;
}
}
}
return false;
}
/**
* Generic variable translator for LAN definitions.
* @example $lng->translate("My name is [x] and I own a [y]", array('x'=>"John", 'y'=>"Cat"));
* @deprecated Use $tp->lanVars() instead.
*/
function translate($lan, $array= array())
{
foreach($array as $k=>$v)
{
$search[] = "[".$k."]";
$replace[] = "<b>".$v."</b>";
}
return str_replace($search, $replace, $lan);
}
/**
* Return a list of Installed Language Packs
* @param str $type - English or Native.
* @example type = english: array(0=>'English', 1=>'French' ...)
* @example type = native: array('English'=>'English', 'French'=>'Francais'...)
* @return array
*/
function installed($type='english')
{
if(null == $this->lanlist)
{
$fl = e107::getFile();
$dirArray = $fl->get_dirs(e_LANGUAGEDIR);
// $handle = opendir(e_LANGUAGEDIR);
$lanlist = array();
// while ($file = readdir($handle))
foreach($dirArray as $file)
{
if ($file != '.' && $file != '..' && is_readable(e_LANGUAGEDIR.$file.'/'.$file.'.php'))
{
$lanlist[] = $file;
}
}
// closedir($handle);
$this->lanlist = array_intersect($lanlist,$this->list);
}
if($type == 'native')
{
$natList = array();
foreach($this->lanlist as $lang)
{
$natList[$lang] = $this->toNative($lang);
}
natsort($natList);
return $natList;
}
return $this->lanlist;
}
/**
* Convert a Language to its Native title. eg. 'Spanish' becomes 'Español'
* @param string $lang
* @return string
*/
function toNative($lang)
{
return (!empty($this->names[$lang])) ? $this->names[$lang] : $lang;
}
/**
* Convert the current URL to a multi-lang for the specified language.
* eg. 'http://www.mydomain.com' becomes 'http://es.mydomain.com'
* @param string $language eg. 'Spanish'
* @return URL
*/
function subdomainUrl($language, $url=e_REQUEST_URL)
{
global $pref;
$iso = (strlen($language) == 2) ? $language : $this->convert($language);
$codelnk = ($language == $pref['sitelanguage']) ? "www" : $iso;
if($codelnk == '')
{
$codelnk = 'www';
}
// $urlval = str_replace($_SERVER['HTTP_HOST'],$codelnk.".".e_DOMAIN,e_SELF);
/* $urlval = (e_QUERY)
? str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF).'?'.e_QUERY
: str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.e_DOMAIN, e_SELF);
*/
$domain = deftrue('e_DOMAIN','example.com');
$urlval = str_replace($_SERVER['HTTP_HOST'], $codelnk.'.'.$domain, $url) ;
return (string) $urlval;
}
/**
* Detect a Language Change
* 1. Scripted Definition eg. define('e_PAGE_LANGUAGE', 'English');
* 2. Parked Domain eg. http://mylanguagedomain.com
* 3. Parked subDomain eg. http://es.mydomain.com (Preferred for SEO)
* 4. e_MENU Query eg. /index.php?[es]
* 5. $_GET['elan'] eg. /index.php?elan=es
* 6. $_POST['sitelanguage'] eg. <input type='hidden' name='sitelanguage' value='Spanish' />
* 7. $GLOBALS['elan'] eg. <?php $GLOBALS['elan']='es' (deprecated)
*
* @param boolean $force force detection, don't use cached value
*/
function detect($force = false)
{
global $pref;
if(false !== $this->detect && !$force) return $this->detect;
$this->_cookie_domain = '';
if(defined('e_PAGE_LANGUAGE') && ($detect_language = $this->isValid(e_PAGE_LANGUAGE))) // page specific override.
{
// Do nothing as $detect_language is set.
}
elseif(vartrue($pref['multilanguage_subdomain']) && $this->isLangDomain(e_DOMAIN) && (defset('MULTILANG_SUBDOMAIN') !== false))
{
$detect_language = (e_SUBDOMAIN) ? $this->isValid(e_SUBDOMAIN) : $pref['sitelanguage'];
// Done in session handler now, based on MULTILANG_SUBDOMAIN value
//e107_ini_set("session.cookie_domain", ".".e_DOMAIN); // Must be before session_start()
$this->_cookie_domain = ".".e_DOMAIN;
define('MULTILANG_SUBDOMAIN',true);
}
elseif(!empty($pref['multilanguage_domain']) && ($newLang = $this->isLangDomain(e_DOMAIN)))
{
$detect_language = $this->isValid($newLang);
$this->_cookie_domain = ".".e_DOMAIN;
}
elseif(e_MENU && ($detect_language = $this->isValid(e_MENU))) //
{
define("e_LANCODE",true);
}
elseif(isset($_GET['elan']) && ($detect_language = $this->isValid($_GET['elan']))) // eg: /index.php?elan=Spanish
{
// Do nothing
}
elseif(isset($_POST['sitelanguage']) && ($detect_language = $this->isValid($_POST['sitelanguage'])))
{
$detect_language = $_POST['sitelanguage'];
$this->_cookie_domain = ".".e_DOMAIN;
}
elseif(isset($GLOBALS['elan']) && ($detect_language = $this->isValid($GLOBALS['elan'])))
{
// Do nothing
}
else
{
$detect_language = false; // ie. No Change.
}
// Done in session handler now
// e107_ini_set("session.cookie_path", e_HTTP);
$this->detect = $detect_language;
return $detect_language;
}
/**
* Get domain to be used in cookeis (e.g. .domain.com), or empty
* if multi-language subdomain settings not enabled
* Available after self::detect()
* @return string
*/
public function getCookieDomain()
{
return $this->_cookie_domain;
}
/**
* Set the Language (Constants, $_SESSION and $_COOKIE) for the current page.
* @param string $language force set
* @return void
*/
function set($language = null)
{
$pref = e107::getPref();
$session = e107::getSession(); // default core session namespace
if($language && ($language = $this->isValid($language))) // force set
{
$this->detect = $language;
}
if($this->detect) // Language-Change Trigger Detected.
{
// new - e_language moved to e107 namespace - $_SESSION['e107']['e_language']
$oldlan = $session->get('e_language');
if(!$session->has('e_language') || (($session->get('e_language') != $this->detect) && $this->isValid($this->detect)))
{
$session->set('e_language', $this->detect);
}
if(varset($_COOKIE['e107_language'])!=$this->detect && (defset('MULTILANG_SUBDOMAIN') != TRUE))
{
setcookie('e107_language', $this->detect, time() + 86400, e_HTTP);
$_COOKIE['e107_language'] = $this->detect; // Used only when a user returns to the site. Not used during this session.
}
else // Multi-lang SubDomains should ignore cookies and remove old ones if they exist.
{
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
$user_language = $this->detect;
// new system trigger 'lanset'
if($oldlan && $oldlan !== $this->detect)
{
e107::getEvent()->trigger('lanset', array('new' => $this->detect, 'old' => $oldlan));
}
}
else // No Language-change Trigger Detected.
{
if($session->has('e_language'))
{
$user_language = $session->get('e_language');
}
elseif(isset($_COOKIE['e107_language']) && ($user_language = $this->isValid($_COOKIE['e107_language'])))
{
$session->set('e_language', $user_language);
}
else
{
$user_language = $pref['sitelanguage'];
if($session->is('e_language'))
{
$session->clear('e_language');
}
if(isset($_COOKIE['e107_language']))
{
unset($_COOKIE['e107_language']);
}
}
}
$this->e_language = $user_language;
$this->setDefs();
return;
}
/**
* Set Language-specific Constants
* FIXME - language detection is a mess - db handler, mysql handler, session handler and language handler + constants invlolved,
* SIMPLIFY, test, get feedback
* @param string $language
* @return
*/
function setDefs()
{
global $pref;
$language = $this->e_language;
//$session = e107::getSession();
// SecretR - don't register lanlist in session, confusions, save it as class property (lan class is singleton)
e107::getSession()->set('language-list', null); // cleanup test installs, will be removed soon
/*if(!$session->is('language-list'))
{
$session->set('language-list', implode(',',$this->installed()));
}*/
//define('e_LANLIST', $session->get('language-list'));
define('e_LANLIST', implode(',', $this->installed()));
define('e_LANGUAGE', $language);
define('USERLAN', $language); // Keep USERLAN for backward compatibility
$iso = $this->convert($language);
define("e_LAN", $iso);
// Below is for BC
if(defined('e_LANCODE') && varset($pref['multilanguage']) && ($language != $pref['sitelanguage']))
{
define("e_LANQRY", "[".$iso."]");
}
else
{
define("e_LANCODE", '');
define("e_LANQRY", false);
}
}
public function getLanSelectArray($force = false)
{
if($force ||null === $this->_select_array)
{
$lanlist = explode(',', e_LANLIST);
$this->_select_array = array();
foreach ($lanlist as $lan)
{
$this->_select_array[$this->convert($lan)] = $this->toNative($lan);
}
}
return $this->_select_array;
}
/**
* Return an array of all language types.
*/
public function getList()
{
return $this->list;
}
/**
* Define Legacy LAN constants based on a supplied array.
* @param null $bcList
*/
public function bcDefs($bcList = null)
{
if(empty($bcList))
{
$bcList = array(
'LAN_180' => 'LAN_SEARCH'
);
}
foreach($bcList as $old => $new)
{
if(!defined($old) && defined($new))
{
define($old, constant($new));
}
}
}
}
?>
16 Lapkričio 2016 - 10:28 / #9
|