Changeset 841 for trunk/includes


Ignore:
Timestamp:
Jan 12, 2016, 10:33:43 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Improved language switching using language code in URL. This is more search engine friendly as each language have own fixed URL.
Location:
trunk/includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/Locale.php

    r840 r841  
    162162  var $Codes;
    163163  var $Dir;
     164  var $LangCode;
     165  var $DefaultLangCode;
     166  var $Available;
    164167
    165168  function __construct(System $System)
     
    168171    $this->Codes = array('en');
    169172    $this->CurrentLocale = new LocaleFile($System);
     173    $this->LangCode = 'en';
     174    $this->DefaultLangCode = 'en';
     175    $this->Available = array();
    170176  }
    171177
     
    218224  function LoadLocale($Code)
    219225  {
    220     $this->CurrentLocale->Dir = $this->Dir;
    221     $this->CurrentLocale->Load($Code);
     226    if(array_key_exists($Code, $this->Available))
     227    {
     228      $this->CurrentLocale->Dir = $this->Dir;
     229      $this->CurrentLocale->Load($Code);
     230    } else throw new Exception('Unsupported locale code '.$Code);
    222231  }
    223232}
     
    226235function T($Text)
    227236{
    228   global $LocaleManager;
    229 
    230   if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text));
     237  global $GlobalLocaleManager;
     238
     239  if(isset($GlobalLocaleManager)) return($GlobalLocaleManager->CurrentLocale->Texts->Translate($Text));
    231240    else return($Text);
    232241}
  • trunk/includes/Version.php

    r840 r841  
    66// and system will need database update.
    77
    8 $Revision = 840; // Subversion revision
     8$Revision = 841; // Subversion revision
    99$DatabaseRevision = 811; // Database structure revision
    10 $ReleaseTime = '2016-01-11';
     10$ReleaseTime = '2016-01-12';
  • trunk/includes/system.php

    r838 r841  
    2323  function Init()
    2424  {
    25     global $Config, $LocaleManager;
    26 
    27     $this->Config = $Config;
     25    global $GlobalLocaleManager;
     26
    2827    $this->Database->Connect($this->Config['Database']['Host'],
    2928      $this->Config['Database']['User'], $this->Config['Database']['Password'],
     
    3433    $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
    3534
    36     $this->PathItems = ProcessURL();
    37     // Initialize locale
    38     $LocaleManager = new LocaleManager($this);
    39     $LocaleManager->Dir = dirname(dirname(__FILE__)).'/locale';
    40     $LocaleManager->Available = array(
     35    $this->LocaleManager = new LocaleManager($this);
     36    $this->LocaleManager->Dir = dirname(dirname(__FILE__)).'/locale';
     37    $this->LocaleManager->Available = array(
    4138      'cs' => array('Code' => 'cs', 'Title' => 'Česky'),
    4239      'en' => array('Code' => 'en', 'Title' => 'English'),
    4340    );
    44     $Code = $Config['Web']['Locale'];
    45     if (isset($_SESSION))
    46       if(array_key_exists('Locale', $_SESSION)) $Code = $_SESSION['Locale'];
    47     if (isset($_GET))
    48       if(array_key_exists('locale', $_GET)) $Code = $_GET['locale'];
    49 
    50     //echo(phpinfo());
    51     if(!array_key_exists($Code, $LocaleManager->Available)) $Code = 'en';
    52     $_SESSION['Locale'] = $Code;
    53     $LocaleManager->LoadLocale($Code);
     41    $GlobalLocaleManager = $this->LocaleManager;
     42    $this->LinkLocaleExceptions = array('images', 'style', 'tmp');
     43
     44    $this->PathItems = ProcessURL();
     45
     46    // Detect interface locale
     47    $this->LocaleManager->DefaultLangCode = $this->Config['Web']['Locale'];
     48    $this->LocaleManager->LangCode = $this->LocaleManager->DefaultLangCode;
     49    if(count($this->PathItems) > 0)
     50    {
     51      $NewLangCode = $this->PathItems[0];
     52      if(array_key_exists($NewLangCode, $this->LocaleManager->Available)) {
     53        array_shift($this->PathItems);
     54        $this->LocaleManager->LangCode = $NewLangCode;
     55      }
     56    }
     57    $this->LocaleManager->LoadLocale($this->LocaleManager->LangCode);
    5458
    5559    $this->Menu = array
     
    103107  function Run()
    104108  {
    105     global $ScriptStartTime, $TranslationTree, $StopAfterUpdateManager,
    106       $UpdateManager, $Config, $DatabaseRevision;
    107 
     109    global $ScriptStartTime, $TranslationTree, $StopAfterUpdateManager, $Config,
     110      $UpdateManager, $DatabaseRevision;
     111
     112    $this->Config = $Config;
    108113    $ScriptStartTime = GetMicrotime();
    109114
    110115    if(GetRemoteAddress() != '') session_start();
    111116
    112     if(!isset($Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
    113     date_default_timezone_set($Config['Web']['Timezone']);
     117    if(!isset($this->Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
     118    date_default_timezone_set($this->Config['Web']['Timezone']);
    114119
    115120    $this->Init();
     
    136141    // Initialize application modules
    137142    $this->ModuleManager->RegisterModule(new ModuleError($this));
    138     $this->ModuleManager->Modules['Error']->ErrorHandler->ShowError = $Config['Web']['ShowPHPError'];
     143    $this->ModuleManager->Modules['Error']->ErrorHandler->ShowError = $this->Config['Web']['ShowPHPError'];
    139144    $this->ModuleManager->RegisterModule(new ModuleLog($this));
    140145    $this->ModuleManager->RegisterModule(new ModuleUser($this));
     
    177182  function Link($Target)
    178183  {
    179     return($this->Config['Web']['BaseURL'].$Target);
     184    $Remaining = substr($Target, count($this->Config['Web']['BaseURL']));
     185    $TargetParts = explode('/', $Remaining);
     186    if(count($TargetParts) > 0)
     187    {
     188      if(in_array($TargetParts[0], $this->LinkLocaleExceptions))
     189      {
     190        return($this->Config['Web']['BaseURL'].$Target);
     191      }
     192    }
     193    return($this->LinkLocale($Target));
     194  }
     195
     196  function LinkLocale($Target, $Locale = '')
     197  {
     198    if($Locale == '') $Locale = $this->LocaleManager->LangCode;
     199    if($Locale == $this->LocaleManager->DefaultLangCode)
     200      return($this->Config['Web']['BaseURL'].$Target);
     201    return($this->Config['Web']['BaseURL'].'/'.$Locale.$Target);
    180202  }
    181203
     
    237259  {
    238260    $Output = '';
     261
    239262    /* @var $Page Page */
    240263    $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
     
    265288  var $Title;
    266289
     290  function ShowLocaleSelector()
     291  {
     292    //$Output .= ' <form action="?setlocale" method="get">';
     293    $Output = ' <select onchange="window.location=this.value">';
     294    foreach($this->System->LocaleManager->Available as $Locale)
     295    {
     296      $Remaining = substr($_SERVER["REQUEST_URI"], strlen($this->System->Config['Web']['BaseURL']));
     297      if(substr($Remaining, 1, strlen($Locale['Code'].'/')) == $this->System->LocaleManager->LangCode.'/')
     298        $Remaining = substr($Remaining, strlen('/'.$Locale['Code']));
     299      if($Locale['Code'] == $this->System->LocaleManager->CurrentLocale->Texts->Code) $Selected = ' selected="selected"';
     300        else $Selected = '';
     301      $URL = $this->System->LinkLocale($Remaining, $Locale['Code']);
     302      $Output .= '<option title="" value="'.$URL.'"'.$Selected.' onchange="this.form.submit()">'.$Locale['Title'].'</option>';
     303    }
     304    $Output .= '</select><noscript><span><input type="submit" value="Submit"/></span></noscript>';
     305
     306    return($Output);
     307  }
     308
    267309  function ShowTopBar()
    268310  {
    269     global $LocaleManager;
    270 
    271311    $Output = '<div class="Menu">';
    272312    if(!$this->System->User->Licence(LICENCE_USER))
     
    292332          '<a href="'.$this->System->Link('/registrace.php').'">'.T('Registration').'</a>';
    293333    }
    294     //$Output .= ' <form action="?setlocale" method="get">';
    295     $Output .= ' <select onchange="window.location=\'?locale=\'+this.value">';
    296     foreach($LocaleManager->Available as $Locale)
    297     {
    298       if($Locale['Code'] == $LocaleManager->CurrentLocale->Texts->Code) $Selected = ' selected="selected"';
    299         else $Selected = '';
    300       $Output .= '<option title="" value="'.$Locale['Code'].'"'.$Selected.' onchange="this.form.submit()">'.$Locale['Title'].'</option>';
    301     }
    302     $Output .= '</select><noscript><span><input type="submit" value="Submit"/></span></noscript>';
     334    $Output .= $this->ShowLocaleSelector();
    303335    //$Output .= '</form>';
    304336    $Output .= '</span></div>';
Note: See TracChangeset for help on using the changeset viewer.