Changeset 618 for trunk/includes/Locale.php
- Timestamp:
- Dec 4, 2013, 12:39:55 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/Locale.php
r610 r618 5 5 var $Data; 6 6 var $Code; 7 var $Title; 7 8 8 9 function __construct() … … 10 11 $this->Data = array(); 11 12 $this->Code = 'en'; 13 $this->Title = 'English'; 12 14 } 13 15 … … 15 17 { 16 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 } 17 26 } 18 27 … … 22 31 var $Dir; 23 32 24 function __construct() 25 { 33 function __construct($System) 34 { 35 parent::__construct($System); 26 36 $this->Texts = new LocaleText(); 27 }28 29 function Translate($Text)30 {31 if(array_key_exists($Text, $this->Texts->Data) and ($this->Texts->Data[$Text] != ''))32 return($this->Texts->Data[$Text]);33 else return($Text);34 37 } 35 38 … … 41 44 $ClassName = 'LocaleText'.$Language; 42 45 $this->Texts = new $ClassName(); 43 } else $this->Texts = new LocaleText();46 } else throw new Exception('Language file '.$FileName.' not found'); 44 47 $this->Texts->Load(); 45 48 } … … 97 100 ' {'."\n". 98 101 ' $this->Code = \''.$this->Texts->Code.'\';'."\n". 102 ' $this->Title = \''.$this->Texts->Title.'\';'."\n". 99 103 ' $this->Data = array('."\n"; 100 104 foreach($this->Texts->Data as $Index => $Item) … … 153 157 } 154 158 155 $Locale = new Locale(); 159 class 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 } 156 224 157 225 // Short named global function 158 226 function T($Text) 159 227 { 160 global $Locale; 161 162 return($Locale->Translate($Text)); 163 } 228 global $LocaleManager; 229 230 if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text)); 231 else return($Text); 232 }
Note:
See TracChangeset
for help on using the changeset viewer.