source: subversion/trunk/translator/func.php @ 2178

Last change on this file since 2178 was 2178, checked in by thomasb, 4 years ago

Use update_from_svn() to get localization files for stats;
Store stats file in (writeable) localization folder;
Increase cache time to 12 hours;
Some style tweaks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date Revision
File size: 5.2 KB
Line 
1<?php
2
3// define locations here
4define('LANGDIR', './localization/');  // langdir location
5define('LABELS', 'labels.inc');   // name of the labels file
6define('MESSAGES', 'messages.inc'); // name of the messages file
7define('ORIGINAL', 'en_US'); // always up-to-date language
8
9// ---- EOF conf ---- //
10
11function get_input_value($fname, $html = false)
12{
13        $value = !empty($_REQUEST[$fname]) ? $_REQUEST[$fname] : "";
14
15        // strip slashes if magic_quotes enabled
16        if ((bool)get_magic_quotes_gpc())
17                $value = stripslashes($value);
18
19        // remove HTML tags if not allowed
20        if (!$html)
21                $value = strip_tags($value);
22
23        return $value;
24 }
25
26
27function update_from_svn($lang, $file)
28{
29        $host = "svn.roundcube.net";
30        $base = "/trunk/roundcubemail/program/localization/";
31       
32        $lang_dir = $lang != "" ? $lang."/" : "";
33        $lang_prefix = $lang != "" ? $lang."_" : "";
34
35        // check if original file is up to date
36        $stat = @stat(LANGDIR."$lang_prefix$file");
37        if (!$stat || ($stat['mtime'] < time() - 3600))
38        {
39                if ($fp = fsockopen("ssl://$host", 443, $err, $err_str))
40                        fwrite($fp, "GET $base$lang_dir$file HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n");
41
42                $headers = true;
43                if ($fp && !$err && ($fl = @fopen(LANGDIR."$lang_prefix$file", 'w')))
44                {
45                        echo '<!-- Update from SVN: ' . $lang_dir.$file . "-->\n";
46                        while (!feof($fp))
47                        {
48                                $line = fgets($fp, 4096);
49                                if (trim($line) == "")
50                                        $headers = false;
51                                if (!$headers)
52                                        fwrite($fl, $line);
53                        }
54
55                        fclose($fp);
56                        fclose($fl);
57                }
58        }
59
60        if (is_file(LANGDIR."$lang_prefix$file"))
61                return LANGDIR."$lang_prefix$file";
62        else
63                return false;
64}
65
66
67function lang_selection($lang)
68{
69        include(update_from_svn('', 'index.inc'));
70
71        $out = "<select name=\"lang\">\n<option value=\"_NEW_\">New Language</option>\n";
72        foreach ((array)$rcube_languages as $l_key => $l_value)
73        {
74                if ($l_key == ORIGINAL)
75                        continue;
76
77                $out .= '<option value="'.$l_key.'"';
78                if ($l_key == $lang) $out .= ' selected';
79                $out .= '>' . htmlspecialchars($l_value) . "</option>\n";
80        }
81        $out .= "</select>";
82       
83        return $out;
84}
85
86
87function build_localization($lang, $file)
88{
89        global $orig_values, $edit_values;
90       
91        $array = $file == "messages.inc" ? '$messages' : '$labels';
92        $fpath = $lang != "_NEW_" ? update_from_svn($lang, $file) : 'nope';
93       
94        if (!strstr($fpath, 'http') && !is_file($fpath))
95                $fpath = realpath("./$file");
96       
97        $out = "";
98        if ($fpath && ($fp = fopen($fpath, 'r')))
99        {
100                while (!feof($fp))
101                {
102                        $line = trim(fgets($fp));
103                       
104                        if (empty($line) && empty($out))
105                                continue;
106                               
107                        $out .= str_replace('#lang', $lang, $line) . "\n";
108                       
109                        // reached begin of php array
110                        if (strstr($line, $array))
111                                break;
112                }
113        }
114        else
115        {
116                $out .= '<?php' . "\n";
117                $out .= $array . " = array();\n";
118        }
119
120        foreach((array)$orig_values as $t_key => $t_value)
121        {
122                $t_value = get_input_value('t_'.$t_key, true);
123                if (empty($t_value) && isset($edit_values[$t_key]))
124                        $t_value = $edit_values[$t_key];
125                if (!empty($t_value))
126                        $out .= $array . "['$t_key'] = '" . addcslashes($t_value, "'") . "';\n";
127        }
128       
129        $out .= "\n?>\n";
130        return $out;
131}
132
133function count_lines($lang, $file)
134{
135        $count = 0;
136        $lines = array();
137       
138        if ($filename = update_from_svn($lang, $file))
139                $lines = file($filename);
140       
141        // count lines starting with $ ($labels/$messages)
142        foreach($lines as $line)
143                if(strpos($line, '$') === 0)
144                        $count++;
145
146        return $count;
147}
148
149function localization_stats($force = false)
150{
151        $cachefile = LANGDIR . 'langstats.txt';
152        // use saved file (cache)
153        if (!$force && file_exists($cachefile))
154                if (filemtime($cachefile) + 60*60*12 > time())
155                        return file_get_contents($cachefile);
156
157        $us_count = count_lines(ORIGINAL, LABELS);
158        $us_count += count_lines(ORIGINAL, MESSAGES);
159
160        include(LANGDIR . 'index.inc');
161       
162        $i = 0;
163        foreach ((array)$rcube_languages as $l_key => $l_value)
164        {
165                if ($l_key == ORIGINAL)
166                        continue;
167
168                $count = count_lines($l_key, LABELS);
169                $count += count_lines($l_key, MESSAGES);
170                $percent = ($count * 100) / $us_count;
171               
172                $rows[] = sprintf("<tr><td class=\"lang%s\">%s</td><td class=\"percent%s\">%.1f %%</td></tr>\n",
173                        ($i%2 ? ' zebra' : ''), htmlspecialchars($l_value), ($i%2 ? ' zebra' : ''), $percent);
174               
175                $i++;
176        }
177
178        // format output tables
179        $result = '<table class="stats"><tr><td><table class="langstats">';
180        for($i=0; $i<count($rows)/2; $i++)
181                $result .= $rows[$i];
182        $result .= '</table></td><td><table class="langstats">';
183        for(; $i<count($rows); $i++)
184                $result .= $rows[$i];
185        $result .= '</td></tr></table>';
186       
187        // save to cache file
188        file_put_contents($cachefile, $result);
189       
190        return $result;
191}
192
193// -------- EOF func --------//
194
195$header = array();
196$orig_values = array();
197$labels = $messages = null;
198
199$file = get_input_value('file');
200$lang = get_input_value('lang');
201$translated = !empty($_REQUEST['trans']);
202
203// read reference file
204if ($file && $lang)
205        include(update_from_svn(ORIGINAL, $file));
206
207if ($file == 'labels.inc' && $labels)
208        $orig_values = $labels;
209else if ($file == 'messages.inc' && $messages)
210        $orig_values = $messages;
211
212unset($labels, $messages);
213
214// read current localization file
215if (!empty($lang) && !empty($file))
216{
217        if ($lang != "_NEW_")
218                @include(update_from_svn($lang, $file));
219       
220        if (!empty($labels))
221                $edit_values = $labels;
222        else if (!empty($messages))
223                $edit_values = $messages;
224        else
225                $edit_values = array();
226               
227        unset($labels, $messages);
228}
229
230?>
Note: See TracBrowser for help on using the repository browser.