source: Common/Locale.php

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