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

Debug toolbar