]> git.mxchange.org Git - friendica.git/blob - util/string_translator.php
tooltip for selecting items
[friendica.git] / util / string_translator.php
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
4 <style>
5         textarea { width: 100% }
6         .no { background: #ffdddd; }
7         label { border-bottom: 1px solid #888; }
8 </style>
9 </head>
10 <body>  
11 <?php
12
13 $FRIENDIKA_PATH = dirname(dirname(__FILE__));
14
15 /* find languages */
16 $LANGS=array();
17 $d = dir($FRIENDIKA_PATH."/view");
18 while (false !== ($entry = $d->read())) {
19         if (is_file($d->path."/".$entry."/strings.php")){
20                 $LANGS[] = $entry;
21         }
22
23 }
24 $d->close();
25
26
27 class A{
28         var $strings = Array();
29 }
30
31 function loadstrings($lang = NULL){
32         global $FRIENDIKA_PATH;
33         if (is_null($lang)) {
34                 $path = $FRIENDIKA_PATH."/util/strings.php";
35         } else {
36                 $path = $FRIENDIKA_PATH."/view/$lang/strings.php";
37         }
38         $a = new A();
39         include_once($path);
40         return $a->strings;
41 }
42
43
44 function savestrings($lang, $strings){
45         global $FRIENDIKA_PATH;
46         $path = $FRIENDIKA_PATH."/view/$lang/strings.php";
47         $f = fopen($path,"w");
48         fwrite($f, "<"); fwrite($f, "?php\n");
49         foreach($strings as $k=>$v){
50              $k=str_replace("'","\'", $k);
51              $k=str_replace("\\\\'","\'", $k);
52              $k=str_replace("\n","\\n", $k);
53              $k=str_replace("\r","\\r", $k);
54              $v=str_replace("'","\'", $v);
55              $v=str_replace("\\\\'","\'", $v);
56          $v=str_replace("\n","\\n", $v);
57              $v=str_replace("\r","\\r", $v);
58
59                  fwrite( $f, '$a->strings[\''.$k.'\'] = \''. $v .'\';'."\n" );
60                  #echo '$a->strings[\''.$k.'\'] = \''. $v .'\''."\n" ;
61         }
62     fwrite($f, "?"); fwrite($f, ">\n");
63         fclose($f);
64 }
65
66
67
68 function hexstr($hexstr) {
69   $hexstr = str_replace(' ', '', $hexstr);
70   $hexstr = str_replace('\x', '', $hexstr);
71   $retstr = pack('H*', $hexstr);
72   return $retstr;
73 }
74
75 function strhex($string) {
76   $hexstr = unpack('H*', $string);
77   return array_shift($hexstr);
78 }
79
80
81 echo "<h1>Translator</h1>";
82 echo "<small>Utility to translate <code>string.php</code> file.";
83 echo " Need write permission to language file you want to modify</small>";
84 echo "<p>Installed languages:";
85 foreach($LANGS as $l){
86         echo "<a href='?lang=$l'>$l</a>, ";
87 }
88 echo "</p>";
89
90
91 $strings['en'] = loadstrings();
92
93 if (isset($_GET['lang'])){
94
95         $lang = $_GET['lang'];
96         $strings[$lang] = loadstrings($lang);
97         
98         $n1 = count($strings['en']);
99         $n2 = count($strings[$lang]);
100         
101         echo "<pre>";
102         echo "Translate en to $lang<br>";
103         //echo "Translated $n2 over $n1 strings<br>";
104         echo "</pre><hr/>";
105
106
107
108         if (isset($_POST['save'])){
109                 echo "saving...";
110                 foreach ($_POST as $k=>$v){
111                         if ($k!="save" && $k!="from"){
112                             $k=hexstr($k);
113                                 $strings[$lang][$k] = $v;
114                         }
115                 }
116                 savestrings($lang, $strings[$lang]);
117                 echo "ok.<br>";
118         }
119
120
121
122
123
124         if (!isset($_POST['from'])){
125                 $from=0;
126         } else {
127                 $from = $_POST['from'];
128                 if ($_POST['save']=="Next")
129                         $from += 10;
130                 if ($_POST['save']=="Prev")
131                         $from -= 10;
132         }
133         $count = count($strings['en']);
134         $len = 10;
135         if ($from+$len>$count) $len=$count-$from;
136         $thestrings = array_slice($strings['en'], $from, $len, true);
137         
138
139         
140         echo "<form method='POST'>";
141         
142         if ($from>0)
143     echo "<input type='submit' name='save' id='save' value='Prev'/>";
144   echo "<input type='submit' name='reload' id='reload' value='Reload'/>";   
145   if ($from+$len<$count)
146     echo "<input type='submit' name='save' id='save' value='Next'/>";
147         
148         foreach($thestrings as $k=>$v){
149                 $id = strhex($k);
150                 $translation = $strings[$lang][$k];
151                 
152                 $v=str_replace("\n","\\n", $v);
153             $v=str_replace("\r","\\r", $v);
154                 $translation=str_replace("\n","\\n", $translation);
155             $translation=str_replace("\r","\\r", $translation);
156                 
157                 $istranslate = $translation != '' ? 'yes':'no';
158                 echo "<dl class='$istranslate'>";
159                 echo "<dt><pre><label for='$id'>".htmlspecialchars($v)."</label></pre></dt>";
160                 echo "<dd><textarea id='$id' name='$id'>$translation</textarea></dd>";
161                 echo "</dl>";
162         }
163         
164         
165         echo "<input type='hidden' name='from' value='$from'/>";        
166
167         if ($from>0)
168                 echo "<input type='submit' name='save' id='save' value='Prev'/>";
169         echo "<input type='submit' name='reload' id='reload' value='Reload'/>";         
170         if ($from+$len<$count)
171                 echo "<input type='submit' name='save' id='save' value='Next'/>";
172
173         echo "</form>";
174         
175         
176 }
177 ?>
178 </body>
179 </html>
180