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: '. 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";
60 // search for plural info
65 $_idx = array_search('-p', $argv);
66 if ($_idx !== false) {
67 $lang_pnum = $argv[$_idx+1];
70 $infile = file($phpfile);
71 foreach($infile as $l) {
73 if (startsWith($l, 'function string_plural_select_')) {
74 $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
76 if (startsWith($l, 'return')) {
77 $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '', $l) , ';') );
82 echo "Language: $lang\n";
83 echo "Plural forms: $lang_pnum\n";
84 echo "Plural logic: $lang_logic;\n";
86 $out .= sprintf('"Language: %s\n"', $lang) ."\n";
87 $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) ."\n";
90 print "\nLoading base message.po...";
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) {
101 if (startsWith($l, 'msgstr')) {
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);
114 if (startsWith($l, '"') && $_f==2) {
115 $_mids[count($_mids)-1] .= "\n".$l;
116 //~ print "\t\t+mids: ".print_t($_mids, true);
118 if (startsWith($l, 'msgid_plural ')) {
120 $_mids[] = str_replace('msgid_plural ', '' , $l);
121 //~ print "\t\t mids: ".print_r($_mids, true);
124 if (startsWith($l, '"') && $_f==1) {
126 $_mids[count($_mids)-1] .= "\n".$l;
127 //~ print "\t+mid: $_mid \n";
129 if (startsWith($l, 'msgid ')) {
131 $_mid = str_replace('msgid ', '' , $l);
132 $_mids = array($_mid);
133 //~ print "\t mid: $_mid \n";
135 //~ print "\t\t\t\t$_f\n\n";
139 print "Creating '$pofile'...";
140 // create msgid and msgstr
143 * Get a string and retun a message.po ready text
144 * - replace " with \"
145 * - replace tab char with \t
146 * - manage multiline strings
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);
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];
168 foreach($a->strings as $key=>$str) {
169 $msgid = massage_string($key);
171 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
172 $out .= "#, php-format\n";
174 $msgid = find_original_msgid($msgid);
175 $out .= 'msgid '. $msgid . "\n";
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";
181 $out .= 'msgid_plural '. $msgid . "\n";
182 $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
184 foreach ( $str as $n => $msgstr) {
185 $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
188 $out .= 'msgstr '. massage_string($str) . "\n";
195 file_put_contents($pofile, $out);
200 print "No warnings.\n";