source: trunk/Packages/Common/Locale.php

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