Offline Server  1.6
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
po2js.php
Go to the documentation of this file.
1 <?php
2 /**
3  * translate mo file to je file obejct
4  * @author Anakeen
5  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
6  */
7 
8 class po2js {
9  public $pofile="";
10  public $entry=array();
11  private $encoding='utf-8';
12  function __construct($pofile) {
13  $this->pofile=$pofile;
14  }
15  public function parseEntry(&$out) {
16  $out = preg_replace(
17  "/(?m)\[BLOCK\s*([^\]]*)\](.*?)\[ENDBLOCK\s*\\1\]/se",
18  "\$this->memoEntry('\\1','\\2')",
19  $out);
20  }
21  public static function trimquote($s) {
22  return trim($s,'"');
23  }
24 
25  public function memoEntry($key,$text) {
26  $tkey=explode("\n",$key);
27  $ttext=explode("\n","$text");
28  $key=trim(implode("\n",array_map('po2js::trimquote',$tkey)));
29  $text=trim(implode("\n",array_map('po2js::trimquote',$ttext)));
30  // print sprintf("msgid=[[[%s]]] msgstr=[[[%s]]]\n", $key, $text);
31  if ($key && $text) $this->entry[$key]=$text;
32  else if ($key=="") {
33  if (stristr($text,"charset=ISO-8859") !== false) {
34  $this->encoding='iso';
35  }
36  }
37  }
38 
39 
40  public function po2array() {
41  if (file_exists($this->pofile)) {
42  $pocontent=file_get_contents($this->pofile);
43  if ($pocontent) {
44  $pocontent.="\n\n";
45  preg_match_all('/^msgid (?P<msgid>".*?)msgstr (?P<msgstr>".*?")\n\n/ms', $pocontent, $matches, PREG_SET_ORDER);
46  foreach($matches as $m) {
47  // $msgid = str_replace("\n", " ", $m['msgid']);
48  //$msgstr = str_replace("\n", " ", $m['msgstr']);
49  $this->memoEntry($m['msgid'], $m['msgstr']);
50  }
51  }
52  }
53 
54  }
55 
56  public function po2json() {
57  $this->po2array();
58  if (count($this->entry) > 0) {
59  $js=json_encode($this->entry);
60  if ($this->encoding=="iso") $js=utf8_encode($js);
61  return $js;
62  } else return "";
63 
64  }
65 }
66 
67 // mettre 2 retour chariots en fin de ligne
68 
69 $c=new po2js($argv[1]);
70 $c->po2array();
71 print $c->po2json();
72 ?>
$entry
Definition: po2js.php:10
memoEntry($key, $text)
Definition: po2js.php:25
static trimquote($s)
Definition: po2js.php:21
po2json()
Definition: po2js.php:56
Definition: po2js.php:8
$pofile
Definition: po2js.php:9
__construct($pofile)
Definition: po2js.php:12
$c
Definition: po2js.php:69
parseEntry(&$out)
Definition: po2js.php:15
po2array()
Definition: po2js.php:40
← centre documentaire © anakeen - published under CC License - Dynacase