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.

122 lines
2.7 KiB

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