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.

204 lines
5.1 KiB

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