3 * Read strings.php file and create messages.po
5 * php utils/php2po.php <path/to/strings.php>
7 * Output to <path/to/messages.po>
10 DEFINE("NORM_REGEXP", "|[\\\]|");
13 if(! class_exists('App')) {
15 public $strings = Array();
20 if ($argc<2 || in_array('-h', $argv) || in_array('--h', $argv)) {
21 print "Usage: ".$argv[0]." [-p <n>] <strings.php>\n\n";
23 print "p\tNumber of plural forms. Default: 2\n";
29 $pofile = dirname($phpfile)."/messages.po";
31 if (!file_exists($phpfile)){
32 print "Unable to find '$phpfile'\n";
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;
44 include_once($phpfile);
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";
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";
62 // search for plural info
67 $_idx = array_search('-p', $argv);
68 if ($_idx !== false) {
69 $lang_pnum = $argv[$_idx+1];
72 $infile = file($phpfile);
73 foreach($infile as $l) {
75 if (startsWith($l, 'function string_plural_select_')) {
76 $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
78 if (startsWith($l, 'return')) {
79 $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '', $l) , ';') );
84 echo "Language: $lang\n";
85 echo "Plural forms: $lang_pnum\n";
86 echo "Plural logic: $lang_logic;\n";
88 $out .= sprintf('"Language: %s\n"', $lang) ."\n";
89 $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) ."\n";
92 print "\nLoading base message.po...";
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) {
103 if (startsWith($l, 'msgstr')) {
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);
116 if (startsWith($l, '"') && $_f==2) {
117 $_mids[count($_mids)-1] .= "\n".$l;
118 //~ print "\t\t+mids: ".print_t($_mids, true);
120 if (startsWith($l, 'msgid_plural ')) {
122 $_mids[] = str_replace('msgid_plural ', '' , $l);
123 //~ print "\t\t mids: ".print_r($_mids, true);
126 if (startsWith($l, '"') && $_f==1) {
128 $_mids[count($_mids)-1] .= "\n".$l;
129 //~ print "\t+mid: $_mid \n";
131 if (startsWith($l, 'msgid ')) {
133 $_mid = str_replace('msgid ', '' , $l);
134 $_mids = array($_mid);
135 //~ print "\t mid: $_mid \n";
137 //~ print "\t\t\t\t$_f\n\n";
141 print "Creating '$pofile'...";
142 // create msgid and msgstr
145 * Get a string and retun a message.po ready text
146 * - replace " with \"
147 * - replace tab char with \t
148 * - manage multiline strings
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);
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];
170 foreach($a->strings as $key=>$str) {
171 $msgid = massage_string($key);
173 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
174 $out .= "#, php-format\n";
176 $msgid = find_original_msgid($msgid);
177 $out .= 'msgid '. $msgid . "\n";
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";
183 $out .= 'msgid_plural '. $msgid . "\n";
184 $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
186 foreach ( $str as $n => $msgstr) {
187 $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
190 $out .= 'msgstr '. massage_string($str) . "\n";
197 file_put_contents($pofile, $out);
202 print "No warnings.\n";