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

Debug toolbar