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>
12 DEFINE("NORM_REGEXP", "|[\\\]|");
14 if(! class_exists('App')) {
16 public $strings = Array();
21 if ($argc<2 || in_array('-h', $argv) || in_array('--h', $argv)) {
22 print "Usage: ".$argv[0]." [-p <n>] <strings.php>\n\n";
24 print "p\tNumber of plural forms. Default: 2\n";
30 $pofile = dirname($phpfile)."/messages.po";
32 if (!file_exists($phpfile)){
33 print "Unable to find '$phpfile'\n";
38 function startsWith($haystack, $needle) {
39 // search backwards starting from haystack length characters from the end
40 return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
45 include_once($phpfile);
48 $out .= "# FRIENDICA Distributed Social Network\n";
49 $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
50 $out .= "# This file is distributed under the same license as the Friendica package.\n";
52 $out .= 'msgid ""' ."\n";
53 $out .= 'msgstr ""' ."\n";
54 $out .= '"Project-Id-Version: friendica\n"' ."\n";
55 $out .= '"Report-Msgid-Bugs-To: \n"' ."\n";
56 $out .= '"POT-Creation-Date: '. date("Y-m-d H:i:sO").'\n"' ."\n";
57 $out .= '"MIME-Version: 1.0\n"' ."\n";
58 $out .= '"Content-Type: text/plain; charset=UTF-8\n"' ."\n";
59 $out .= '"Content-Transfer-Encoding: 8bit\n"' ."\n";
61 // search for plural info
66 $_idx = array_search('-p', $argv);
67 if ($_idx !== false) {
68 $lang_pnum = $argv[$_idx+1];
71 $infile = file($phpfile);
72 foreach($infile as $l) {
74 if (startsWith($l, 'function string_plural_select_')) {
75 $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
77 if (startsWith($l, 'return')) {
78 $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '', $l) , ';') );
83 echo "Language: $lang\n";
84 echo "Plural forms: $lang_pnum\n";
85 echo "Plural logic: $lang_logic;\n";
87 $out .= sprintf('"Language: %s\n"', $lang) ."\n";
88 $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) ."\n";
91 print "\nLoading base message.po...";
93 // load base messages.po and extract msgids
94 $base_msgids = array();
95 $norm_base_msgids = array();
96 $base_f = file("util/messages.po") or die("No base messages.po\n");
97 $_f = 0; $_mid = ""; $_mids = array();
98 foreach( $base_f as $l) {
102 if (startsWith($l, 'msgstr')) {
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);
115 if (startsWith($l, '"') && $_f==2) {
116 $_mids[count($_mids)-1] .= "\n".$l;
117 //~ print "\t\t+mids: ".print_t($_mids, true);
119 if (startsWith($l, 'msgid_plural ')) {
121 $_mids[] = str_replace('msgid_plural ', '' , $l);
122 //~ print "\t\t mids: ".print_r($_mids, true);
125 if (startsWith($l, '"') && $_f==1) {
127 $_mids[count($_mids)-1] .= "\n".$l;
128 //~ print "\t+mid: $_mid \n";
130 if (startsWith($l, 'msgid ')) {
132 $_mid = str_replace('msgid ', '' , $l);
133 $_mids = array($_mid);
134 //~ print "\t mid: $_mid \n";
136 //~ print "\t\t\t\t$_f\n\n";
140 print "Creating '$pofile'...";
141 // create msgid and msgstr
144 * Get a string and retun a message.po ready text
145 * - replace " with \"
146 * - replace tab char with \t
147 * - manage multiline strings
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);
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];
169 foreach($a->strings as $key=>$str) {
170 $msgid = massage_string($key);
172 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
173 $out .= "#, php-format\n";
175 $msgid = find_original_msgid($msgid);
176 $out .= 'msgid '. $msgid . "\n";
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";
182 $out .= 'msgid_plural '. $msgid . "\n";
183 $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
185 foreach ( $str as $n => $msgstr) {
186 $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
189 $out .= 'msgstr '. massage_string($str) . "\n";
196 file_put_contents($pofile, $out);
201 print "No warnings.\n";