+use Friendica\App;
+
+DEFINE("NORM_REGEXP", "|[\\\]|");
+
+if(! class_exists('App')) {
+ class TmpA {
+ public $strings = [];
+ }
+ $a = new TmpA();
+}
+
+if ($argc<2 || in_array('-h', $argv) || in_array('--h', $argv)) {
+ print "Usage: ".$argv[0]." [-p <n>] <strings.php>\n\n";
+ print "Options:\n";
+ print "p\tNumber of plural forms. Default: 2\n";
+ print "\n";
+ return;
+}
+
+$phpfile = $argv[1];
+$pofile = dirname($phpfile)."/messages.po";
+
+if (!file_exists($phpfile)){
+ print "Unable to find '$phpfile'\n";
+ return;
+}
+
+// utility functions
+function startsWith($haystack, $needle) {
+ // search backwards starting from haystack length characters from the end
+ return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
+}
+
+
+// start !
+include_once($phpfile);
+
+$out = '';
+$out .= "# FRIENDICA Distributed Social Network\n";
+$out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
+$out .= "# This file is distributed under the same license as the Friendica package.\n";
+$out .= "# \n";
+$out .= 'msgid ""' ."\n";
+$out .= 'msgstr ""' ."\n";
+$out .= '"Project-Id-Version: friendica\n"' ."\n";
+$out .= '"Report-Msgid-Bugs-To: \n"' ."\n";
+$out .= '"POT-Creation-Date: '. date("Y-m-d H:i:sO").'\n"' ."\n";
+$out .= '"MIME-Version: 1.0\n"' ."\n";
+$out .= '"Content-Type: text/plain; charset=UTF-8\n"' ."\n";
+$out .= '"Content-Transfer-Encoding: 8bit\n"' ."\n";
+
+// search for plural info
+$lang = "";
+$lang_logic = "";
+$lang_pnum = 2;
+
+$_idx = array_search('-p', $argv);
+if ($_idx !== false) {
+ $lang_pnum = $argv[$_idx+1];
+}
+
+$infile = file($phpfile);
+foreach($infile as $l) {
+ $l = trim($l);
+ if (startsWith($l, 'function string_plural_select_')) {
+ $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
+ }
+ if (startsWith($l, 'return')) {
+ $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '', $l) , ';') );
+ break;
+ }
+}
+
+echo "Language: $lang\n";
+echo "Plural forms: $lang_pnum\n";
+echo "Plural logic: $lang_logic;\n";
+
+$out .= sprintf('"Language: %s\n"', $lang) ."\n";
+$out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) ."\n";
+$out .= "\n";
+
+print "\nLoading base message.po...";
+
+// load base messages.po and extract msgids
+$base_msgids = [];
+$norm_base_msgids = [];
+$base_f = file("util/messages.po") || die("No base messages.po\n");
+$_f = 0; $_mid = ""; $_mids = [];
+foreach( $base_f as $l) {
+ $l = trim($l);
+ //~ print $l."\n";
+
+ if (startsWith($l, 'msgstr')) {
+ if ($_mid != '""') {
+ $base_msgids[$_mid] = $_mids;
+ $norm_base_msgids[preg_replace(NORM_REGEXP, "", $_mid)] = $_mid;
+ //~ print "\t\t\t".$_mid. print_r($base_msgids[$_mid], true);