]> git.mxchange.org Git - friendica.git/blob - util/php2po.php
Rewrite util/php2po.php
[friendica.git] / util / php2po.php
1 <?php
2         /**
3          * Read strings.php file and create messages.po
4          *
5          * php utils/php2po.php <path/to/strings.php>
6          * 
7          * Output to <path/to/messages.po>
8          */
9          
10         DEFINE("NORM_REGEXP", "|[\\\]|");
11         
12         
13         if(! class_exists('App')) {
14                 class TmpA {
15                         public $strings = Array();
16                 }
17                 $a = new TmpA();
18         }
19
20         if ($argc<2 || in_array('-h', $argv) || in_array('--h', $argv)) {
21                 print "Usage: ".$argv[0]." [-p <n>] <strings.php>\n\n";
22                 print "Options:\n";
23                 print "p\tNumber of plural forms. Default: 2\n";
24                 print "\n";
25                 return;
26         }
27
28         $phpfile = $argv[1];
29         $pofile = dirname($phpfile)."/messages.po";
30
31         if (!file_exists($phpfile)){
32                 print "Unable to find '$phpfile'\n";
33                 return;
34         }
35
36         // utility functions
37         function startsWith($haystack, $needle) {
38                 // search backwards starting from haystack length characters from the end
39                 return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
40         }
41
42
43         // start !
44         include_once($phpfile);
45
46         $out  = '';
47         $out .= "# FRIENDICA Distributed Social Network\n";
48         $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
49         $out .= "# This file is distributed under the same license as the Friendica package.\n";
50         $out .= "# \n";
51         $out .= 'msgid ""' ."\n";
52         $out .= 'msgstr ""' ."\n";
53         $out .= '"Project-Id-Version: friendica\n"' ."\n";
54         $out .= '"Report-Msgid-Bugs-To: \n"' ."\n";
55         $out .= '"POT-Creation-Date: 2015-09-22 09:58+0200\n"' ."\n";
56         $out .= '"PO-Revision-Date: 2015-10-06 17:43+0000\n"' ."\n";
57         $out .= '"Language-Team: Italian (http://www.transifex.com/Friendica/friendica/language/it/)\n"' ."\n";
58         $out .= '"MIME-Version: 1.0\n"' ."\n";
59         $out .= '"Content-Type: text/plain; charset=UTF-8\n"' ."\n";
60         $out .= '"Content-Transfer-Encoding: 8bit\n"' ."\n";
61         
62         // search for plural info
63         $lang = "";
64         $lang_logic = "";
65         $lang_pnum = 2;
66         
67         $_idx = array_search('-p', $argv);
68         if ($_idx !== false) {
69                 $lang_pnum = $argv[$_idx+1];
70         }
71         
72         $infile = file($phpfile);
73         foreach($infile as $l) {
74                 $l = trim($l);
75                 if  (startsWith($l, 'function string_plural_select_')) {
76                         $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
77                 }
78                 if (startsWith($l, 'return')) {
79                         $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '',  $l) , ';') );
80                         break;
81                 }
82         }
83         
84         echo "Language: $lang\n";
85         echo "Plural forms: $lang_pnum\n";
86         echo "Plural logic: $lang_logic;\n";
87                 
88         $out .= sprintf('"Language: %s\n"', $lang) ."\n";
89         $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic)  ."\n";
90         $out .= "\n";
91
92         print "\nLoading base message.po...";
93         
94         // load base messages.po and extract msgids
95         $base_msgids = array();
96         $norm_base_msgids = array();
97         $base_f = file("util/messages.po") or die("No base messages.po\n");
98         $_f = 0; $_mid = ""; $_mids = array();
99         foreach( $base_f as $l) {
100                 $l = trim($l);
101                 //~ print $l."\n";
102                 
103                 if (startsWith($l, 'msgstr')) {
104                         if ($_mid != '""') {
105                                 $base_msgids[$_mid] =  $_mids;
106                                 $norm_base_msgids[preg_replace(NORM_REGEXP, "", $_mid)] = $_mid;
107                                 //~ print "\t\t\t".$_mid. print_r($base_msgids[$_mid], true);
108                         }
109                         
110                         $_f = 0;
111                         $_mid = "";
112                         $_mids = array();
113                         
114                 }
115                 
116                 if (startsWith($l, '"') && $_f==2) {
117                         $_mids[count($_mids)-1] .= "\n".$l;
118                         //~ print "\t\t+mids: ".print_t($_mids, true);
119                 }
120                 if (startsWith($l, 'msgid_plural ')) {
121                         $_f = 2;
122                         $_mids[] = str_replace('msgid_plural ', '' , $l);
123                         //~ print "\t\t mids: ".print_r($_mids, true);
124                 }
125                 
126                 if (startsWith($l, '"') && $_f==1) {
127                         $_mid .= "\n".$l;
128                         $_mids[count($_mids)-1] .= "\n".$l;
129                         //~ print "\t+mid: $_mid \n";
130                 }
131                 if (startsWith($l, 'msgid ')) {
132                         $_f = 1;
133                         $_mid = str_replace('msgid ', '' , $l);
134                                 $_mids = array($_mid);
135                         //~ print "\t mid: $_mid \n";
136                 }
137                 //~ print "\t\t\t\t$_f\n\n";
138         }
139         
140         print " done\n";
141         print "Creating '$pofile'...";
142         // create msgid and msgstr 
143         
144         /**
145          * Get a string and retun a message.po ready text
146          * - replace " with \"
147          * - replace tab char with \t
148          * - manage multiline strings
149          */
150         function massage_string($str) {
151                 $str = str_replace('\\','\\\\',$str);
152                 $str = str_replace('"','\"',$str);
153                 $str = str_replace("\t",'\t',$str);
154                 $str = str_replace("\n",'\n"'."\n".'"',$str);
155                 if (strpos($str, "\n")!==false  && $str[0]!=='"') $str = '"'."\n".$str;
156                 $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
157                 return sprintf('"%s"', $str);
158         }
159         
160         function find_original_msgid($str) {
161                 global $norm_base_msgids;
162                 $norm_str = preg_replace(NORM_REGEXP, "", $str);
163                 if (array_key_exists($norm_str, $norm_base_msgids)) {
164                         return $norm_base_msgids[$norm_str];
165                 }
166                 return $str;
167         }
168         
169         $warnings = "";
170         foreach($a->strings as $key=>$str) {
171                 $msgid = massage_string($key);
172                 
173                 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
174                         $out .= "#, php-format\n";
175                 }
176                 $msgid = find_original_msgid($msgid);
177                 $out .= 'msgid '. $msgid . "\n";
178                 
179                 if (is_array($str)) {
180                         if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1]))  {
181                                 $out .= 'msgid_plural '. $base_msgids[$msgid][1] . "\n";
182                         } else {
183                                 $out .= 'msgid_plural '. $msgid . "\n";
184                                 $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
185                         }
186                         foreach ( $str as $n => $msgstr) {
187                                 $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
188                         }
189                 } else {
190                         $out .= 'msgstr '. massage_string($str) . "\n";
191                 }
192                 
193                 $out .= "\n";
194         
195         }
196
197         file_put_contents($pofile, $out);
198         
199         print " done\n";
200         
201         if ($warnings=="") {
202                 print "No warnings.\n";
203         } else {
204                 print $warnings;
205         }
206