Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
5.4 KiB

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