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