Ignore:
Timestamp:
Dec 27, 2022, 7:50:23 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Locale.php

    r884 r888  
    33class LocaleText
    44{
    5   var $Data;
    6   var $Code;
    7   var $Title;
     5  public array $Data;
     6  public string $Code;
     7  public string $Title;
    88
    99  function __construct()
     
    1414  }
    1515
    16   function Load()
    17   {
    18   }
    19 
    20   function Translate($Text, $Group = '')
     16  function Load(): void
     17  {
     18  }
     19
     20  function Translate(string $Text, string $Group = ''): string
    2121  {
    2222    if (array_key_exists($Text, $this->Data[$Group]) and ($this->Data[$Group][$Text] != ''))
     
    2525  }
    2626
    27   function TranslateReverse($Text, $Group = '')
     27  function TranslateReverse(string $Text, string $Group = ''): string
    2828  {
    2929    $Key = array_search($Text, $this->Data[$Group]);
     
    3535class LocaleFile extends Model
    3636{
    37   var $Texts;
    38   var $Dir;
     37  public LocaleText $Texts;
     38  public string $Dir;
    3939
    4040  function __construct(System $System)
     
    4444  }
    4545
    46   function Load($Language)
     46  function Load(string $Language): void
    4747  {
    4848    $FileName = $this->Dir.'/'.$Language.'.php';
     
    5555  }
    5656
    57   function AnalyzeCode($Path)
     57  function AnalyzeCode(string $Path): void
    5858  {
    5959    // Search for php files
     
    7575            $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
    7676            $Content = substr($Content, strpos($Content, 'T(') + 2);
    77             $Ord = ord($Previous);
    78             //echo($Ord.',');
     77            $Ord = ord($Previous);           
    7978            if (!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
    8079            {
     
    9897  }
    9998
    100   function SaveToFile($FileName)
     99  function SaveToFile(string $FileName): void
    101100  {
    102101    $Content = '<?php'."\n".
     
    119118  }
    120119
    121   function LoadFromDatabase($Database, $LangCode)
     120  function LoadFromDatabase(Database $Database, string $LangCode): void
    122121  {
    123122    $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     
    132131  }
    133132
    134   function SaveToDatabase(Database $Database, $LangCode)
    135   {
    136     $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     133  function SaveToDatabase(string $LangCode): void
     134  {
     135    $DbResult = $this->Database->select('Language', '*', 'Code='.$this->Database->quote($LangCode));
    137136    if ($DbResult->num_rows > 0)
    138137    {
    139138      $Language = $DbResult->fetch_assoc();
    140       $Database->delete('Locale', '`Language`='.$Language['Id']);
     139      $this->Database->delete('Locale', '`Language`='.$Language['Id']);
    141140      foreach ($this->Texts->Data as $Index => $Item)
    142         $Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
    143         'VALUES('.$Language['Id'].','.$Database->quote($Index).','.$Database->quote($Item).')');
    144     }
    145   }
    146 
    147   function UpdateToDatabase(Database $Database, $LangCode)
    148   {
    149     $DbResult = $Database->select('Language', '*', '`Code`='.$Database->quote($LangCode));
     141        $this->Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
     142        'VALUES('.$Language['Id'].','.$this->Database->quote($Index).','.$this->Database->quote($Item).')');
     143    }
     144  }
     145
     146  function UpdateToDatabase(string $LangCode): void
     147  {
     148    $DbResult = $this->Database->select('Language', '*', '`Code`='.$this->Database->quote($LangCode));
    150149    if ($DbResult->num_rows > 0)
    151150    {
     
    153152      foreach ($this->Texts->Data as $Index => $Item)
    154153      {
    155         if (is_array($Item)) continue;
    156         $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).
     154        $DbResult = $this->Database->select('Locale', '*', '(`Original` ='.$this->Database->quote($Index).
    157155          ') AND (`Language`='.($Language['Id']).')');
    158156        if ($DbResult->num_rows > 0)
    159         {
    160           $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
    161             '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
    162         } else
    163         {
    164           $Database->insert('Locale', array('Language' => $Language['Id'],
    165            'Original' => $Index, 'Translated' => $Item, 'Fuzzy' => 0));
    166         }
     157        $this->Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
     158          '(`Original` ='.$this->Database->quote($Index).')', array('Translated' => $Item));
     159        else $this->Database->insert('Locale', array('Language' => $Language['Id'],
     160         'Original' => $Index, 'Translated' => $Item));
    167161      }
    168162    }
     
    172166class LocaleManager extends Model
    173167{
    174   var $CurrentLocale;
    175   var $Codes;
    176   var $Dir;
    177   var $LangCode;
    178   var $DefaultLangCode;
    179   var $Available;
     168  public LocaleFile $CurrentLocale;
     169  public array $Codes;
     170  public string $Dir;
     171  public string $LangCode;
     172  public string $DefaultLangCode;
     173  public array $Available;
    180174
    181175  function __construct(System $System)
     
    187181    $this->DefaultLangCode = 'en';
    188182    $this->Available = array();
    189   }
    190 
    191   function LoadAvailable()
     183    $this->Dir = '';
     184  }
     185
     186  function LoadAvailable(): void
    192187  {
    193188    $this->Available = array();
     
    206201  }
    207202
    208   function UpdateAll($Directory)
     203  function UpdateAll(string $Directory): void
    209204  {
    210205    $Locale = new LocaleFile($this->System);
     
    227222          if (!array_key_exists($Index, $Locale->Texts->Data))
    228223            unset($FileLocale->Texts->Data[$Index]);
    229         $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
     224        $FileLocale->UpdateToDatabase($FileLocale->Texts->Code);
    230225        $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
    231226        $FileLocale->SaveToFile($FileName);
     
    235230  }
    236231
    237   function LoadLocale($Code)
     232  function LoadLocale(string $Code): void
    238233  {
    239234    if (array_key_exists($Code, $this->Available))
     
    246241
    247242// Short named global translation function
    248 function T($Text, $Group = '')
     243function T(string $Text, string $Group = ''): string
    249244{
    250245  global $GlobalLocaleManager;
Note: See TracChangeset for help on using the changeset viewer.