Source for file languagefilter.php
Documentation is available at languagefilter.php
* @subpackage System.languagefilter
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Joomla! Language Filter Plugin
* @subpackage System.languagefilter
protected static $mode_sef;
protected static $lang_codes;
protected static $default_lang;
protected static $default_sef;
protected static $cookie;
private static $_user_lang_code;
// Ensure that constructor is called one time
self::$cookie =
SID ==
'';
if (!self::$default_lang)
$app =
JFactory::getApplication();
$router =
$app->getRouter();
self::$mode_sef =
($router->getMode() ==
JROUTER_MODE_SEF) ?
true :
false;
self::$sefs =
JLanguageHelper::getLanguages('sef');
self::$lang_codes =
JLanguageHelper::getLanguages('lang_code');
self::$default_lang =
JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
self::$default_sef =
self::$lang_codes[self::$default_lang]->sef;
self::$homes =
MultilangstatusHelper::getHomes();
$levels =
$user->getAuthorisedViewLevels();
foreach (self::$sefs as $sef =>
&$language)
if (isset
($language->access) &&
$language->access &&
!in_array($language->access, $levels))
unset
(self::$sefs[$sef]);
$app->setLanguageFilter(true);
// Get the route path from the request.
$path =
JString::substr($uri->toString(), JString::strlen($uri->base()));
// Apache mod_rewrite is Off
// Trim any spaces or slashes from the ends of the path and explode into segments.
// The language segment is always at the beginning of the route path if it exists.
$sef =
$uri->getVar('lang');
if (!empty($parts) &&
empty($sef))
$sef =
$uri->getVar('lang');
if (isset
(self::$sefs[$sef]))
$lang_code =
self::$sefs[$sef]->lang_code;
$conf =
JFactory::getConfig();
$cookie_domain =
$conf->get('config.cookie_domain', '');
$cookie_path =
$conf->get('config.cookie_path', '/');
$app->input->set('language', $lang_code);
// Detect browser feature
$app->setDetectBrowser($this->params->get('detect_browser', '1') ==
'1');
$app->item_associations =
$this->params->get('item_associations', 0);
self::$tag =
JFactory::getLanguage()->getTag();
$router =
$app->getRouter();
// attach build rules for language SEF
$router->attachBuildRule(array($this, 'buildRule'));
// attach parse rules for language SEF
$router->attachParseRule(array($this, 'parseRule'));
// Adding custom site name
if (isset
($languages[self::$tag]) &&
$languages[self::$tag]->sitename)
JFactory::getConfig()->set('sitename', $languages[self::$tag]->sitename);
$sef =
$uri->getVar('lang');
$sef =
self::$lang_codes[self::$tag]->sef;
elseif (!isset
(self::$sefs[$sef]))
$sef =
self::$default_sef;
$Itemid =
$uri->getVar('Itemid');
if ($item->home &&
$uri->getVar('option') !=
'com_search')
if (isset
($parts['query']) &&
strpos($parts['query'], '&'))
$parts['query'] =
str_replace('&', '&', $parts['query']);
// test if the url contains same vars as in menu link
foreach ($uri->getQuery(true) as $key =>
$value)
if (!in_array($key, array('format', 'Itemid', 'lang')) &&
!(isset
($vars[$key]) &&
$vars[$key] ==
$value))
foreach ($vars as $key =>
$value)
$this->params->get('remove_default_prefix', 0) ==
0
||
$sef !=
self::$default_sef
||
$sef !=
self::$lang_codes[self::$tag]->sef
||
$this->params->get('detect_browser', 1) &&
JLanguageHelper::detectLanguage() !=
self::$tag &&
!self::$cookie
$uri->setPath($uri->getPath().
'/'.
$sef.
'/');
$uri->setPath($uri->getPath());
$uri->setVar('lang', $sef);
$app =
JFactory::getApplication();
// No cookie - let's try to detect browser language or use site default
if ($this->params->get('detect_browser', 1))
$lang_code =
self::$default_lang;
$parts =
explode('/', $path);
// Redirect only if not in post
if (!empty($lang_code) &&
($app->input->getMethod() !=
"POST" ||
count($app->input->post) ==
0))
if ($this->params->get('remove_default_prefix', 0) ==
0)
// redirect if sef does not exists
if (!isset
(self::$sefs[$sef]))
// Use the current language sef or the default one
$sef = isset
(self::$lang_codes[$lang_code]) ?
self::$lang_codes[$lang_code]->sef :
self::$default_sef;
$uri->setPath($sef .
'/' .
$path);
if ($app->getCfg('sef_rewrite'))
$app->redirect($uri->base().
$uri->toString(array('path', 'query', 'fragment')));
$path =
$uri->toString(array('path', 'query', 'fragment'));
$app->redirect($uri->base().
'index.php'.
($path ?
('/' .
$path) :
''));
// redirect if sef does not exists and language is not the default one
if (!isset
(self::$sefs[$sef]) &&
$lang_code !=
self::$default_lang)
$sef = isset
(self::$lang_codes[$lang_code]) ?
self::$lang_codes[$lang_code]->sef :
self::$default_sef;
$uri->setPath($sef .
'/' .
$path);
if ($app->getCfg('sef_rewrite'))
$app->redirect($uri->base().
$uri->toString(array('path', 'query', 'fragment')));
$path =
$uri->toString(array('path', 'query', 'fragment'));
$app->redirect($uri->base().
'index.php'.
($path ?
('/' .
$path) :
''));
// redirect if sef is the default one
elseif (isset
(self::$sefs[$sef]) &&
self::$default_lang ==
self::$sefs[$sef]->lang_code &&
(!$this->params->get('detect_browser', 1) ||
JLanguageHelper::detectLanguage() ==
self::$tag ||
self::$cookie)
$uri->setPath(implode('/', $parts));
if ($app->getCfg('sef_rewrite'))
$app->redirect($uri->base().
$uri->toString(array('path', 'query', 'fragment')));
$path =
$uri->toString(array('path', 'query', 'fragment'));
$app->redirect($uri->base().
'index.php'.
($path ?
('/' .
$path) :
''));
$lang_code = isset
(self::$sefs[$sef]) ?
self::$sefs[$sef]->lang_code :
'';
if ($lang_code &&
JLanguage::exists($lang_code))
$uri->setPath(implode('/', $parts));
$sef =
$uri->getVar('lang');
if (!isset
(self::$sefs[$sef]))
$sef = isset
(self::$lang_codes[$lang_code]) ?
self::$lang_codes[$lang_code]->sef :
self::$default_sef;
$uri->setVar('lang', $sef);
if ($app->input->getMethod() !=
"POST" ||
count($app->input->post) ==
0)
$app->redirect(JUri::base(true).
'/index.php?'.
$uri->getQuery());
$array =
array('lang' =>
$sef);
* before store user method
* Method is called before user data is stored in the database
* @param array $user Holds the old user data.
* @param boolean $isnew True if a new user is stored.
* @param array $new Holds the new user data.
if ($this->params->get('automatic_change', '1') ==
'1' &&
key_exists('params', $user))
$registry->loadString($user['params']);
self::$_user_lang_code =
$registry->get('language');
if (empty(self::$_user_lang_code))
self::$_user_lang_code =
self::$default_lang;
* after store user method
* Method is called after user data is stored in the database
* @param array $user Holds the new user data.
* @param boolean $isnew True if a new user is stored.
* @param boolean $success True if user was succesfully stored in the database.
* @param string $msg Message.
if ($this->params->get('automatic_change', '1') ==
'1' &&
key_exists('params', $user) &&
$success)
$registry->loadString($user['params']);
$lang_code =
$registry->get('language');
$lang_code =
self::$default_lang;
$app =
JFactory::getApplication();
if ($lang_code ==
self::$_user_lang_code ||
!isset
(self::$lang_codes[$lang_code]))
$app->setUserState('com_users.edit.profile.redirect', null);
$app->setUserState('com_users.edit.profile.redirect', 'index.php?Itemid='.
$app->getMenu()->getDefault($lang_code)->id.
'&lang='.
self::$lang_codes[$lang_code]->sef);
$conf =
JFactory::getConfig();
$cookie_domain =
$conf->get('config.cookie_domain', '');
$cookie_path =
$conf->get('config.cookie_path', '/');
* This method should handle any login logic and report back to the subject
* @param array $user Holds the user data
* @param array $options Array holding options (remember, autoregister, group)
* @return boolean True on success
if ($app->isSite() &&
$this->params->get('automatic_change', 1))
$active =
$menu->getActive();
$lang_code =
$user['language'];
$lang_code =
self::$default_lang;
if ($lang_code !=
self::$tag)
$conf =
JFactory::getConfig();
$cookie_domain =
$conf->get('config.cookie_domain', '');
$cookie_path =
$conf->get('config.cookie_path', '/');
// Change the language code
// Change the redirect (language have changed)
if (isset
($associations[$lang_code]) &&
$menu->getItem($associations[$lang_code]))
$itemid =
$associations[$lang_code];
$app->setUserState('users.login.form.return', 'index.php?&Itemid='.
$itemid);
$itemid = isset
(self::$homes[$lang_code]) ?
self::$homes[$lang_code]->id :
self::$homes['*']->id;
$app->setUserState('users.login.form.return', 'index.php?&Itemid='.
$itemid);
* This method adds alternate meta tags for associated menu items
$app =
JFactory::getApplication();
$option =
$app->input->get('option');
if ($app->isSite() &&
$this->params->get('alternate_meta') &&
$doc->getType() ==
'html')
$active =
$menu->getActive();
// load menu associations
$active_link =
JRoute::_('index.php?Itemid='.
$active->id, false);
$active_link =
JRoute::_($active->link.
'&Itemid='.
$active->id, false);
if ($active_link ==
JUri::base(true).
'/')
$active_link .=
'index.php';
if ($current_link ==
JUri::base(true).
'/')
$current_link .=
'index.php';
// Check the exact menu item's URL
if ($active_link ==
$current_link)
unset
($associations[$active->language]);
// load component associations
// No cookie - let's try to detect browser language or use site default
if ($this->params->get('detect_browser', 1)){
$lang_code =
self::$default_lang;
unset
($cassociations[$lang_code]);
// handle the default associations
if ((!empty($associations) ||
!empty($cassociations)) &&
$this->params->get('item_associations'))
foreach (JLanguageHelper::getLanguages() as $language)
if (isset
($cassociations[$language->lang_code]))
$link =
JRoute::_($cassociations[$language->lang_code].
'&lang='.
$language->sef);
$doc->addHeadLink($server .
$link, 'alternate', 'rel', array('hreflang' =>
$language->lang_code));
elseif (isset
($associations[$language->lang_code]))
$item =
$menu->getItem($associations[$language->lang_code]);
$link =
JRoute::_('index.php?Itemid='.
$item->id.
'&lang='.
$language->sef);
$link =
JRoute::_($item->link.
'&Itemid='.
$item->id.
'&lang='.
$language->sef);
$doc->addHeadLink($server .
$link, 'alternate', 'rel', array('hreflang' =>
$language->lang_code));
// link to the home page of each language
elseif ($active &&
$active->home)
$item =
$menu->getDefault($language->lang_code);
if ($item &&
$item->language !=
$active->language &&
$item->language !=
'*')
$link =
JRoute::_('index.php?Itemid='.
$item->id.
'&lang='.
$language->sef);
$link =
JRoute::_($item->link.
'&Itemid='.
$item->id.
'&lang='.
$language->sef);
$doc->addHeadLink($server .
$link, 'alternate', 'rel', array('hreflang' =>
$language->lang_code));
* Getting the Language Cookie settings
* @return string The cookie time.
private function getLangCookieTime()
if ($this->params->get('lang_cookie', 1) ==
1)
$lang_cookie =
time() +
365 *
86400;
Documentation generated on Tue, 19 Nov 2013 15:06:36 +0100 by phpDocumentor 1.4.3