source: trunk/Packages/Common/Locale.php

Last change on this file was 8, checked in by chronos, 19 months ago
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File size: 7.8 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 if (!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
79 {
80 // Do for non-alpha previous character
81 $Original = substr($Content, 0, strpos($Content, ')'));
82 $Original2 = '';
83 if ((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
84 $Original2 = substr($Original, 1, -1);
85 if ((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
86 $Original2 = substr($Original, 1, -1);
87 if ($Original2 != '')
88 {
89 if (!array_key_exists($Original2, $this->Texts->Data))
90 $this->Texts->Data[$Original2] = '';
91 }
92 }
93 }
94 }
95 }
96 }
97 }
98
99 function SaveToFile(string $FileName): void
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";
110 foreach ($this->Texts->Data as $Index => $Item)
111 {
112 $Content .= " '".$Index."' => '".$Item."',\n";
113 }
114 $Content .= ' );'."\n".
115 ' }'."\n".
116 '}'."\n";
117 file_put_contents($FileName, $Content);
118 }
119
120 function LoadFromDatabase(Database $Database, string $LangCode): void
121 {
122 $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
123 if ($DbResult->num_rows > 0)
124 {
125 $Language = $DbResult->fetch_assoc();
126 $this->Texts->Data = array();
127 $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$Language['Id']);
128 while ($DbRow = $DbResult->fetch_assoc())
129 $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
130 }
131 }
132
133 function SaveToDatabase(string $LangCode): void
134 {
135 $DbResult = $this->Database->select('Language', '*', 'Code='.$this->Database->quote($LangCode));
136 if ($DbResult->num_rows > 0)
137 {
138 $Language = $DbResult->fetch_assoc();
139 $this->Database->delete('Locale', '`Language`='.$Language['Id']);
140 foreach ($this->Texts->Data as $Index => $Item)
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));
149 if ($DbResult->num_rows > 0)
150 {
151 $Language = $DbResult->fetch_assoc();
152 foreach ($this->Texts->Data as $Index => $Item)
153 {
154 $DbResult = $this->Database->select('Locale', '*', '(`Original` ='.$this->Database->quote($Index).
155 ') AND (`Language`='.($Language['Id']).')');
156 if ($DbResult->num_rows > 0)
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'],
160 'Original' => $Index, 'Translated' => $Item));
161 }
162 }
163 }
164}
165
166class LocaleManager extends Model
167{
168 public LocaleFile $CurrentLocale;
169 public array $Codes;
170 public string $Dir;
171 public string $LangCode;
172 public string $DefaultLangCode;
173 public array $Available;
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();
183 $this->Dir = '';
184 }
185
186 function LoadAvailable(): void
187 {
188 $this->Available = array();
189 $FileList = scandir($this->Dir);
190 foreach ($FileList as $FileName)
191 {
192 if (substr($FileName, -4) == '.php')
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
203 function UpdateAll(string $Directory): void
204 {
205 $Locale = new LocaleFile($this->System);
206 $Locale->AnalyzeCode($Directory);
207 $FileList = scandir($this->Dir);
208 foreach ($FileList as $FileName)
209 {
210 if (substr($FileName, -4) == '.php')
211 {
212 $FileLocale = new LocaleFile($this->System);
213 $FileLocale->Dir = $this->Dir;
214 $FileLocale->Load(substr($FileName, 0, -4));
215
216 // Add new
217 foreach ($Locale->Texts->Data as $Index => $Item)
218 if (!array_key_exists($Index, $FileLocale->Texts->Data))
219 $FileLocale->Texts->Data[$Index] = $Item;
220 // Remove old
221 foreach ($FileLocale->Texts->Data as $Index => $Item)
222 if (!array_key_exists($Index, $Locale->Texts->Data))
223 unset($FileLocale->Texts->Data[$Index]);
224 $FileLocale->UpdateToDatabase($FileLocale->Texts->Code);
225 $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
226 $FileLocale->SaveToFile($FileName);
227 }
228 }
229 $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
230 }
231
232 function LoadLocale(string $Code): void
233 {
234 if (array_key_exists($Code, $this->Available))
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
243function T(string $Text, string $Group = ''): string
244{
245 global $GlobalLocaleManager;
246
247 if (isset($GlobalLocaleManager)) return $GlobalLocaleManager->CurrentLocale->Texts->Translate($Text, $Group);
248 else return $Text;
249}
Note: See TracBrowser for help on using the repository browser.