Ignore:
Timestamp:
Jan 14, 2016, 10:25:47 PM (8 years ago)
Author:
chronos
Message:
  • Added: Support for translatable URLs. Now each language can have URL in its language.

This requires to all links inside web to use System->Link function which is responsible for translation.
All links need to be written in english so they can be translated to other language.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/system.php

    r842 r843  
    99  var $OnPageNotFound;
    1010  var $Pages;
     11  /* @var LocaleManager */
     12  var $LocaleManager;
    1113
    1214  function __construct()
     
    2325  function Init()
    2426  {
    25     global $GlobalLocaleManager;
     27    global $GlobalLocaleManager, $Config;
     28
     29    $this->Config = $Config;
     30    date_default_timezone_set($this->Config['Web']['Timezone']);
    2631
    2732    $this->Database->Connect($this->Config['Database']['Host'],
     
    5560      }
    5661    }
    57     $this->LocaleManager->LoadLocale($this->LocaleManager->LangCode);
     62    if(array_key_exists($this->LocaleManager->LangCode, $this->LocaleManager->Available))
     63      $this->LocaleManager->LoadLocale($this->LocaleManager->LangCode);
    5864
    5965    $this->Menu = array
     
    9399  function Run()
    94100  {
    95     global $ScriptStartTime, $TranslationTree, $StopAfterUpdateManager, $Config,
     101    global $ScriptStartTime, $TranslationTree, $StopAfterUpdateManager,
    96102      $UpdateManager, $DatabaseRevision;
    97103
    98     $this->Config = $Config;
    99104    $ScriptStartTime = GetMicrotime();
    100 
    101105    if(GetRemoteAddress() != '') session_start();
    102106
    103107    if(!isset($this->Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
    104     date_default_timezone_set($this->Config['Web']['Timezone']);
    105108
    106109    $this->Init();
     
    181184  }
    182185
     186  function TranslateURL($URL, $Locale)
     187  {
     188    // Try translate URL directory parts from current locale to target locale
     189    $Remaining = $URL;
     190    $RemainingParts = explode('?', $Remaining);
     191    $Directory = $RemainingParts[0];
     192    if(count($RemainingParts) > 1)
     193    {
     194      $Params = $RemainingParts[1];
     195    } else {
     196      $Params = '';
     197    }
     198    $TargetLocaleManager = new LocaleManager($this);
     199    $TargetLocaleManager->Dir = $this->LocaleManager->Dir;
     200    $TargetLocaleManager->Available = $this->LocaleManager->Available;
     201    $TargetLocaleManager->LoadLocale($Locale);
     202
     203    $DirectoryParts = explode('/', $Directory);
     204    foreach($DirectoryParts as $Index => $Item)
     205    {
     206      $NewText = $TargetLocaleManager->CurrentLocale->Texts->Translate($Item, 'URL');
     207      $DirectoryParts[$Index] = $NewText;
     208    }
     209    $Directory = implode('/', $DirectoryParts);
     210    $Remaining = $Directory;
     211    if($Params != '') $Remaining .= '?'.$Params;
     212
     213    return($Remaining);
     214  }
     215
     216  function TranslateReverseURL($URL, $Locale)
     217  {
     218    // Try translate URL directory parts from current locale to target locale
     219    $Remaining = $URL;
     220    $RemainingParts = explode('?', $Remaining);
     221    $Directory = $RemainingParts[0];
     222    if(count($RemainingParts) > 1)
     223    {
     224      $Params = $RemainingParts[1];
     225    } else {
     226      $Params = '';
     227    }
     228    $TargetLocaleManager = new LocaleManager($this);
     229    $TargetLocaleManager->Dir = $this->LocaleManager->Dir;
     230    $TargetLocaleManager->Available = $this->LocaleManager->Available;
     231    $TargetLocaleManager->LoadLocale($Locale);
     232
     233    $DirectoryParts = explode('/', $Directory);
     234    foreach($DirectoryParts as $Index => $Item)
     235    {
     236      $NewText = $TargetLocaleManager->CurrentLocale->Texts->TranslateReverse($Item, 'URL');
     237      $DirectoryParts[$Index] = $NewText;
     238    }
     239    $Directory = implode('/', $DirectoryParts);
     240    $Remaining = $Directory;
     241    if($Params != '') $Remaining .= '?'.$Params;
     242
     243    return($Remaining);
     244  }
     245
    183246  function LinkLocale($Target, $Locale = '')
    184247  {
    185248    if($Locale == '') $Locale = $this->LocaleManager->LangCode;
     249
     250    $Target = $this->TranslateURL($Target, $Locale);
     251
    186252    if($Locale == $this->LocaleManager->DefaultLangCode)
    187253      return($this->Config['Web']['BaseURL'].$Target);
     
    216282    if(count($PathItems) == 0) $PathItems = array('');
    217283    $PathItem = $PathItems[0];
     284    $PathItem = $this->LocaleManager->CurrentLocale->Texts->TranslateReverse($PathItem, 'URL');
     285
    218286    if(array_key_exists($PathItem, $Pages))
    219287    {
     
    286354      if($Locale['Code'] == $this->System->LocaleManager->CurrentLocale->Texts->Code) $Selected = ' selected="selected"';
    287355        else $Selected = '';
     356      $Remaining = $this->System->TranslateReverseURL($Remaining, $this->System->LocaleManager->LangCode);
     357
    288358      $URL = $this->System->LinkLocale($Remaining, $Locale['Code']);
    289359      $Output .= '<option title="" value="'.$URL.'"'.$Selected.' onchange="this.form.submit()">'.$Locale['Title'].'</option>';
     
    306376      //$Output .= ''<span class="MenuItem">Moje překlady: <a href="">Dokončené</a> <a href="">Rozpracované</a> <a href="">Exporty</a> Tým: <a href="">'.$Team['name'].'</a></span>';
    307377      $Output .= $this->System->User->Name.' <a href="'.$this->System->Link('/?action=logout').'">'.T('Logout').'</a>'.
    308           ' <a href="'.$this->System->Link('/user.php?user='.$this->System->User->Id).'">'.T('My page').'</a>'.
    309           ' <a href="'.$this->System->Link('/Options.php').'">'.T('Options').'</a>'.
     378          ' <a href="'.$this->System->Link('/user/?user='.$this->System->User->Id).'">'.T('My page').'</a>'.
     379          ' <a href="'.$this->System->Link('/options/').'">'.T('Options').'</a>'.
    310380          ' <a title="Vámi přeložené texty" href="'.$this->System->Link('/TranslationList.php?user='.
    311381          $this->System->User->Id.'&amp;group=0&amp;state=2&amp;text=&amp;entry=').'">'.T('Translated').'</a>'.
     
    317387    {
    318388      $Output .= '<a href="'.$this->System->Link('/login/').'">'.T('Login').'</a>&nbsp;'.
    319           '<a href="'.$this->System->Link('/registrace.php').'">'.T('Registration').'</a>';
     389        '<a href="'.$this->System->Link('/registration/').'">'.T('Registration').'</a>';
    320390    }
    321391    $Output .= $this->ShowLocaleSelector();
Note: See TracChangeset for help on using the changeset viewer.