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.

151 lines
3.2 KiB

9 years ago
  1. <?php
  2. define("DQ_ESCAPE", "__DQ__");
  3. function po2php_run(&$argv, &$argc) {
  4. if ($argc!=2) {
  5. print "Usage: ".$argv[0]." <file.po>\n\n";
  6. return;
  7. }
  8. $pofile = $argv[1];
  9. $outfile = dirname($pofile)."/strings.php";
  10. if (strstr($outfile, 'util')) {
  11. $lang = 'en';
  12. } else {
  13. $lang = str_replace('-','_',basename(dirname($pofile)));
  14. }
  15. if (!file_exists($pofile)) {
  16. print "Unable to find '$pofile'\n";
  17. return;
  18. }
  19. print "Out to '$outfile'\n";
  20. $out = "<?php\n\n";
  21. $infile = file($pofile);
  22. $k = "";
  23. $v = "";
  24. $arr = false;
  25. $ink = false;
  26. $inv = false;
  27. $escape_s_exp = '|[^\\\\]\$[a-z]|';
  28. function escape_s($match) {
  29. return str_replace('$','\$',$match[0]);
  30. }
  31. foreach ($infile as $l) {
  32. $l = str_replace('\"', DQ_ESCAPE, $l);
  33. $len = strlen($l);
  34. if ($l[0] == "#") {
  35. $l = "";
  36. }
  37. if (substr($l, 0, 15) == '"Plural-Forms: ') {
  38. $match = array();
  39. preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
  40. $cond = str_replace('n', '$n', $match[2]);
  41. // define plural select function if not already defined
  42. $fnname = 'string_plural_select_' . $lang;
  43. $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
  44. $out .= 'function '. $fnname . '($n){' . "\n";
  45. $out .= ' return ' . $cond . ';' . "\n";
  46. $out .= '}}' . "\n";
  47. }
  48. if ($k != "" && substr($l, 0, 7) == "msgstr ") {
  49. if ($ink) {
  50. $ink = false;
  51. $out .= '$a->strings["' . $k . '"] = ';
  52. }
  53. if ($inv) {
  54. $inv = false;
  55. $out .= '"' . $v . '"';
  56. }
  57. $v = substr($l, 8, $len - 10);
  58. $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
  59. $inv = true;
  60. //$out .= $v;
  61. }
  62. if ($k != "" && substr($l, 0, 7) == "msgstr[") {
  63. if ($ink) {
  64. $ink = false;
  65. $out .= '$a->strings["' . $k . '"] = ';
  66. }
  67. if ($inv) {
  68. $inv = false;
  69. $out .= '"' . $v . '"';
  70. }
  71. if (!$arr) {
  72. $arr=True;
  73. $out .= "array(\n";
  74. }
  75. $match = array();
  76. preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
  77. $out .= "\t"
  78. . preg_replace_callback($escape_s_exp, 'escape_s', $match[1])
  79. . " => "
  80. . preg_replace_callback($escape_s_exp, 'escape_s', $match[2])
  81. . ",\n";
  82. }
  83. if (substr($l, 0, 6) == "msgid_") {
  84. $ink = false;
  85. $out .= '$a->strings["' . $k . '"] = ';
  86. }
  87. if ($ink) {
  88. $k .= trim($l, "\"\r\n");
  89. $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
  90. //$out .= '$a->strings['.$k.'] = ';
  91. }
  92. if (substr($l, 0, 6) == "msgid ") {
  93. if ($inv) {
  94. $inv = false;
  95. $out .= '"' . $v . '"';
  96. }
  97. if ($k != "") {
  98. $out .= ($arr) ? ");\n" : ";\n";
  99. }
  100. $arr = false;
  101. $k = str_replace("msgid ", "", $l);
  102. if ($k != '""') {
  103. $k = trim($k, "\"\r\n");
  104. } else {
  105. $k = "";
  106. }
  107. $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
  108. $ink = true;
  109. }
  110. if ($inv && substr($l, 0, 6) != "msgstr") {
  111. $v .= trim($l, "\"\r\n");
  112. $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
  113. //$out .= '$a->strings['.$k.'] = ';
  114. }
  115. }
  116. if ($inv) {
  117. $inv = false;
  118. $out .= '"' . $v . '"';
  119. }
  120. if ($k != "") {
  121. $out .= ($arr ? ");\n" : ";\n");
  122. }
  123. $out = str_replace(DQ_ESCAPE, '\"', $out);
  124. file_put_contents($outfile, $out);
  125. }
  126. if (array_search(__FILE__, get_included_files()) === 0) {
  127. po2php_run($_SERVER["argv"],$_SERVER["argc"]);
  128. }