Ignore:
Timestamp:
Dec 6, 2021, 11:33:48 AM (3 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
File:
1 edited

Legend:

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

    r92 r95  
    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
     
    9898  }
    9999
    100   function SaveToFile($FileName)
     100  function SaveToFile(string $FileName): void
    101101  {
    102102    $Content = '<?php'."\n".
     
    119119  }
    120120
    121   function LoadFromDatabase($Database, $LangCode)
     121  function LoadFromDatabase(Database $Database, string $LangCode): void
    122122  {
    123123    $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     
    132132  }
    133133
    134   function SaveToDatabase(Database $Database, $LangCode)
    135   {
    136     $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
     134  function SaveToDatabase(string $LangCode): void
     135  {
     136    $DbResult = $this->Database->select('Language', '*', 'Code='.$this->Database->quote($LangCode));
    137137    if ($DbResult->num_rows > 0)
    138138    {
    139139      $Language = $DbResult->fetch_assoc();
    140       $Database->delete('Locale', '`Language`='.$Language['Id']);
     140      $this->Database->delete('Locale', '`Language`='.$Language['Id']);
    141141      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));
     142        $this->Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
     143        'VALUES('.$Language['Id'].','.$this->Database->quote($Index).','.$this->Database->quote($Item).')');
     144    }
     145  }
     146
     147  function UpdateToDatabase(string $LangCode): void
     148  {
     149    $DbResult = $this->Database->select('Language', '*', '`Code`='.$this->Database->quote($LangCode));
    150150    if ($DbResult->num_rows > 0)
    151151    {
     
    153153      foreach ($this->Texts->Data as $Index => $Item)
    154154      {
    155         $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).
     155        $DbResult = $this->Database->select('Locale', '*', '(`Original` ='.$this->Database->quote($Index).
    156156          ') AND (`Language`='.($Language['Id']).')');
    157157        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'],
     158        $this->Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
     159          '(`Original` ='.$this->Database->quote($Index).')', array('Translated' => $Item));
     160        else $this->Database->insert('Locale', array('Language' => $Language['Id'],
    161161         'Original' => $Index, 'Translated' => $Item));
    162162      }
     
    167167class LocaleManager extends Model
    168168{
    169   var $CurrentLocale;
    170   var $Codes;
    171   var $Dir;
    172   var $LangCode;
    173   var $DefaultLangCode;
    174   var $Available;
     169  public LocaleFile $CurrentLocale;
     170  public array $Codes;
     171  public string $Dir;
     172  public string $LangCode;
     173  public string $DefaultLangCode;
     174  public array $Available;
    175175
    176176  function __construct(System $System)
     
    182182    $this->DefaultLangCode = 'en';
    183183    $this->Available = array();
    184   }
    185 
    186   function LoadAvailable()
     184    $this->Dir = '';
     185  }
     186
     187  function LoadAvailable(): void
    187188  {
    188189    $this->Available = array();
     
    201202  }
    202203
    203   function UpdateAll($Directory)
     204  function UpdateAll(string $Directory): void
    204205  {
    205206    $Locale = new LocaleFile($this->System);
     
    222223          if (!array_key_exists($Index, $Locale->Texts->Data))
    223224            unset($FileLocale->Texts->Data[$Index]);
    224         $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
     225        $FileLocale->UpdateToDatabase($FileLocale->Texts->Code);
    225226        $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
    226227        $FileLocale->SaveToFile($FileName);
     
    230231  }
    231232
    232   function LoadLocale($Code)
     233  function LoadLocale(string $Code): void
    233234  {
    234235    if (array_key_exists($Code, $this->Available))
     
    241242
    242243// Short named global translation function
    243 function T($Text, $Group = '')
     244function T(string $Text, string $Group = ''): string
    244245{
    245246  global $GlobalLocaleManager;
Note: See TracChangeset for help on using the changeset viewer.