source: trunk/Packages/Common/Locale.php

Last change on this file was 39, checked in by chronos, 4 years ago
  • Added: Support for locales and czech language. It can be switched on from top language selector.
File size: 7.6 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 if (is_array($Item)) continue;
156 $DbResult = $Database->select('Locale', '*', '(`Original` ='.$Database->quote($Index).
157 ') AND (`Language`='.($Language['Id']).')');
158 if ($DbResult->num_rows > 0)
159 {
160 $Database->update('Locale', '(`Language`='.($Language['Id']).') AND '.
161 '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
162 } else
163 {
164 $Database->insert('Locale', array('Language' => $Language['Id'],
165 'Original' => $Index, 'Translated' => $Item, 'Fuzzy' => 0));
166 }
167 }
168 }
169 }
170}
171
172class LocaleManager extends Model
173{
174 var $CurrentLocale;
175 var $Codes;
176 var $Dir;
177 var $LangCode;
178 var $DefaultLangCode;
179 var $Available;
180
181 function __construct(System $System)
182 {
183 parent::__construct($System);
184 $this->Codes = array('en');
185 $this->CurrentLocale = new LocaleFile($System);
186 $this->LangCode = 'en';
187 $this->DefaultLangCode = 'en';
188 $this->Available = array();
189 }
190
191 function LoadAvailable()
192 {
193 $this->Available = array();
194 $FileList = scandir($this->Dir);
195 foreach ($FileList as $FileName)
196 {
197 if (substr($FileName, -4) == '.php')
198 {
199 $Code = substr($FileName, 0, -4);
200 $Locale = new LocaleFile($this->System);
201 $Locale->Dir = $this->Dir;
202 $Locale->Load($Code);
203 $this->Available['Code'] = array('Code' => $Code, 'Title' => $Locale->Texts->Title);
204 }
205 }
206 }
207
208 function UpdateAll($Directory)
209 {
210 $Locale = new LocaleFile($this->System);
211 $Locale->AnalyzeCode($Directory);
212 $FileList = scandir($this->Dir);
213 foreach ($FileList as $FileName)
214 {
215 if (substr($FileName, -4) == '.php')
216 {
217 $FileLocale = new LocaleFile($this->System);
218 $FileLocale->Dir = $this->Dir;
219 $FileLocale->Load(substr($FileName, 0, -4));
220
221 // Add new
222 foreach ($Locale->Texts->Data as $Index => $Item)
223 if (!array_key_exists($Index, $FileLocale->Texts->Data))
224 $FileLocale->Texts->Data[$Index] = $Item;
225 // Remove old
226 foreach ($FileLocale->Texts->Data as $Index => $Item)
227 if (!array_key_exists($Index, $Locale->Texts->Data))
228 unset($FileLocale->Texts->Data[$Index]);
229 $FileLocale->UpdateToDatabase($this->System->Database, $FileLocale->Texts->Code);
230 $FileName = $this->Dir.'/'.$FileLocale->Texts->Code.'.php';
231 $FileLocale->SaveToFile($FileName);
232 }
233 }
234 $this->CurrentLocale->Load($this->CurrentLocale->Texts->Code);
235 }
236
237 function LoadLocale($Code)
238 {
239 if (array_key_exists($Code, $this->Available))
240 {
241 $this->CurrentLocale->Dir = $this->Dir;
242 $this->CurrentLocale->Load($Code);
243 } else throw new Exception(sprintf(T('Unsupported locale code %s'), $Code));
244 }
245}
246
247// Short named global translation function
248function T($Text, $Group = '')
249{
250 global $GlobalLocaleManager;
251
252 if (isset($GlobalLocaleManager)) return $GlobalLocaleManager->CurrentLocale->Texts->Translate($Text, $Group);
253 else return $Text;
254}
Note: See TracBrowser for help on using the repository browser.