Ignore:
Timestamp:
Jun 1, 2023, 12:18:18 AM (18 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File:
1 edited

Legend:

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

    r7 r8  
    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         $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).
     154        $DbResult = $this->Database->select('Locale', '*', '(`Original` ='.$this->Database->quote($Index).
    156155          ') AND (`Language`='.($Language['Id']).')');
    157156        if ($DbResult->num_rows > 0)
    158         $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
    159           '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
    160         else $Database->insert('Locale', array('Language' => $Language['Id'],
     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'],
    161160         'Original' => $Index, 'Translated' => $Item));
    162161      }
     
    167166class LocaleManager extends Model
    168167{
    169   var $CurrentLocale;
    170   var $Codes;
    171   var $Dir;
    172   var $LangCode;
    173   var $DefaultLangCode;
    174   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;
    175174
    176175  function __construct(System $System)
     
    182181    $this->DefaultLangCode = 'en';
    183182    $this->Available = array();
    184   }
    185 
    186   function LoadAvailable()
     183    $this->Dir = '';
     184  }
     185
     186  function LoadAvailable(): void
    187187  {
    188188    $this->Available = array();
     
    201201  }
    202202
    203   function UpdateAll($Directory)
     203  function UpdateAll(string $Directory): void
    204204  {
    205205    $Locale = new LocaleFile($this->System);
     
    222222          if (!array_key_exists($Index, $Locale->Texts->Data))
    223223            unset($FileLocale->Texts->Data[$Index]);
    224         $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
     224        $FileLocale->UpdateToDatabase($FileLocale->Texts->Code);
    225225        $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
    226226        $FileLocale->SaveToFile($FileName);
     
    230230  }
    231231
    232   function LoadLocale($Code)
     232  function LoadLocale(string $Code): void
    233233  {
    234234    if (array_key_exists($Code, $this->Available))
     
    241241
    242242// Short named global translation function
    243 function T($Text, $Group = '')
     243function T(string $Text, string $Group = ''): string
    244244{
    245245  global $GlobalLocaleManager;
Note: See TracChangeset for help on using the changeset viewer.