Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
xgettextPhp.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5 */
6 
7 require_once "xgettextCommon.php";
9 {
10  protected $tokenFile = array();
11  protected function extractExtraLabel($inputFile)
12  {
13  $this->tokenFile = token_get_all(file_get_contents($inputFile));
14  // extract searchLabel comment
15  $filteredLabel = preg_filter("/.*@(searchLabel)\\s+([^\\n]+)\\n.*/s", "\\2", array_map(function ($t)
16  {
17  return $t[1];
18  }
19  , array_filter($this->tokenFile, function ($t)
20  {
21  return ($t[0] === T_DOC_COMMENT);
22  })));
23 
24  return $filteredLabel;
25  }
26  protected function extractSharp()
27  {
28  // extract searchLabel comment
29  $filteredLabel = array();
30  // extract sharp comment
31  $filteredSharp = array_filter($this->tokenFile, function ($t)
32  {
33  return ($t[0] === T_COMMENT && $t[1][0] === '#');
34  });
35  foreach ($filteredSharp as $sharpComment) {
36  $sharpComment[1][0] = ' ';
37  if (preg_match_all('/\sN?_\("([^\)]+)"\)/', $sharpComment[1], $matches)) {
38  foreach ($matches[1] as $m) {
39  $filteredLabel[] = $m;
40  }
41  }
42  }
43  return $filteredLabel;
44  }
45 
46  public function extract()
47  {
48  $potFile = $this->outputFile;
49  $phpFile = $potFile . "_searchlabel_.php";
50  $searchLabel = array();
51  $sharpLabel = array();
52  foreach ($this->inputFiles as $k => $phpInputFile) {
53  $phpInputFile = trim($phpInputFile);
54  if (!$phpInputFile) {
55  unset($this->inputFiles[$k]);
56  continue;
57  }
58  $labels = $this->extractExtraLabel($phpInputFile);
59  $searchLabel = array_merge($searchLabel, $labels);
60  $labels = $this->extractSharp();
61  $sharpLabel = array_merge($sharpLabel, $labels);
62  }
63  $searchPhp = "<?php\n";
64  foreach ($searchLabel as $label) {
65  $searchPhp.= sprintf("\n// _COMMENT Search Label\n");
66  $searchPhp.= sprintf('$a=_("%s");', preg_replace('/"/', '\"', $label));
67  }
68  foreach ($sharpLabel as $label) {
69  $searchPhp.= sprintf("\n// _COMMENT Sharp Label\n");
70  $searchPhp.= sprintf('$a=_("%s");', preg_replace('/"/', '\"', $label));
71  }
72 
73  file_put_contents($phpFile, $searchPhp);
74  $inputFilesArgs = array_map("escapeshellarg", $this->inputFiles);
75  $cmd = sprintf('xgettext \
76  --force-po \
77  --language=PHP \
78  --sort-output \
79  --from-code=utf-8 \
80  --no-location \
81  --add-comments=_COMMENT \
82  --keyword=___:1 \
83  --keyword=___:1,2c \
84  --keyword=n___:1,2 \
85  --keyword=pgettext:1,2 \
86  --keyword=n___:1,2,4c \
87  --keyword=npgettext:1,2,4c \
88  --keyword="N_" \
89  --keyword="text" \
90  --keyword="Text" \
91  %s -o %s %s %s && rm %s', $this->getXoptions() , escapeshellarg($potFile) , join(' ', $inputFilesArgs) , escapeshellarg($phpFile) , escapeshellarg($phpFile));
92 
93  self::mySystem($cmd);
94  }
95 }
96 
extractExtraLabel($inputFile)
Definition: xgettextPhp.php:11
foreach($argv as $arg) $cmd
← centre documentaire © anakeen