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