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.

128 lines
2.9 KiB

9 years ago
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. if (!file_exists($pofile)){
  15. print "Unable to find '$pofile'\n";
  16. return;
  17. }
  18. print "Out to '$outfile'\n";
  19. $out="<?php\n\n";
  20. $infile = file($pofile);
  21. $k="";
  22. $v="";
  23. $arr = False;
  24. $ink = False;
  25. $inv = False;
  26. $escape_s_exp = '|[^\\\\]\$[a-z]|';
  27. function escape_s($match){
  28. return str_replace('$','\$',$match[0]);
  29. }
  30. foreach ($infile as $l) {
  31. $l = str_replace('\"', DQ_ESCAPE, $l);
  32. $len = strlen($l);
  33. if ($l[0]=="#") $l="";
  34. if (substr($l,0,15)=='"Plural-Forms: '){
  35. $match=Array();
  36. preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
  37. $cond = str_replace('n','$n',$match[2]);
  38. // define plural select function if not already defined
  39. $fnname = 'string_plural_select_' . $lang;
  40. $out .= 'if(! function_exists("'.$fnname.'")) {'."\n";
  41. $out .= 'function '. $fnname . '($n){'."\n";
  42. $out .= ' return '.$cond.';'."\n";
  43. $out .= '}}'."\n";
  44. }
  45. if ($k!="" && substr($l,0,7)=="msgstr "){
  46. if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
  47. if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
  48. $v = substr($l,8,$len-10);
  49. $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
  50. $inv = True;
  51. //$out .= $v;
  52. }
  53. if ($k!="" && substr($l,0,7)=="msgstr["){
  54. if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
  55. if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
  56. if (!$arr) {
  57. $arr=True;
  58. $out .= "array(\n";
  59. }
  60. $match=Array();
  61. preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
  62. $out .= "\t".
  63. preg_replace_callback($escape_s_exp,'escape_s',$match[1])
  64. ." => "
  65. .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
  66. }
  67. if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
  68. if ($ink) {
  69. $k .= trim($l,"\"\r\n");
  70. $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
  71. //$out .= '$a->strings['.$k.'] = ';
  72. }
  73. if (substr($l,0,6)=="msgid "){
  74. if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
  75. if ($k!="") $out .= $arr?");\n":";\n";
  76. $arr=False;
  77. $k = str_replace("msgid ","",$l);
  78. if ($k != '""' ) {
  79. $k = trim($k,"\"\r\n");
  80. } else {
  81. $k = "";
  82. }
  83. $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
  84. $ink = True;
  85. }
  86. if ($inv && substr($l,0,6)!="msgstr") {
  87. $v .= trim($l,"\"\r\n");
  88. $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
  89. //$out .= '$a->strings['.$k.'] = ';
  90. }
  91. }
  92. if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
  93. if ($k!="") $out .= $arr?");\n":";\n";
  94. $out = str_replace(DQ_ESCAPE, '\"', $out);
  95. file_put_contents($outfile, $out);
  96. }
  97. if (array_search(__file__,get_included_files())===0){
  98. po2php_run($_SERVER["argv"],$_SERVER["argc"]);
  99. }