PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette.Addons/GettextExtractor/GettextExtractor.class.php

https://code.google.com/
PHP | 164 lines | 124 code | 35 blank | 5 comment | 15 complexity | 7f1af34e574407854dbb3cd09392c166 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. if (version_compare(PHP_VERSION, '5.2.2', '<'))
  3. exit('GettextExtractor needs PHP 5.2.2 or newer');
  4. class GettextExtractor
  5. {
  6. const LOG_FILE = '/extractor.log';
  7. public $logHandler;
  8. public $outputFile;
  9. public $inputFiles = array();
  10. public $filters = array(
  11. 'php' => array('PHP'),
  12. 'phtml' => array('PHP', 'NetteCurlyBrackets')
  13. );
  14. public $pluralMatchRegexp = '#\%([0-9]+\$)*d#';
  15. public $data = array();
  16. protected $filterStore = array();
  17. /* ----------- Mandatory functions ---------- */
  18. public function __construct()
  19. {
  20. $this->logHandler = fopen(dirname(__FILE__) . self::LOG_FILE, "w");
  21. }
  22. public function __destruct()
  23. {
  24. if (is_resource($this->logHandler)) fclose($this->logHandler);
  25. }
  26. public function log($message)
  27. {
  28. if (is_resource($this->logHandler)) {
  29. fwrite($this->logHandler, $message . "\n");
  30. }
  31. }
  32. protected function throwException()
  33. {
  34. $message = 'Something unexpected occured. See GettextExtractor log for details';
  35. echo $message;
  36. throw new Exception($message);
  37. }
  38. /* ------------ Extractor functions ----------- */
  39. public function extract($inputFiles)
  40. {
  41. foreach ($inputFiles as $inputFile)
  42. {
  43. if (!file_exists($inputFile)) {
  44. $this->log('ERROR: Invalid input file specified: ' . $inputFile);
  45. $this->throwException();
  46. }
  47. if (!is_readable($inputFile)) {
  48. $this->log('ERROR: Input file is not readable: ' . $inputFile);
  49. $this->throwException();
  50. }
  51. $this->log('Extracting data from file ' . $inputFile);
  52. foreach ($this->filters as $extension => $filters)
  53. {
  54. // Check file extension
  55. if (substr($inputFile, strlen($inputFile) - strlen($extension)) != $extension) continue;
  56. $this->log('Processing file ' . $inputFile);
  57. foreach ($filters as $filterName)
  58. {
  59. $filter = $this->getFilter($filterName);
  60. $filterData = $filter->extract($inputFile);
  61. $this->log(' Filter ' . $filterName . ' applied');
  62. $this->data = array_merge_recursive($this->data, $filterData);
  63. }
  64. }
  65. }
  66. $this->log('Data exported successfully');
  67. return $this->data;
  68. }
  69. public function getFilter($filter)
  70. {
  71. $filter = $filter . 'Filter';
  72. if (isset($this->filterStore[$filter])) return $this->filterStore[$filter];
  73. if (!class_exists($filter)) {
  74. $filter_file = dirname(__FILE__) . '/Filters/' . $filter . ".php";
  75. if (!file_exists($filter_file)) {
  76. $this->log('ERROR: Filter file ' . $filter_file . ' not found');
  77. $this->throwException();
  78. }
  79. require_once $filter_file;
  80. if (!class_exists($filter)) {
  81. $this->log('ERROR: Class ' . $filter . ' not found');
  82. $this->throwException();
  83. }
  84. }
  85. $this->filterStore[$filter] = new $filter;
  86. $this->log('Filter ' . $filter . ' loaded');
  87. return $this->filterStore[$filter];
  88. }
  89. public function write($outputFile, $data = null) {
  90. $data = $data ? $data : $this->data;
  91. // Output file permission check
  92. if (file_exists($outputFile) && !is_writable($outputFile)) {
  93. $this->log('ERROR: Output file is not writable!');
  94. $this->throwException();
  95. }
  96. $handle = fopen($outputFile, "w");
  97. fwrite($handle, $this->formatData($data));
  98. fclose($handle);
  99. $this->log('Data written successfully');
  100. }
  101. protected function formatData($data)
  102. {
  103. $output = array();
  104. $output[] = '# Gettext keys exported by GettextExtractor';
  105. $output[] = 'msgid ""';
  106. $output[] = 'msgstr ""';
  107. $output[] = '"Content-Type: text/plain; charset=UTF-8\n"';
  108. $output[] = '"Plural-Forms: nplurals=2; plural=(n != 1);\n"';
  109. $output[] = '';
  110. ksort($data);
  111. foreach ($data as $key => $files)
  112. {
  113. ksort($files);
  114. foreach ($files as $file)
  115. $output[] = '# ' . $file;
  116. $output[] = 'msgid "' . $key . '"'; //removed add slashes
  117. if (preg_match($this->pluralMatchRegexp, $key, $matches)) {
  118. $output[] = 'msgid_plural "' . $key . '"';
  119. //$output[] = 'msgid_plural ""';
  120. $output[] = 'msgstr[0] "' . $key . '"'; //removed add slashes
  121. $output[] = 'msgstr[1] "' . $key . '"'; //removed add slashes
  122. } else {
  123. $output[] = 'msgstr "' . $key . '"'; //removed add slashes
  124. }
  125. $output[] = '';
  126. }
  127. return join("\n", $output);
  128. }
  129. }