[791] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | class LocaleText
|
---|
| 4 | {
|
---|
[887] | 5 | public array $Data;
|
---|
| 6 | public string $Code;
|
---|
| 7 | public string $Title;
|
---|
[791] | 8 |
|
---|
| 9 | function __construct()
|
---|
| 10 | {
|
---|
| 11 | $this->Data = array();
|
---|
| 12 | $this->Code = 'en';
|
---|
| 13 | $this->Title = 'English';
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[887] | 16 | function Load(): void
|
---|
[791] | 17 | {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[887] | 20 | function Translate(string $Text, string $Group = ''): string
|
---|
[791] | 21 | {
|
---|
[873] | 22 | if (array_key_exists($Text, $this->Data[$Group]) and ($this->Data[$Group][$Text] != ''))
|
---|
[874] | 23 | return $this->Data[$Group][$Text];
|
---|
| 24 | else return $Text;
|
---|
[791] | 25 | }
|
---|
| 26 |
|
---|
[887] | 27 | function TranslateReverse(string $Text, string $Group = ''): string
|
---|
[791] | 28 | {
|
---|
| 29 | $Key = array_search($Text, $this->Data[$Group]);
|
---|
[874] | 30 | if (($Key === FALSE) or ($this->Data[$Group][$Key] == '')) return $Text;
|
---|
| 31 | else return $Key;
|
---|
[791] | 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | class LocaleFile extends Model
|
---|
| 36 | {
|
---|
[887] | 37 | public LocaleText $Texts;
|
---|
| 38 | public string $Dir;
|
---|
[791] | 39 |
|
---|
| 40 | function __construct(System $System)
|
---|
| 41 | {
|
---|
| 42 | parent::__construct($System);
|
---|
| 43 | $this->Texts = new LocaleText();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[887] | 46 | function Load(string $Language): void
|
---|
[791] | 47 | {
|
---|
| 48 | $FileName = $this->Dir.'/'.$Language.'.php';
|
---|
[873] | 49 | if (file_exists($FileName)) {
|
---|
[791] | 50 | include_once($FileName);
|
---|
| 51 | $ClassName = 'LocaleText'.$Language;
|
---|
| 52 | $this->Texts = new $ClassName();
|
---|
| 53 | } else throw new Exception(sprintf(T('Language file %s not found'), $FileName));
|
---|
| 54 | $this->Texts->Load();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[887] | 57 | function AnalyzeCode(string $Path): void
|
---|
[791] | 58 | {
|
---|
| 59 | // Search for php files
|
---|
| 60 | $FileList = scandir($Path);
|
---|
[873] | 61 | foreach ($FileList as $FileName)
|
---|
[791] | 62 | {
|
---|
| 63 | $FullName = $Path.'/'.$FileName;
|
---|
[873] | 64 | if (($FileName == '.') or ($FileName == '..')) ; // Skip virtual items
|
---|
| 65 | else if (substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files
|
---|
| 66 | else if (is_dir($FullName)) $this->AnalyzeCode($FullName);
|
---|
| 67 | else if (file_exists($FullName))
|
---|
[791] | 68 | {
|
---|
[873] | 69 | if (substr($FullName, -4) == '.php')
|
---|
[791] | 70 | {
|
---|
| 71 | $Content = file_get_contents($FullName);
|
---|
| 72 | // Search occurence of T() function
|
---|
[873] | 73 | while (strpos($Content, 'T(') !== false)
|
---|
[791] | 74 | {
|
---|
| 75 | $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
|
---|
| 76 | $Content = substr($Content, strpos($Content, 'T(') + 2);
|
---|
[929] | 77 | $Ord = ord($Previous);
|
---|
[873] | 78 | if (!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
|
---|
[791] | 79 | {
|
---|
| 80 | // Do for non-alpha previous character
|
---|
| 81 | $Original = substr($Content, 0, strpos($Content, ')'));
|
---|
| 82 | $Original2 = '';
|
---|
[873] | 83 | if ((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
|
---|
[791] | 84 | $Original2 = substr($Original, 1, -1);
|
---|
[873] | 85 | if ((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
|
---|
[791] | 86 | $Original2 = substr($Original, 1, -1);
|
---|
[873] | 87 | if ($Original2 != '')
|
---|
[791] | 88 | {
|
---|
[873] | 89 | if (!array_key_exists($Original2, $this->Texts->Data))
|
---|
[791] | 90 | $this->Texts->Data[$Original2] = '';
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[887] | 99 | function SaveToFile(string $FileName): void
|
---|
[791] | 100 | {
|
---|
| 101 | $Content = '<?php'."\n".
|
---|
| 102 | ''."\n".
|
---|
| 103 | 'class LocaleText'.$this->Texts->Code.' extends LocaleText'."\n".
|
---|
| 104 | '{'."\n".
|
---|
| 105 | ' function Load()'."\n".
|
---|
| 106 | ' {'."\n".
|
---|
| 107 | ' $this->Code = \''.$this->Texts->Code.'\';'."\n".
|
---|
| 108 | ' $this->Title = \''.$this->Texts->Title.'\';'."\n".
|
---|
| 109 | ' $this->Data = array('."\n";
|
---|
[873] | 110 | foreach ($this->Texts->Data as $Index => $Item)
|
---|
[791] | 111 | {
|
---|
| 112 | $Content .= " '".$Index."' => '".$Item."',\n";
|
---|
| 113 | }
|
---|
| 114 | $Content .= ' );'."\n".
|
---|
| 115 | ' }'."\n".
|
---|
| 116 | '}'."\n";
|
---|
| 117 | file_put_contents($FileName, $Content);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[887] | 120 | function LoadFromDatabase(Database $Database, string $LangCode): void
|
---|
[791] | 121 | {
|
---|
| 122 | $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
|
---|
[873] | 123 | if ($DbResult->num_rows > 0)
|
---|
[791] | 124 | {
|
---|
| 125 | $Language = $DbResult->fetch_assoc();
|
---|
| 126 | $this->Texts->Data = array();
|
---|
| 127 | $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$Language['Id']);
|
---|
[873] | 128 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
[791] | 129 | $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[887] | 133 | function SaveToDatabase(string $LangCode): void
|
---|
[791] | 134 | {
|
---|
[887] | 135 | $DbResult = $this->Database->select('Language', '*', 'Code='.$this->Database->quote($LangCode));
|
---|
[873] | 136 | if ($DbResult->num_rows > 0)
|
---|
[791] | 137 | {
|
---|
| 138 | $Language = $DbResult->fetch_assoc();
|
---|
[887] | 139 | $this->Database->delete('Locale', '`Language`='.$Language['Id']);
|
---|
[873] | 140 | foreach ($this->Texts->Data as $Index => $Item)
|
---|
[887] | 141 | $this->Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
|
---|
| 142 | 'VALUES('.$Language['Id'].','.$this->Database->quote($Index).','.$this->Database->quote($Item).')');
|
---|
[791] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[887] | 146 | function UpdateToDatabase(string $LangCode): void
|
---|
[791] | 147 | {
|
---|
[887] | 148 | $DbResult = $this->Database->select('Language', '*', '`Code`='.$this->Database->quote($LangCode));
|
---|
[873] | 149 | if ($DbResult->num_rows > 0)
|
---|
[791] | 150 | {
|
---|
| 151 | $Language = $DbResult->fetch_assoc();
|
---|
[873] | 152 | foreach ($this->Texts->Data as $Index => $Item)
|
---|
[791] | 153 | {
|
---|
[887] | 154 | $DbResult = $this->Database->select('Locale', '*', '(`Original` ='.$this->Database->quote($Index).
|
---|
[791] | 155 | ') AND (`Language`='.($Language['Id']).')');
|
---|
[873] | 156 | if ($DbResult->num_rows > 0)
|
---|
[887] | 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'],
|
---|
[791] | 160 | 'Original' => $Index, 'Translated' => $Item));
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | class LocaleManager extends Model
|
---|
| 167 | {
|
---|
[887] | 168 | public LocaleFile $CurrentLocale;
|
---|
| 169 | public array $Codes;
|
---|
| 170 | public string $Dir;
|
---|
| 171 | public string $LangCode;
|
---|
| 172 | public string $DefaultLangCode;
|
---|
| 173 | public array $Available;
|
---|
[791] | 174 |
|
---|
| 175 | function __construct(System $System)
|
---|
| 176 | {
|
---|
| 177 | parent::__construct($System);
|
---|
| 178 | $this->Codes = array('en');
|
---|
| 179 | $this->CurrentLocale = new LocaleFile($System);
|
---|
| 180 | $this->LangCode = 'en';
|
---|
| 181 | $this->DefaultLangCode = 'en';
|
---|
| 182 | $this->Available = array();
|
---|
[887] | 183 | $this->Dir = '';
|
---|
[791] | 184 | }
|
---|
| 185 |
|
---|
[887] | 186 | function LoadAvailable(): void
|
---|
[791] | 187 | {
|
---|
| 188 | $this->Available = array();
|
---|
| 189 | $FileList = scandir($this->Dir);
|
---|
[873] | 190 | foreach ($FileList as $FileName)
|
---|
[791] | 191 | {
|
---|
[873] | 192 | if (substr($FileName, -4) == '.php')
|
---|
[791] | 193 | {
|
---|
| 194 | $Code = substr($FileName, 0, -4);
|
---|
| 195 | $Locale = new LocaleFile($this->System);
|
---|
| 196 | $Locale->Dir = $this->Dir;
|
---|
| 197 | $Locale->Load($Code);
|
---|
| 198 | $this->Available['Code'] = array('Code' => $Code, 'Title' => $Locale->Texts->Title);
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[887] | 203 | function UpdateAll(string $Directory): void
|
---|
[791] | 204 | {
|
---|
| 205 | $Locale = new LocaleFile($this->System);
|
---|
| 206 | $Locale->AnalyzeCode($Directory);
|
---|
| 207 | $FileList = scandir($this->Dir);
|
---|
[873] | 208 | foreach ($FileList as $FileName)
|
---|
[791] | 209 | {
|
---|
[873] | 210 | if (substr($FileName, -4) == '.php')
|
---|
[791] | 211 | {
|
---|
| 212 | $FileLocale = new LocaleFile($this->System);
|
---|
| 213 | $FileLocale->Dir = $this->Dir;
|
---|
| 214 | $FileLocale->Load(substr($FileName, 0, -4));
|
---|
| 215 |
|
---|
| 216 | // Add new
|
---|
[873] | 217 | foreach ($Locale->Texts->Data as $Index => $Item)
|
---|
| 218 | if (!array_key_exists($Index, $FileLocale->Texts->Data))
|
---|
[791] | 219 | $FileLocale->Texts->Data[$Index] = $Item;
|
---|
| 220 | // Remove old
|
---|
[873] | 221 | foreach ($FileLocale->Texts->Data as $Index => $Item)
|
---|
| 222 | if (!array_key_exists($Index, $Locale->Texts->Data))
|
---|
[791] | 223 | unset($FileLocale->Texts->Data[$Index]);
|
---|
[887] | 224 | $FileLocale->UpdateToDatabase($FileLocale->Texts->Code);
|
---|
[791] | 225 | $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
|
---|
| 226 | $FileLocale->SaveToFile($FileName);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[887] | 232 | function LoadLocale(string $Code): void
|
---|
[791] | 233 | {
|
---|
[873] | 234 | if (array_key_exists($Code, $this->Available))
|
---|
[791] | 235 | {
|
---|
| 236 | $this->CurrentLocale->Dir = $this->Dir;
|
---|
| 237 | $this->CurrentLocale->Load($Code);
|
---|
| 238 | } else throw new Exception(sprintf(T('Unsupported locale code %s'), $Code));
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | // Short named global translation function
|
---|
[887] | 243 | function T(string $Text, string $Group = ''): string
|
---|
[791] | 244 | {
|
---|
| 245 | global $GlobalLocaleManager;
|
---|
| 246 |
|
---|
[874] | 247 | if (isset($GlobalLocaleManager)) return $GlobalLocaleManager->CurrentLocale->Texts->Translate($Text, $Group);
|
---|
| 248 | else return $Text;
|
---|
[791] | 249 | }
|
---|