sfI18nPhpExtractor.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: sfI18nPhpExtractor.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  14. */
  15. class sfI18nPhpExtractor implements sfI18nExtractorInterface
  16. {
  17. /**
  18. * Extract i18n strings for the given content.
  19. *
  20. * @param string $content The content
  21. *
  22. * @return array An array of i18n strings
  23. */
  24. public function extract($content)
  25. {
  26. $tokens = token_get_all($content);
  27. $strings = array();
  28. $i18n_function = 0;
  29. $line = 0;
  30. $heredoc = false;
  31. $buffer = '';
  32. foreach ($tokens as $token)
  33. {
  34. if (is_string($token))
  35. {
  36. switch ($token)
  37. {
  38. case '(':
  39. if (1 == $i18n_function)
  40. {
  41. $i18n_function = 2;
  42. }
  43. break;
  44. default:
  45. $i18n_function = 0;
  46. }
  47. }
  48. else
  49. {
  50. list($id, $text) = $token;
  51. switch ($id)
  52. {
  53. case T_STRING:
  54. if ($heredoc && 2 == $i18n_function)
  55. {
  56. $buffer .= $text;
  57. }
  58. else
  59. {
  60. $i18n_function = ('__' == $text || 'format_number_choice' == $text) ? 1 : 0;
  61. }
  62. break;
  63. case T_WHITESPACE:
  64. break;
  65. case T_START_HEREDOC:
  66. $heredoc = true;
  67. break;
  68. case T_END_HEREDOC:
  69. $heredoc = false;
  70. if ($buffer)
  71. {
  72. $strings[] = $buffer;
  73. }
  74. $i18n_function = 0;
  75. break;
  76. case T_CONSTANT_ENCAPSED_STRING:
  77. if (2 == $i18n_function)
  78. {
  79. $delimiter = $text[0];
  80. $strings[] = str_replace('\\'.$delimiter, $delimiter, substr($text, 1, -1));
  81. }
  82. $i18n_function = 0;
  83. break;
  84. default:
  85. if ($heredoc && 2 == $i18n_function)
  86. {
  87. $buffer .= $text;
  88. }
  89. else
  90. {
  91. $i18n_function = 0;
  92. }
  93. }
  94. }
  95. }
  96. return $strings;
  97. }
  98. }