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.

203 lines
5.0 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. 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: '. date("Y-m-d H:i:sO").'\n"' ."\n";
  46. $out .= '"MIME-Version: 1.0\n"' ."\n";
  47. $out .= '"Content-Type: text/plain; charset=UTF-8\n"' ."\n";
  48. $out .= '"Content-Transfer-Encoding: 8bit\n"' ."\n";
  49. // search for plural info
  50. $lang = "";
  51. $lang_logic = "";
  52. $lang_pnum = 2;
  53. $_idx = array_search('-p', $argv);
  54. if ($_idx !== false) {
  55. $lang_pnum = $argv[$_idx+1];
  56. }
  57. $infile = file($phpfile);
  58. foreach($infile as $l) {
  59. $l = trim($l);
  60. if (startsWith($l, 'function string_plural_select_')) {
  61. $lang = str_replace( 'function string_plural_select_' , '', str_replace( '($n){','', $l) );
  62. }
  63. if (startsWith($l, 'return')) {
  64. $lang_logic = str_replace( '$', '', trim( str_replace( 'return ' , '', $l) , ';') );
  65. break;
  66. }
  67. }
  68. echo "Language: $lang\n";
  69. echo "Plural forms: $lang_pnum\n";
  70. echo "Plural logic: $lang_logic;\n";
  71. $out .= sprintf('"Language: %s\n"', $lang) ."\n";
  72. $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) ."\n";
  73. $out .= "\n";
  74. print "\nLoading base message.po...";
  75. // load base messages.po and extract msgids
  76. $base_msgids = array();
  77. $norm_base_msgids = array();
  78. $base_f = file("util/messages.po") or die("No base messages.po\n");
  79. $_f = 0; $_mid = ""; $_mids = array();
  80. foreach( $base_f as $l) {
  81. $l = trim($l);
  82. //~ print $l."\n";
  83. if (startsWith($l, 'msgstr')) {
  84. if ($_mid != '""') {
  85. $base_msgids[$_mid] = $_mids;
  86. $norm_base_msgids[preg_replace(NORM_REGEXP, "", $_mid)] = $_mid;
  87. //~ print "\t\t\t".$_mid. print_r($base_msgids[$_mid], true);
  88. }
  89. $_f = 0;
  90. $_mid = "";
  91. $_mids = array();
  92. }
  93. if (startsWith($l, '"') && $_f==2) {
  94. $_mids[count($_mids)-1] .= "\n".$l;
  95. //~ print "\t\t+mids: ".print_t($_mids, true);
  96. }
  97. if (startsWith($l, 'msgid_plural ')) {
  98. $_f = 2;
  99. $_mids[] = str_replace('msgid_plural ', '' , $l);
  100. //~ print "\t\t mids: ".print_r($_mids, true);
  101. }
  102. if (startsWith($l, '"') && $_f==1) {
  103. $_mid .= "\n".$l;
  104. $_mids[count($_mids)-1] .= "\n".$l;
  105. //~ print "\t+mid: $_mid \n";
  106. }
  107. if (startsWith($l, 'msgid ')) {
  108. $_f = 1;
  109. $_mid = str_replace('msgid ', '' , $l);
  110. $_mids = array($_mid);
  111. //~ print "\t mid: $_mid \n";
  112. }
  113. //~ print "\t\t\t\t$_f\n\n";
  114. }
  115. print " done\n";
  116. print "Creating '$pofile'...";
  117. // create msgid and msgstr
  118. /**
  119. * Get a string and retun a message.po ready text
  120. * - replace " with \"
  121. * - replace tab char with \t
  122. * - manage multiline strings
  123. */
  124. function massage_string($str) {
  125. $str = str_replace('\\','\\\\',$str);
  126. $str = str_replace('"','\"',$str);
  127. $str = str_replace("\t",'\t',$str);
  128. $str = str_replace("\n",'\n"'."\n".'"',$str);
  129. if (strpos($str, "\n")!==false && $str[0]!=='"') $str = '"'."\n".$str;
  130. $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
  131. return sprintf('"%s"', $str);
  132. }
  133. function find_original_msgid($str) {
  134. global $norm_base_msgids;
  135. $norm_str = preg_replace(NORM_REGEXP, "", $str);
  136. if (array_key_exists($norm_str, $norm_base_msgids)) {
  137. return $norm_base_msgids[$norm_str];
  138. }
  139. return $str;
  140. }
  141. $warnings = "";
  142. foreach($a->strings as $key=>$str) {
  143. $msgid = massage_string($key);
  144. if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
  145. $out .= "#, php-format\n";
  146. }
  147. $msgid = find_original_msgid($msgid);
  148. $out .= 'msgid '. $msgid . "\n";
  149. if (is_array($str)) {
  150. if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1])) {
  151. $out .= 'msgid_plural '. $base_msgids[$msgid][1] . "\n";
  152. } else {
  153. $out .= 'msgid_plural '. $msgid . "\n";
  154. $warnings .= "[W] No source plural form for msgid:\n". str_replace("\n","\n\t", $msgid) . "\n\n";
  155. }
  156. foreach ( $str as $n => $msgstr) {
  157. $out .= 'msgstr['.$n.'] '. massage_string($msgstr) . "\n";
  158. }
  159. } else {
  160. $out .= 'msgstr '. massage_string($str) . "\n";
  161. }
  162. $out .= "\n";
  163. }
  164. file_put_contents($pofile, $out);
  165. print " done\n";
  166. if ($warnings=="") {
  167. print "No warnings.\n";
  168. } else {
  169. print $warnings;
  170. }