source: trunk/Packages/Common/Locale.php

Last change on this file was 3, checked in by chronos, 8 years ago
  • Modified: Updated to work with PHP7. Old database class replaced by Common package.
File size: 7.5 KB
Line 
1<?php
2
3class LocaleText
4{
5 var $Data;
6 var $Code;
7 var $Title;
8
9 function __construct()
10 {
11 $this->Data = array();
12 $this->Code = 'en';
13 $this->Title = 'English';
14 }
15
16 function Load()
17 {
18 }
19
20 function Translate($Text, $Group = '')
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($Text, $Group = '')
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 var $Texts;
38 var $Dir;
39
40 function __construct(System $System)
41 {
42 parent::__construct($System);
43 $this->Texts = new LocaleText();
44 }
45
46 function Load($Language)
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($Path)
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($FileName)
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, $LangCode)
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(Database $Database, $LangCode)
135 {
136 $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
137 if($DbResult->num_rows > 0)
138 {
139 $Language = $DbResult->fetch_assoc();
140 $Database->delete('Locale', '`Language`='.$Language['Id']);
141 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));
150 if($DbResult->num_rows > 0)
151 {
152 $Language = $DbResult->fetch_assoc();
153 foreach($this->Texts->Data as $Index => $Item)
154 {
155 $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).
156 ') AND (`Language`='.($Language['Id']).')');
157 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'],
161 'Original' => $Index, 'Translated' => $Item));
162 }
163 }
164 }
165}
166
167class LocaleManager extends Model
168{
169 var $CurrentLocale;
170 var $Codes;
171 var $Dir;
172 var $LangCode;
173 var $DefaultLangCode;
174 var $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 }
185
186 function LoadAvailable()
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($Directory)
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($this->System->Database, $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($Code)
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($Text, $Group = '')
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.