]> git.mxchange.org Git - friendica.git/blob - util/php2po.php
Merge pull request #2146 from annando/1512-ostatus-picture-posts
[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: '. date("Y-m-d H:i:sO").'\n"' ."\n";
56         $out .= '"MIME-Version: 1.0\n"' ."\n";
57         $out .= '"Content-Type: text/plain; charset=UTF-8\n"' ."\n";
58         $out .= '"Content-Transfer-Encoding: 8bit\n"' ."\n";
59         
60         // search for plural info
61         $lang = "";
62         $lang_logic = "";
63         $lang_pnum = 2;
64         
65         $_idx = array_search('-p', $argv);
66         if ($_idx !== false) {
67                 $lang_pnum = $argv[$_idx+1];
68         }
69         
70         $infile = file($phpfile);
71         foreach($infile as $l) {
72                 $l = trim($l);
73                 if  (startsWith($l, 'function string_plural_select_')) {
74                         $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
75                 }
76                 if (startsWith($l, 'return')) {
77                         $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '',  $l) , ';') );
78                         break;
79                 }
80         }
81         
82         echo "Language: $lang\n";
83         echo "Plural forms: $lang_pnum\n";
84         echo "Plural logic: $lang_logic;\n";
85                 
86         $out .= sprintf('"Language: %s\n"', $lang) ."\n";
87         $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic)  ."\n";
88         $out .= "\n";
89
90         print "\nLoading base message.po...";
91         
92         // load base messages.po and extract msgids
93         $base_msgids = array();
94         $norm_base_msgids = array();
95         $base_f = file("util/messages.po") or die("No base messages.po\n");
96         $_f = 0; $_mid = ""; $_mids = array();
97         foreach( $base_f as $l) {
98                 $l = trim($l);
99                 //~ print $l."\n";
100                 
101                 if (startsWith($l, 'msgstr')) {
102                         if ($_mid != '""') {
103                                 $base_msgids[$_mid] =  $_mids;
104                                 $norm_base_msgids[preg_replace(NORM_REGEXP, "", $_mid)] = $_mid;
105                                 //~ print "\t\t\t".$_mid. print_r($base_msgids[$_mid], true);
106                         }
107                         
108                         $_f = 0;
109                         $_mid = "";
110                         $_mids = array();
111                         
112                 }
113                 
114                 if (startsWith($l, '"') && $_f==2) {
115                         $_mids[count($_mids)-1] .= "\n".$l;
116                         //~ print "\t\t+mids: ".print_t($_mids, true);
117                 }
118                 if (startsWith($l, 'msgid_plural ')) {
119                         $_f = 2;
120                         $_mids[] = str_replace('msgid_plural ', '' , $l);
121                         //~ print "\t\t mids: ".print_r($_mids, true);
122                 }
123                 
124                 if (startsWith($l, '"') && $_f==1) {
125                         $_mid .= "\n".$l;
126                         $_mids[count($_mids)-1] .= "\n".$l;
127                         //~ print "\t+mid: $_mid \n";
128                 }
129                 if (startsWith($l, 'msgid ')) {
130                         $_f = 1;
131                         $_mid = str_replace('msgid ', '' , $l);
132                                 $_mids = array($_mid);
133                         //~ print "\t mid: $_mid \n";
134                 }
135                 //~ print "\t\t\t\t$_f\n\n";
136         }
137         
138         print " done\n";
139         print "Creating '$pofile'...";
140         // create msgid and msgstr 
141         
142         /**
143          * Get a string and retun a message.po ready text
144          * - replace " with \"
145          * - replace tab char with \t
146          * - manage multiline strings
147          */
148         function massage_string($str) {
149                 $str = str_replace('\\','\\\\',$str);
150                 $str = str_replace('"','\"',$str);
151                 $str = str_replace("\t",'\t',$str);
152                 $str = str_replace("\n",'\n"'."\n".'"',$str);
153                 if (strpos($str, "\n")!==false  && $str[0]!=='"') $str = '"'."\n".$str;
154                 $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
155                 return sprintf('"%s"', $str);
156         }
157         
158         function find_original_msgid($str) {
159                 global $norm_base_msgids;
160                 $norm_str = preg_replace(NORM_REGEXP, "", $str);
161                 if (array_key_exists($norm_str, $norm_base_msgids)) {
162                         return $norm_base_msgids[$norm_str];
163                 }
164                 return $str;
165         }
166         
167         $warnings = "";
168         foreach($a->strings as $key=>$str) {
169                 $msgid = massage_string($key);
170                 
171                 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
172                         $out .= "#, php-format\n";
173                 }
174                 $msgid = find_original_msgid($msgid);
175                 $out .= 'msgid '. $msgid . "\n";
176                 
177                 if (is_array($str)) {
178                         if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1]))  {
179                                 $out .= 'msgid_plural '. $base_msgids[$msgid][1] . "\n";
180                         } else {
181                                 $out .= 'msgid_plural '. $msgid . "\n";
182                                 $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
183                         }
184                         foreach ( $str as $n => $msgstr) {
185                                 $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
186                         }
187                 } else {
188                         $out .= 'msgstr '. massage_string($str) . "\n";
189                 }
190                 
191                 $out .= "\n";
192         
193         }
194
195         file_put_contents($pofile, $out);
196         
197         print " done\n";
198         
199         if ($warnings=="") {
200                 print "No warnings.\n";
201         } else {
202                 print $warnings;
203         }
204