Changeset 816 for trunk/includes/Locale.php
- Timestamp:
- Feb 22, 2015, 11:20:50 PM (11 years ago)
- File:
-
- 1 edited
-
trunk/includes/Locale.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/Locale.php
r815 r816 3 3 class LocaleText 4 4 { 5 var $Data;6 var $Code;7 var $Title;5 var $Data; 6 var $Code; 7 var $Title; 8 8 9 9 function __construct() 10 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 }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 26 } 27 27 28 28 class LocaleFile extends Model 29 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 files30 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 53 $FileList = scandir($Path); 54 54 foreach($FileList as $FileName) 55 55 { 56 $FullName = $Path.'/'.$FileName;57 if(($FileName == '.') or ($FileName == '..')) ; // Skip virtual items58 else if(substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files59 else if(is_dir($FullName)) $this->AnalyzeCode($FullName);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 60 else if(file_exists($FullName)) 61 61 { 62 62 if(substr($FullName, -4) == '.php') 63 63 { 64 $Content = file_get_contents($FullName);65 // Search occurence of T() function66 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 character75 $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 }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 88 } 89 89 } … … 93 93 function SaveToFile($FileName) 94 94 { 95 $Content = '<?php'."\n".95 $Content = '<?php'."\n". 96 96 ''."\n". 97 97 'class LocaleText'.$this->Texts->Code.' extends LocaleText'."\n". 98 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".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 110 '}'."\n"; 111 111 file_put_contents($FileName, $Content); … … 114 114 function LoadFromDatabase($Database, $LangCode) 115 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 }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 125 } 126 126 127 127 function SaveToDatabase(Database $Database, $LangCode) 128 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 }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 138 } 139 139 140 140 function UpdateToDatabase(Database $Database, $LangCode) 141 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 '.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 151 '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item)); 152 else $Database->insert('Locale', array('Language' => $Language['Id'],152 else $Database->insert('Locale', array('Language' => $Language['Id'], 153 153 'Original' => $Index, 'Translated' => $Item)); 154 154 } 155 }155 } 156 156 } 157 157 } … … 159 159 class LocaleManager extends Model 160 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 LocaleFile($System);170 }171 172 function LoadAvailable()173 {174 $this->Available = array();175 $FileList = scandir($this->Dir);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 LocaleFile($System); 170 } 171 172 function LoadAvailable() 173 { 174 $this->Available = array(); 175 $FileList = scandir($this->Dir); 176 176 foreach($FileList as $FileName) 177 177 { 178 if(substr($FileName, -4) == '.php')179 {180 $Code = substr($FileName, 0, -4);181 $Locale = new LocaleFile($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 LocaleFile($this->System);192 $Locale->AnalyzeCode($Directory);193 $FileList = scandir($this->Dir);178 if(substr($FileName, -4) == '.php') 179 { 180 $Code = substr($FileName, 0, -4); 181 $Locale = new LocaleFile($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 LocaleFile($this->System); 192 $Locale->AnalyzeCode($Directory); 193 $FileList = scandir($this->Dir); 194 194 foreach($FileList as $FileName) 195 195 { 196 if(substr($FileName, -4) == '.php')197 {198 $FileLocale = new LocaleFile($this->System);199 $FileLocale->Dir = $this->Dir;200 $FileLocale->Load(substr($FileName, 0, -4));201 202 // Add new203 foreach($Locale->Texts->Data as $Index => $Item)204 if(!array_key_exists($Index, $FileLocale->Texts->Data))205 $FileLocale->Texts->Data[$Index] = $Item;206 // Remove old207 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 {196 if(substr($FileName, -4) == '.php') 197 { 198 $FileLocale = new LocaleFile($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 220 $this->CurrentLocale->Dir = $this->Dir; 221 $this->CurrentLocale->Load($Code);222 }221 $this->CurrentLocale->Load($Code); 222 } 223 223 } 224 224 … … 226 226 function T($Text) 227 227 { 228 global $LocaleManager;229 230 if(isset($LocaleManager)) return($LocaleManager->CurrentLocale->Texts->Translate($Text));231 else return($Text);232 } 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.
