source: trunk/includes/Locale.php@ 618

Last change on this file since 618 was 618, checked in by chronos, 11 years ago
  • Added: Language selection menu in top right panel.
  • Added: All language files are updated from administration.
  • Added: LocaleManager to implement basic management functions.
File size: 6.8 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)
21 {
22 if(array_key_exists($Text, $this->Data) and ($this->Data[$Text] != ''))
23 return($this->Data[$Text]);
24 else return($Text);
25 }
26}
27
28class Locale extends Model
29{
30 var $Texts;
31 var $Dir;
32
33 function __construct($System)
34 {
35 parent::__construct($System);
36 $this->Texts = new LocaleText();
37 }
38
39 function Load($Language)
40 {
41 $FileName = $this->Dir.'/'.$Language.'.php';
42 if(file_exists($FileName)) {
43 include_once($FileName);
44 $ClassName = 'LocaleText'.$Language;
45 $this->Texts = new $ClassName();
46 } else throw new Exception('Language file '.$FileName.' not found');
47 $this->Texts->Load();
48 }
49
50 function AnalyzeCode($Path)
51 {
52 // Search for php files
53 $FileList = scandir($Path);
54 foreach($FileList as $FileName)
55 {
56 $FullName = $Path.'/'.$FileName;
57 if(($FileName == '.') or ($FileName == '..')) ; // Skip virtual items
58 else if(substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files
59 else if(is_dir($FullName)) $this->AnalyzeCode($FullName);
60 else if(file_exists($FullName))
61 {
62 if(substr($FullName, -4) == '.php')
63 {
64 $Content = file_get_contents($FullName);
65 // Search occurence of T() function
66 while(strpos($Content, 'T(') !== false)
67 {
68 $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
69 $Content = substr($Content, strpos($Content, 'T(') + 2);
70 $Ord = ord($Previous);
71 //echo($Ord.',');
72 if(!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
73 {
74 // Do for non-alpha previous character
75 $Original = substr($Content, 0, strpos($Content, ')'));
76 $Original2 = '';
77 if((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
78 $Original2 = substr($Original, 1, -1);
79 if((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
80 $Original2 = substr($Original, 1, -1);
81 if($Original2 != '')
82 {
83 if(!array_key_exists($Original2, $this->Texts->Data))
84 $this->Texts->Data[$Original2] = '';
85 }
86 }
87 }
88 }
89 }
90 }
91 }
92
93 function SaveToFile($FileName)
94 {
95 $Content = '<?php'."\n".
96 ''."\n".
97 'class LocaleText'.$this->Texts->Code.' extends LocaleText'."\n".
98 '{'."\n".
99 ' function Load()'."\n".
100 ' {'."\n".
101 ' $this->Code = \''.$this->Texts->Code.'\';'."\n".
102 ' $this->Title = \''.$this->Texts->Title.'\';'."\n".
103 ' $this->Data = array('."\n";
104 foreach($this->Texts->Data as $Index => $Item)
105 {
106 $Content .= " '".$Index."' => '".$Item."',\n";
107 }
108 $Content .= ' );'."\n".
109 ' }'."\n".
110 '}'."\n";
111 file_put_contents($FileName, $Content);
112 }
113
114 function LoadFromDatabase($Database, $LangCode)
115 {
116 $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
117 if($DbResult->num_rows > 0)
118 {
119 $Language = $DbResult->fetch_assoc();
120 $this->Texts->Data = array();
121 $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$Language['Id']);
122 while($DbRow = $DbResult->fetch_assoc())
123 $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
124 }
125 }
126
127 function SaveToDatabase(Database $Database, $LangCode)
128 {
129 $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
130 if($DbResult->num_rows > 0)
131 {
132 $Language = $DbResult->fetch_assoc();
133 $Database->delete('Locale', '`Language`='.$Language['Id']);
134 foreach($this->Texts->Data as $Index => $Item)
135 $Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
136 'VALUES('.$Language['Id'].','.$Database->quote($Index).','.$Database->quote($Item).')');
137 }
138 }
139
140 function UpdateToDatabase(Database $Database, $LangCode)
141 {
142 $DbResult = $Database->select('Language', '*', 'Code='.$Database->quote($LangCode));
143 if($DbResult->num_rows > 0)
144 {
145 $Language = $DbResult->fetch_assoc();
146 foreach($this->Texts->Data as $Index => $Item)
147 {
148 $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).') AND (`Language`='.($Language['Id']).')');
149 if($DbResult->num_rows > 0)
150 $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
151 '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
152 else $Database->insert('Locale', array('Language' => $Language['Id'],
153 'Original' => $Index, 'Translated' => $Item));
154 }
155 }
156 }
157}
158
159class LocaleManager extends Model
160{
161 var $CurrentLocale;
162 var $Codes;
163 var $Dir;
164
165 function __construct($System)
166 {
167 parent::__construct($System);
168 $this->Codes = array('en');
169 $this->CurrentLocale = new Locale($System);
170 }
171
172 function LoadAvailable()
173 {
174 $this->Available = array();
175 $FileList = scandir($this->Dir);
176 foreach($FileList as $FileName)
177 {
178 if(substr($FileName, -4) == '.php')
179 {
180 $Code = substr($FileName, 0, -4);
181 $Locale = new Locale($this->System);
182 $Locale->Dir = $this->Dir;
183 $Locale->Load($Code);
184 $this->Available['Code'] = array('Code' => $Code, 'Title' => $Locale->Texts->Title);
185 }
186 }
187 }
188
189 function UpdateAll($Directory)
190 {
191 $Locale = new Locale($this->System);
192 $Locale->AnalyzeCode($Directory);
193 $FileList = scandir($this->Dir);
194 foreach($FileList as $FileName)
195 {
196 if(substr($FileName, -4) == '.php')
197 {
198 $FileLocale = new Locale($this->System);
199 $FileLocale->Dir = $this->Dir;
200 $FileLocale->Load(substr($FileName, 0, -4));
201
202 // Add new
203 foreach($Locale->Texts->Data as $Index => $Item)
204 if(!array_key_exists($Index, $FileLocale->Texts->Data))
205 $FileLocale->Texts->Data[$Index] = $Item;
206 // Remove old
207 foreach($FileLocale->Texts->Data as $Index => $Item)
208 if(!array_key_exists($Index, $Locale->Texts->Data))
209 unset($FileLocale->Texts->Data[$Index]);
210 $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
211 $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
212 $FileLocale->SaveToFile($FileName);
213 }
214 }
215 $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
216 }
217
218 function LoadLocale($Code)
219 {
220 $this->CurrentLocale->Dir = $this->Dir;
221 $this->CurrentLocale->Load($Code);
222 }
223}
224
225// Short named global function
226function T($Text)
227{
228 global $LocaleManager;
229
230 if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text));
231 else return($Text);
232}
Note: See TracBrowser for help on using the repository browser.