sfI18nExtract.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @package symfony
  11. * @subpackage i18n
  12. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  13. * @version SVN: $Id: sfI18nExtract.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  14. */
  15. abstract class sfI18nExtract
  16. {
  17. protected
  18. $currentMessages = array(),
  19. $newMessages = array(),
  20. $allSeenMessages = array(),
  21. $culture = null,
  22. $parameters = array(),
  23. $i18n = null;
  24. /**
  25. * Class constructor.
  26. *
  27. * @see initialize()
  28. */
  29. public function __construct(sfI18N $i18n, $culture, $parameters = array())
  30. {
  31. $this->initialize($i18n, $culture, $parameters);
  32. }
  33. /**
  34. * Initializes the current extract object.
  35. *
  36. * @param sfI18N $i18n A sfI18N instance
  37. * @param string $culture The culture
  38. * @param array $parameters An array of parameters
  39. */
  40. function initialize(sfI18N $i18n, $culture, $parameters = array())
  41. {
  42. $this->allSeenMessages = array();
  43. $this->newMessages = array();
  44. $this->currentMessages = array();
  45. $this->culture = $culture;
  46. $this->parameters = $parameters;
  47. $this->i18n = $i18n;
  48. $this->configure();
  49. $this->loadMessageSources();
  50. $this->loadCurrentMessages();
  51. }
  52. /**
  53. * Configures the current extract object.
  54. */
  55. public function configure()
  56. {
  57. }
  58. /**
  59. * Extracts i18n strings.
  60. *
  61. * This class must be implemented by subclasses.
  62. */
  63. abstract public function extract();
  64. /**
  65. * Saves the new messages.
  66. *
  67. * Current limitations:
  68. * - For file backends (XLIFF and gettext), it only saves in the "most global" file
  69. */
  70. public function saveNewMessages()
  71. {
  72. $messageSource = $this->i18n->getMessageSource();
  73. foreach ($this->getNewMessages() as $message)
  74. {
  75. $messageSource->append($message);
  76. }
  77. $messageSource->save();
  78. }
  79. /**
  80. * Deletes old messages.
  81. *
  82. * Current limitations:
  83. * - For file backends (XLIFF and gettext), it only deletes in the "most global" file
  84. */
  85. public function deleteOldMessages()
  86. {
  87. $messageSource = $this->i18n->getMessageSource();
  88. foreach ($this->getOldMessages() as $message)
  89. {
  90. $messageSource->delete($message);
  91. }
  92. }
  93. /**
  94. * Gets the new i18n strings.
  95. *
  96. * @return array An array of i18n strings
  97. */
  98. final public function getNewMessages()
  99. {
  100. return array_diff($this->getAllSeenMessages(), $this->getCurrentMessages());
  101. }
  102. /**
  103. * Gets the current i18n strings.
  104. *
  105. * @return array An array of i18n strings
  106. */
  107. public function getCurrentMessages()
  108. {
  109. return $this->currentMessages;
  110. }
  111. /**
  112. * Gets all i18n strings seen during the extraction process.
  113. *
  114. * @return array An array of i18n strings
  115. */
  116. public function getAllSeenMessages()
  117. {
  118. return $this->allSeenMessages;
  119. }
  120. /**
  121. * Gets old i18n strings.
  122. *
  123. * This returns all strings that weren't seen during the extraction process
  124. * and are in the current messages.
  125. *
  126. * @return array An array of i18n strings
  127. */
  128. final public function getOldMessages()
  129. {
  130. return array_diff($this->getCurrentMessages(), $this->getAllSeenMessages());
  131. }
  132. /**
  133. * Loads message sources objects and sets the culture.
  134. */
  135. protected function loadMessageSources()
  136. {
  137. $this->i18n->getMessageSource()->setCulture($this->culture);
  138. $this->i18n->getMessageSource()->load();
  139. }
  140. /**
  141. * Loads messages already saved in the message sources.
  142. */
  143. protected function loadCurrentMessages()
  144. {
  145. $this->currentMessages = array();
  146. foreach ($this->i18n->getMessageSource()->read() as $catalogue => $translations)
  147. {
  148. foreach ($translations as $key => $values)
  149. {
  150. $this->currentMessages[] = $key;
  151. }
  152. }
  153. }
  154. /**
  155. * Extracts i18n strings from PHP files.
  156. *
  157. * @param string $dir The PHP full path name
  158. */
  159. protected function extractFromPhpFiles($dir)
  160. {
  161. $phpExtractor = new sfI18nPhpExtractor();
  162. $files = sfFinder::type('file')->name('*.php');
  163. $messages = array();
  164. foreach ($files->in($dir) as $file)
  165. {
  166. $messages = array_merge($messages, $phpExtractor->extract(file_get_contents($file)));
  167. }
  168. $this->updateMessages($messages);
  169. }
  170. /**
  171. * Updates the internal arrays with new messages.
  172. *
  173. * @param array $messages An array of new i18n strings
  174. */
  175. protected function updateMessages($messages)
  176. {
  177. $this->allSeenMessages = array_unique(array_merge($this->allSeenMessages, $messages));
  178. }
  179. }