Ignore:
Timestamp:
Feb 22, 2015, 11:20:50 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Tabs converted to spaces.
  • Modified: Remove spaces from end of lines.
  • Added: Code format script.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/Locale.php

    r815 r816  
    33class LocaleText
    44{
    5         var $Data;
    6         var $Code;
    7         var $Title;
     5  var $Data;
     6  var $Code;
     7  var $Title;
    88
    99  function __construct()
    1010  {
    11         $this->Data = array();
    12         $this->Code = 'en';
    13         $this->Title = 'English';
    14   }
    15 
    16         function Load()
    17         {
    18         }
    19 
    20         function Translate($Text)
    21         {
    22                 if(array_key_exists($Text, $this->Data) and ($this->Data[$Text] != ''))
    23                   return($this->Data[$Text]);
    24                   else return($Text);
    25         }
     11    $this->Data = array();
     12    $this->Code = 'en';
     13    $this->Title = 'English';
     14  }
     15
     16  function Load()
     17  {
     18  }
     19
     20  function Translate($Text)
     21  {
     22    if(array_key_exists($Text, $this->Data) and ($this->Data[$Text] != ''))
     23      return($this->Data[$Text]);
     24      else return($Text);
     25  }
    2626}
    2727
    2828class LocaleFile extends Model
    2929{
    30         var $Texts;
    31         var $Dir;
    32 
    33         function __construct($System)
    34         {
    35                 parent::__construct($System);
    36                 $this->Texts = new LocaleText();
    37         }
    38 
    39         function Load($Language)
    40         {
    41                 $FileName = $this->Dir.'/'.$Language.'.php';
    42                 if(file_exists($FileName)) {
    43                         include_once($FileName);
    44                         $ClassName = 'LocaleText'.$Language;
    45                   $this->Texts = new $ClassName();
    46                 } else throw new Exception('Language file '.$FileName.' not found');
    47                 $this->Texts->Load();
    48         }
    49 
    50         function AnalyzeCode($Path)
    51   {
    52         // Search for php files
     30  var $Texts;
     31  var $Dir;
     32
     33  function __construct($System)
     34  {
     35    parent::__construct($System);
     36    $this->Texts = new LocaleText();
     37  }
     38
     39  function Load($Language)
     40  {
     41    $FileName = $this->Dir.'/'.$Language.'.php';
     42    if(file_exists($FileName)) {
     43      include_once($FileName);
     44      $ClassName = 'LocaleText'.$Language;
     45      $this->Texts = new $ClassName();
     46    } else throw new Exception('Language file '.$FileName.' not found');
     47    $this->Texts->Load();
     48  }
     49
     50  function AnalyzeCode($Path)
     51  {
     52    // Search for php files
    5353    $FileList = scandir($Path);
    5454    foreach($FileList as $FileName)
    5555    {
    56         $FullName = $Path.'/'.$FileName;
    57         if(($FileName == '.') or ($FileName == '..')) ; // Skip virtual items
    58         else if(substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files
    59         else if(is_dir($FullName)) $this->AnalyzeCode($FullName);
     56      $FullName = $Path.'/'.$FileName;
     57      if(($FileName == '.') or ($FileName == '..')) ; // Skip virtual items
     58      else if(substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files
     59      else if(is_dir($FullName)) $this->AnalyzeCode($FullName);
    6060      else if(file_exists($FullName))
    6161      {
    6262        if(substr($FullName, -4) == '.php')
    6363        {
    64                 $Content = file_get_contents($FullName);
    65                 // Search occurence of T() function
    66                 while(strpos($Content, 'T(') !== false)
    67                 {
    68                         $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
    69                         $Content = substr($Content, strpos($Content, 'T(') + 2);
    70                         $Ord = ord($Previous);
    71                         //echo($Ord.',');
    72                         if(!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
    73                         {
    74                                 // Do for non-alpha previous character
    75                           $Original = substr($Content, 0, strpos($Content, ')'));
    76                           $Original2 = '';
    77                           if((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
    78                             $Original2 = substr($Original, 1, -1);
    79                           if((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
    80                             $Original2 = substr($Original, 1, -1);
    81                           if($Original2 != '')
    82                           {
    83                                 if(!array_key_exists($Original2, $this->Texts->Data))
    84                                   $this->Texts->Data[$Original2] = '';
    85                           }
    86                         }
    87                 }
     64          $Content = file_get_contents($FullName);
     65          // Search occurence of T() function
     66          while(strpos($Content, 'T(') !== false)
     67          {
     68            $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
     69            $Content = substr($Content, strpos($Content, 'T(') + 2);
     70            $Ord = ord($Previous);
     71            //echo($Ord.',');
     72            if(!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
     73            {
     74              // Do for non-alpha previous character
     75              $Original = substr($Content, 0, strpos($Content, ')'));
     76              $Original2 = '';
     77              if((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
     78                $Original2 = substr($Original, 1, -1);
     79              if((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
     80                $Original2 = substr($Original, 1, -1);
     81              if($Original2 != '')
     82              {
     83                if(!array_key_exists($Original2, $this->Texts->Data))
     84                  $this->Texts->Data[$Original2] = '';
     85              }
     86            }
     87          }
    8888        }
    8989      }
     
    9393  function SaveToFile($FileName)
    9494  {
    95         $Content = '<?php'."\n".
     95    $Content = '<?php'."\n".
    9696    ''."\n".
    9797    'class LocaleText'.$this->Texts->Code.' extends LocaleText'."\n".
    9898    '{'."\n".
    99           '  function Load()'."\n".
    100           '  {'."\n".
    101           '    $this->Code = \''.$this->Texts->Code.'\';'."\n".
    102           '    $this->Title = \''.$this->Texts->Title.'\';'."\n".
    103                 '    $this->Data = array('."\n";
    104                 foreach($this->Texts->Data as $Index => $Item)
    105                 {
    106                         $Content .= "      '".$Index."' => '".$Item."',\n";
    107                 }
    108                 $Content .= '    );'."\n".
    109           '  }'."\n".
     99    '  function Load()'."\n".
     100    '  {'."\n".
     101    '    $this->Code = \''.$this->Texts->Code.'\';'."\n".
     102    '    $this->Title = \''.$this->Texts->Title.'\';'."\n".
     103    '    $this->Data = array('."\n";
     104    foreach($this->Texts->Data as $Index => $Item)
     105    {
     106      $Content .= "      '".$Index."' => '".$Item."',\n";
     107    }
     108    $Content .= '    );'."\n".
     109    '  }'."\n".
    110110    '}'."\n";
    111111    file_put_contents($FileName, $Content);
     
    114114  function LoadFromDatabase($Database, $LangCode)
    115115  {
    116         $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
    117         if($DbResult->num_rows > 0)
    118         {
    119           $Language = $DbResult->fetch_assoc();
    120         $this->Texts->Data = array();
    121         $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$Language['Id']);
    122           while($DbRow = $DbResult->fetch_assoc())
    123             $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
    124         }
     116    $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     117    if($DbResult->num_rows > 0)
     118    {
     119      $Language = $DbResult->fetch_assoc();
     120      $this->Texts->Data = array();
     121      $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$Language['Id']);
     122      while($DbRow = $DbResult->fetch_assoc())
     123        $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
     124    }
    125125  }
    126126
    127127  function SaveToDatabase(Database $Database, $LangCode)
    128128  {
    129         $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
    130         if($DbResult->num_rows > 0)
    131         {
    132           $Language = $DbResult->fetch_assoc();
    133           $Database->delete('Locale', '`Language`='.$Language['Id']);
    134           foreach($this->Texts->Data as $Index => $Item)
    135           $Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
    136             'VALUES('.$Language['Id'].','.$Database->quote($Index).','.$Database->quote($Item).')');
    137         }
     129    $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     130    if($DbResult->num_rows > 0)
     131    {
     132      $Language = $DbResult->fetch_assoc();
     133      $Database->delete('Locale', '`Language`='.$Language['Id']);
     134      foreach($this->Texts->Data as $Index => $Item)
     135        $Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
     136        'VALUES('.$Language['Id'].','.$Database->quote($Index).','.$Database->quote($Item).')');
     137    }
    138138  }
    139139
    140140  function UpdateToDatabase(Database $Database, $LangCode)
    141141  {
    142         $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
    143         if($DbResult->num_rows > 0)
    144         {
    145           $Language = $DbResult->fetch_assoc();
    146         foreach($this->Texts->Data as $Index => $Item)
    147       {
    148             $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).') AND (`Language`='.($Language['Id']).')');
    149           if($DbResult->num_rows > 0)
    150           $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
     142    $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     143    if($DbResult->num_rows > 0)
     144    {
     145      $Language = $DbResult->fetch_assoc();
     146      foreach($this->Texts->Data as $Index => $Item)
     147      {
     148        $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).') AND (`Language`='.($Language['Id']).')');
     149        if($DbResult->num_rows > 0)
     150        $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
    151151          '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
    152           else $Database->insert('Locale', array('Language' => $Language['Id'],
     152        else $Database->insert('Locale', array('Language' => $Language['Id'],
    153153         'Original' => $Index, 'Translated' => $Item));
    154154      }
    155         }
     155    }
    156156  }
    157157}
     
    159159class LocaleManager extends Model
    160160{
    161         var $CurrentLocale;
    162         var $Codes;
    163         var $Dir;
    164 
    165         function __construct($System)
    166         {
    167                 parent::__construct($System);
    168                 $this->Codes = array('en');
    169                 $this->CurrentLocale = new LocaleFile($System);
    170         }
    171 
    172         function LoadAvailable()
    173         {
    174                 $this->Available = array();
    175         $FileList = scandir($this->Dir);
     161  var $CurrentLocale;
     162  var $Codes;
     163  var $Dir;
     164
     165  function __construct($System)
     166  {
     167    parent::__construct($System);
     168    $this->Codes = array('en');
     169    $this->CurrentLocale = new LocaleFile($System);
     170  }
     171
     172  function LoadAvailable()
     173  {
     174    $this->Available = array();
     175    $FileList = scandir($this->Dir);
    176176    foreach($FileList as $FileName)
    177177    {
    178           if(substr($FileName, -4) == '.php')
    179           {
    180                 $Code = substr($FileName, 0, -4);
    181                 $Locale = new LocaleFile($this->System);
    182                 $Locale->Dir = $this->Dir;
    183                 $Locale->Load($Code);
    184                 $this->Available['Code'] = array('Code' => $Code, 'Title' => $Locale->Texts->Title);
    185           }
    186           }
    187   }
    188 
    189         function UpdateAll($Directory)
    190         {
    191                 $Locale = new LocaleFile($this->System);
    192                 $Locale->AnalyzeCode($Directory);
    193           $FileList = scandir($this->Dir);
     178      if(substr($FileName, -4) == '.php')
     179      {
     180        $Code = substr($FileName, 0, -4);
     181        $Locale = new LocaleFile($this->System);
     182        $Locale->Dir = $this->Dir;
     183        $Locale->Load($Code);
     184        $this->Available['Code'] = array('Code' => $Code, 'Title' => $Locale->Texts->Title);
     185      }
     186    }
     187  }
     188
     189  function UpdateAll($Directory)
     190  {
     191    $Locale = new LocaleFile($this->System);
     192    $Locale->AnalyzeCode($Directory);
     193    $FileList = scandir($this->Dir);
    194194    foreach($FileList as $FileName)
    195195    {
    196           if(substr($FileName, -4) == '.php')
    197           {
    198                   $FileLocale = new LocaleFile($this->System);
    199                   $FileLocale->Dir = $this->Dir;
    200                   $FileLocale->Load(substr($FileName, 0, -4));
    201 
    202                   // Add new
    203                   foreach($Locale->Texts->Data as $Index => $Item)
    204                     if(!array_key_exists($Index, $FileLocale->Texts->Data))
    205                       $FileLocale->Texts->Data[$Index] = $Item;
    206                   // Remove old
    207                   foreach($FileLocale->Texts->Data as $Index => $Item)
    208                     if(!array_key_exists($Index, $Locale->Texts->Data))
    209                       unset($FileLocale->Texts->Data[$Index]);
    210               $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
    211               $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
    212             $FileLocale->SaveToFile($FileName);
    213           }
    214         }
    215         $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
    216         }
    217 
    218         function LoadLocale($Code)
    219         {
     196      if(substr($FileName, -4) == '.php')
     197      {
     198        $FileLocale = new LocaleFile($this->System);
     199        $FileLocale->Dir = $this->Dir;
     200        $FileLocale->Load(substr($FileName, 0, -4));
     201
     202        // Add new
     203        foreach($Locale->Texts->Data as $Index => $Item)
     204          if(!array_key_exists($Index, $FileLocale->Texts->Data))
     205            $FileLocale->Texts->Data[$Index] = $Item;
     206        // Remove old
     207        foreach($FileLocale->Texts->Data as $Index => $Item)
     208          if(!array_key_exists($Index, $Locale->Texts->Data))
     209            unset($FileLocale->Texts->Data[$Index]);
     210        $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
     211        $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
     212        $FileLocale->SaveToFile($FileName);
     213      }
     214    }
     215    $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
     216  }
     217
     218  function LoadLocale($Code)
     219  {
    220220    $this->CurrentLocale->Dir = $this->Dir;
    221           $this->CurrentLocale->Load($Code);
    222         }
     221    $this->CurrentLocale->Load($Code);
     222  }
    223223}
    224224
     
    226226function T($Text)
    227227{
    228         global $LocaleManager;
    229 
    230         if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text));
    231           else return($Text);
    232 }
     228  global $LocaleManager;
     229
     230  if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text));
     231    else return($Text);
     232}
Note: See TracChangeset for help on using the changeset viewer.