source: trunk/includes/Locale.php@ 608

Last change on this file since 608 was 608, checked in by chronos, 12 years ago
  • Added: Implemented mechanism for update interface translation list from source code. Resulted list is saved to lanugage file and database.
File size: 4.1 KB
Line 
1<?php
2
3class LocaleText
4{
5 var $Data;
6 var $Code;
7
8 function __construct()
9 {
10 $this->Data = array();
11 $this->Code = 'en';
12 }
13
14 function Load()
15 {
16 }
17}
18
19class Locale extends Model
20{
21 var $Texts;
22 var $Dir;
23
24 function __construct()
25 {
26 $this->Texts = new LocaleText();
27 }
28
29 function Translate($Text)
30 {
31 if(array_key_exists($Text, $this->Texts->Data))
32 return($this->Texts->Data[$Text]);
33 else return($Text);
34 }
35
36 function Load($Language)
37 {
38 $FileName = $this->Dir.'/'.$Language.'.php';
39 if(file_exists($FileName)) {
40 include_once($FileName);
41 $ClassName = 'LocaleText'.$Language;
42 $this->Texts = new $ClassName();
43 } else $this->Texts = new LocaleText();
44 $this->Texts->Load();
45 }
46
47 function AnalyzeCode($Path)
48 {
49 // Search for php files
50 $FileList = scandir($Path);
51 foreach($FileList as $FileName)
52 {
53 $FullName = $Path.'/'.$FileName;
54 if(($FileName == '.') or ($FileName == '..')) ; // Skip virtual items
55 else if(substr($FileName, 0, 1) == '.') ; // Skip Linux hidden files
56 else if(is_dir($FullName)) $this->AnalyzeCode($FullName);
57 else if(file_exists($FullName))
58 {
59 if(substr($FullName, -4) == '.php')
60 {
61 $Content = file_get_contents($FullName);
62 // Search occurence of T() function
63 while(strpos($Content, 'T(') !== false)
64 {
65 $Previous = strtolower(substr($Content, strpos($Content, 'T(') - 1, 1));
66 $Content = substr($Content, strpos($Content, 'T(') + 2);
67 $Ord = ord($Previous);
68 //echo($Ord.',');
69 if(!(($Ord >= ord('a')) and ($Ord <= ord('z'))))
70 {
71 // Do for non-alpha previous character
72 $Original = substr($Content, 0, strpos($Content, ')'));
73 $Original2 = '';
74 if((substr($Original, 0, 1) == "'") and (substr($Original, -1, 1) == "'"))
75 $Original2 = substr($Original, 1, -1);
76 if((substr($Original, 0, 1) == '"') and (substr($Original, -1, 1) == '"'))
77 $Original2 = substr($Original, 1, -1);
78 if($Original2 != '')
79 {
80 if(!array_key_exists($Original2, $this->Texts->Data))
81 $this->Texts->Data[$Original2] = '';
82 }
83 }
84 }
85 }
86 }
87 }
88 }
89
90 function SaveToFile($FileName)
91 {
92 $Content = '<?php'."\n".
93 ''."\n".
94 'class LocaleText'.$this->Texts->Code.' extends LocaleText'."\n".
95 '{'."\n".
96 ' function Load()'."\n".
97 ' {'."\n".
98 ' $this->Code = \'cs\';'."\n".
99 ' $this->Data = array('."\n";
100 foreach($this->Texts->Data as $Index => $Item)
101 {
102 $Content .= " '".$Index."' => '".$Item."',\n";
103 }
104 $Content .= ' );'."\n".
105 ' }'."\n".
106 '}'."\n";
107 file_put_contents($FileName, $Content);
108 }
109
110 function LoadFromDatabase($Database, $LanguageId)
111 {
112 $this->Texts->Data = array();
113 $DbResult = $Database->select('Locale', '`Original`, `Translated`', '`Language`='.$LanguageId * 1);
114 while($DbRow = $DbResult->fetch_assoc())
115 $this->Texts->Data[$DbRow['Original']] = $DbRow['Translated'];
116 }
117
118 function SaveToDatabase(Database $Database, $LanguageId)
119 {
120 $Database->delete('Locale', '`Language`='.$LanguageId * 1);
121 foreach($this->Texts->Data as $Index => $Item)
122 $Database->query('INSERT INTO `Locale` (`Language`,`Original`,`Translated`) '.
123 'VALUES('.$LanguageId.','.$Database->quote($Index).','.$Database->quote($Item).')');
124 }
125
126 function UpdateToDatabase(Database $Database, $LanguageId)
127 {
128 foreach($this->Texts->Data as $Index => $Item)
129 {
130 $DbResult = $Database->select('Locale', '*', '`Original` ='.$Database->quote($Index));
131 if($DbResult->num_rows > 0)
132 $Database->update('Locale', '(`Language`='.($LanguageId * 1).') AND '.
133 '(`Original` ='.$Database->quote($Index).')', array('Translated' => $Item));
134 else $Database->insert('Locale', array('Language' => $LanguageId * 1,
135 'Original' => $Index, 'Translated' => $Item));
136 }
137 }
138}
139
140$Locale = new Locale();
141
142// Short named global function
143function T($Text)
144{
145 global $Locale;
146
147 return($Locale->Translate($Text));
148}
Note: See TracBrowser for help on using the repository browser.