Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
xgettextFamily.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";
8 require_once "ods2csv.php";
9 
11 {
12  protected $outputFile = "messages";
13  protected $inputFiles = array();
14  protected $outputFiles = array();
15 
16  public static function getUsage()
17  {
18  global $argv;
19  return sprintf("%s extract tranlation from family structure files\n
20  %s -o <output directory> <input file> [<input file>...]\n
21  If option has '-f-', files from stdin are used\n", $argv[0], $argv[0]);
22  }
23  /**
24  * extractPOFromCSV from a CSV file and print it on standard output
25  *
26  * @param string $fi file input path
27  * @param string $podir Po directory
28  * @return void
29  */
30  protected function extractPOFromCSV($fi, $podir)
31  {
32  $fdoc = fopen($fi, "r");
33  if (!$fdoc) {
34  throw new Exception("fam2po: Can't access file [$fi]");
35  } else {
36  $podoc = null;
37  $contentToWrite = "";
38  $nline = - 1;
39  $famname = "*******";
40  $cv_idview_index = 0;
41  $cv_lview_index = 0;
42  $cv_menu_index = 0;
43  $date = date("c");
44  while (!feof($fdoc)) {
45 
46  $nline++;
47 
48  $buffer = rtrim(fgets($fdoc, 16384));
49  $data = explode(";", $buffer);
50 
51  $num = count($data);
52  if ($num < 1) {
53  continue;
54  }
55 
56  $data[0] = trim($this->getArrayIndexValue($data, 0));
57  switch ($data[0]) {
58  case "BEGIN":
59  $famname = $this->getArrayIndexValue($data, 5);
60  $famtitle = $this->getArrayIndexValue($data, 2);
61  if ($famname) {
62  $oFileName = $podir . "/" . $famname . ".pot";
63  $this->outputFiles[] = $oFileName;
64  $podoc = fopen($oFileName, "w+");
65  } else {
66  $podoc = null;
67  }
68  $contentToWrite = "msgid \"\"\n";
69  $contentToWrite.= "msgstr \"\"\n";
70  $contentToWrite.= "\"Project-Id-Version: $famname \\n\"\n";
71  $contentToWrite.= "\"Report-Msgid-Bugs-To: \\n\"\n";
72  $contentToWrite.= "\"PO-Revision-Date: $date\\n\"\n";
73  $contentToWrite.= "\"Last-Translator: Automatically generated\\n\"\n";
74  $contentToWrite.= "\"Language-Team: none\\n\"\n";
75  $contentToWrite.= "\"MIME-Version: 1.0\\n\"\n";
76  $contentToWrite.= "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
77  $contentToWrite.= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
78  $contentToWrite.= "\"Language: \\n\"\n\n";
79  $contentToWrite.= "#, fuzzy, ($fi)\n";
80  $contentToWrite.= "msgid \"" . $famname . "#title\"\n";
81  $contentToWrite.= "msgstr \"" . $famtitle . "\"\n\n";
82 
83  break;
84 
85  case "END":
86  if (!$podoc) {
87  throw new Exception("xgettextFamily: Can't create tempory family po file [$podir/$famname.pot]");
88  } else {
89  fwrite($podoc, $contentToWrite);
90  fclose($podoc);
91  }
92  $famname = "*******";
93  break;
94 
95  case "ORDER":
96  $type = $this->getArrayIndexValue($data, 1);
97  $cv_idview_index = 0;
98  $cv_lview_index = 0;
99  $cv_menu_index = 0;
100  if ($type === "CVDOC") {
101  foreach ($data as $index => $value) {
102  if ($value === "cv_idview") $cv_idview_index = $index;
103  else if ($value === "cv_lview") $cv_lview_index = $index;
104  else if ($value === "cv_menu") $cv_menu_index = $index;
105  if ($cv_idview_index && $cv_lview_index && $cv_menu_index) break;
106  }
107  }
108  break;
109 
110  case "DOC":
111  $type = $this->getArrayIndexValue($data, 1);
112  if ($type === "CVDOC") {
113  $cvName = $this->getArrayIndexValue($data, 2);
114  if ($cvName && !is_numeric($cvName) && $cv_idview_index) {
115  $oFileName = $podir . "/" . $cvName . ".pot";
116  $cvdoc = fopen($oFileName, "w+");
117 
118  $this->outputFiles[] = $oFileName;
119  if (!$cvdoc) {
120  throw new Exception("fam2po: Can't create tempory CV po file [$podir/$cvName.pot]");
121  }
122  $cvContentToWrite = "msgid \"\"\n";
123  $cvContentToWrite.= "msgstr \"\"\n";
124  $cvContentToWrite.= "\"Project-Id-Version: $cvName \\n\"\n";
125  $cvContentToWrite.= "\"Report-Msgid-Bugs-To: \\n\"\n";
126  $cvContentToWrite.= "\"PO-Revision-Date: $date\\n\"\n";
127  $cvContentToWrite.= "\"Last-Translator: Automatically generated\\n\"\n";
128  $cvContentToWrite.= "\"Language-Team: none\\n\"\n";
129  $cvContentToWrite.= "\"MIME-Version: 1.0\\n\"\n";
130  $cvContentToWrite.= "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
131  $cvContentToWrite.= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
132  $cvContentToWrite.= "\"Language: \\n\"\n\n";
133  $tcv_idview = explode('\n', $this->getArrayIndexValue($data, $cv_idview_index));
134  $tcv_lview = explode('\n', $this->getArrayIndexValue($data, $cv_lview_index));
135  $tcv_menu = explode('\n', $this->getArrayIndexValue($data, $cv_menu_index));
136  foreach ($tcv_idview as $i => $id) {
137  if ($cv_lview_index && $tcv_lview[$i]) {
138  $cvContentToWrite.= "#: $fi\n";
139  $cvContentToWrite.= "#, fuzzy\n";
140  $cvContentToWrite.= "msgid \"" . $cvName . "#label#" . $id . "\"\n";
141  $cvContentToWrite.= "msgstr \"" . $tcv_lview[$i] . "\"\n\n";
142  }
143  if ($cv_menu_index && $tcv_menu[$i]) {
144  $cvContentToWrite.= "#: $fi\n";
145  $cvContentToWrite.= "#, fuzzy\n";
146  $cvContentToWrite.= "msgid \"" . $cvName . "#menu#" . $id . "\"\n";
147  $cvContentToWrite.= "msgstr \"" . $tcv_menu[$i] . "\"\n\n";
148  }
149  }
150  fwrite($cvdoc, $cvContentToWrite);
151  fclose($cvdoc);
152  }
153  }
154  break;
155 
156  case "ATTR":
157  case "MODATTR":
158  case "PARAM":
159  case "OPTION":
160  $contentToWrite.= "#: $fi\n";
161  $contentToWrite.= "#, fuzzy, ($fi)\n";
162  $contentToWrite.= "msgid \"" . $famname . "#" . strtolower($this->getArrayIndexValue($data, 1)) . "\"\n";
163  $contentToWrite.= "msgstr \"" . $this->getArrayIndexValue($data, 3) . "\"\n\n";
164  // Enum ----------------------------------------------
165  $type = $this->getArrayIndexValue($data, 6);
166  if ($type == "enum" || $type == "enumlist") {
167  $d = str_replace('\,', '\#', $this->getArrayIndexValue($data, 12));
168  $tenum = explode(",", $d);
169  foreach ($tenum as $ve) {
170  $d = str_replace('\#', ',', $ve);
171  $enumValues = explode("|", $d);
172  $contentToWrite.= "#, fuzzy, ($fi)\n";
173  $contentToWrite.= "msgid \"" . $famname . "#" . strtolower($this->getArrayIndexValue($data, 1)) . "#" . (str_replace('\\', '', $this->getArrayIndexValue($enumValues, 0))) . "\"\n";
174  $contentToWrite.= "msgstr \"" . (str_replace('\\', '', $this->getArrayIndexValue($enumValues, 1))) . "\"\n\n";
175  }
176  }
177  // Options ----------------------------------------------
178  $options = $this->getArrayIndexValue($data, 15);
179  $options = explode("|", $options);
180  foreach ($options as $currentOption) {
181  $currentOption = explode("=", $currentOption);
182  $currentOptionKey = $this->getArrayIndexValue($currentOption, 0);
183  $currentOptionValue = $this->getArrayIndexValue($currentOption, 1);
184  switch (strtolower($currentOptionKey)) {
185  case "elabel":
186  case "ititle":
187  case "submenu":
188  case "ltitle":
189  case "eltitle":
190  case "elsymbol":
191  case "lsymbol":
192  case "showempty":
193  $contentToWrite.= "#, fuzzy, ($fi)\n";
194  $contentToWrite.= "msgid \"" . $famname . "#" . strtolower($this->getArrayIndexValue($data, 1)) . "#" . strtolower($currentOptionKey) . "\"\n";
195  $contentToWrite.= "msgstr \"" . $currentOptionValue . "\"\n\n";
196  }
197  }
198  }
199  }
200  }
201  }
202 
203  protected function getArrayIndexValue(&$array, $index)
204  {
205  return isset($array[$index]) ? $array[$index] : "";
206  }
207 
208  public function getOutputFile()
209  {
210  return implode(", ", $this->outputFiles);
211  }
212 
213  public function extract()
214  {
215  if (!is_dir($this->outputFile)) {
216  throw new Exception(sprintf('output option "%s" must be a directory', $this->outputFile));
217  }
218 
219  $convert = new Ods2Csv();
220  foreach ($this->inputFiles as $familyFile) {
221  if (empty($familyFile)) {
222  continue;
223  }
224  $err = "";
225  try {
226  $familyFilePathInfo = pathinfo($familyFile);
227  if (file_exists($familyFile)) {
228  if (isset($familyFilePathInfo['extension'])) {
229  switch ($familyFilePathInfo['extension']) {
230  case 'ods':
231  $csvfile = $familyFile . ".csv";
232  $convert->convertOds2csv($familyFile, $csvfile);
233  if (file_exists($csvfile)) {
234  $this->extractPOFromCSV($csvfile, $this->outputFile);
235  } else {
236  throw new Exception("Unable to generate CSV from " . $familyFile);
237  }
238  unlink($csvfile);
239  break;
240 
241  case 'csv':
242  $this->extractPOFromCSV($familyFile, $this->outputFile);
243  break;
244 
245  default:
246  $err = ($familyFile . " has an unknown extension, skipping it.");
247  }
248  } else {
249  $err = ($familyFile . " has no extension, skipping it.");
250  }
251  } else {
252  throw new Exception("Can't access file " . $familyFile);
253  }
254  }
255  catch(Exception $e) {
256  $err.= $e->getMessage() . " " . $e->getFile() . " line (" . $e->getLine() . ")\n";
257  }
258  if ($err) {
259  throw new Exception($err);
260  }
261  }
262  }
263  }
264 
265 
if($dbaccess=="") if($statusFile=== '') $famtitle
$d
Definition: dav.php:77
$csvfile
extractPOFromCSV($fi, $podir)
$podir
Definition: fam2po.php:20
getArrayIndexValue(&$array, $index)
if($file) if($subject==""&&$file) if($subject=="") $err
$value
$data
← centre documentaire © anakeen