Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
buildTools/po2js.php
Go to the documentation of this file.
1 <?php
2 /**
3  * translate mo file to json
4  * @author Anakeen
5  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
6  */
7 
8 $c = new Po2js($argv[1]);
9 print $c->po2json();
10 /**
11  * Convert a PO file to json string
12  */
13 class Po2js
14 {
15 
16  protected $pofile = "";
17  protected $entries = array();
18  protected $encoding = 'utf-8';
19 
20  /**
21  * Construct the object
22  *
23  * @param string $pofile path to the po file
24  * @throws Exception
25  */
26  public function __construct($pofile)
27  {
28  if (file_exists($pofile)) {
29  $this->pofile = $pofile;
30  } else {
31  throw new Exception("PO file ($pofile) doesn't exist.");
32  }
33 
34  }
35 
36  /**
37  * Convert the current PO file to a json string
38  *
39  * JSON contains an object where key are po key and content po translation
40  * if there is no translation return an empty string
41  *
42  * @return string
43  */
44  public function po2json()
45  {
46  $this->po2array();
47  if (!empty($this->entries)) {
48  $js = json_encode($this->entries);
49  if ($this->encoding === "iso") {
50  $js = utf8_encode($js);
51  }
52  return $js;
53  } else {
54  return "";
55  }
56  }
57 
58  /**
59  * Extract PO entries an store them
60  *
61  * @throws Exception
62  */
63  protected function po2array()
64  {
65  if (file_exists($this->pofile)) {
66  $pocontent = file_get_contents($this->pofile);
67  if ($pocontent !== false) {
68  $pocontent .= "\n\n";
69  preg_match_all('/^(msgctxt (?P<msgctxt>".*?))?msgid (?P<msgid>".*?)msgstr (?P<msgstr>".*?")\n\n/ms',
70  $pocontent, $matches, PREG_SET_ORDER);
71  foreach ($matches as $m) {
72  $this->memoEntry($m['msgid'], $m['msgstr'], $m['msgctxt']);
73  }
74  } else {
75  throw new Exception("PO file ({$this->pofile}) is not readable.");
76  }
77  } else {
78  throw new Exception("PO file ({$this->pofile}) doesn't exist.");
79  }
80  }
81 
82  /**
83  * Clean a key and a translation and add them to $this->entries
84  * @param $key
85  * @param $text
86  */
87  protected function memoEntry($key, $text, $ctxt='')
88  {
89  $tkey = explode("\n", $key);
90  $ttext = explode("\n", $text);
91  $tctxt = explode("\n", $ctxt);
92  $key = trim(implode("\n", array_map('Po2js::trimquote', $tkey)));
93  $text = trim(implode("\n", array_map('Po2js::trimquote', $ttext)));
94  $ctxt = trim(implode("\n", array_map('Po2js::trimquote', $tctxt)));
95  if ($key && $text) {
96  if ($ctxt) {
97  $this->entries["_msgctxt_"][$ctxt][$key] = $text;
98  } else {
99  $this->entries[$key] = $text;
100  }
101  } else if ($key == "") {
102  if (stristr($text, "charset=ISO-8859") !== false) {
103  $this->encoding = 'iso';
104  }
105  }
106  }
107 
108  protected static function trimquote($s)
109  {
110  return trim($s, '"');
111  }
112 
113 }
static trimquote($s)
memoEntry($key, $text, $ctxt='')
__construct($pofile)
if($famId) $s
print
Definition: checklist.php:49
← centre documentaire © anakeen