DataProvider.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model\Js;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * DataProvider for js translation
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class DataProvider implements DataProviderInterface
  14. {
  15. /**
  16. * Application state
  17. *
  18. * @var \Magento\Framework\App\State
  19. */
  20. protected $appState;
  21. /**
  22. * Js translation configuration
  23. *
  24. * @var Config
  25. */
  26. protected $config;
  27. /**
  28. * Files utility
  29. *
  30. * @var \Magento\Framework\App\Utility\Files
  31. */
  32. protected $filesUtility;
  33. /**
  34. * Filesystem
  35. *
  36. * @var \Magento\Framework\Filesystem\File\ReadFactory
  37. */
  38. protected $fileReadFactory;
  39. /**
  40. * Basic translate renderer
  41. *
  42. * @var \Magento\Framework\Phrase\RendererInterface
  43. */
  44. protected $translate;
  45. /**
  46. * @param \Magento\Framework\App\State $appState
  47. * @param Config $config
  48. * @param \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory
  49. * @param \Magento\Framework\Phrase\RendererInterface $translate
  50. * @param \Magento\Framework\Component\ComponentRegistrar $componentRegistrar
  51. * @param \Magento\Framework\Component\DirSearch $dirSearch
  52. * @param \Magento\Framework\View\Design\Theme\ThemePackageList $themePackageList
  53. * @param \Magento\Framework\App\Utility\Files|null $filesUtility
  54. */
  55. public function __construct(
  56. \Magento\Framework\App\State $appState,
  57. Config $config,
  58. \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory,
  59. \Magento\Framework\Phrase\RendererInterface $translate,
  60. \Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
  61. \Magento\Framework\Component\DirSearch $dirSearch,
  62. \Magento\Framework\View\Design\Theme\ThemePackageList $themePackageList,
  63. \Magento\Framework\App\Utility\Files $filesUtility = null
  64. ) {
  65. $this->appState = $appState;
  66. $this->config = $config;
  67. $this->fileReadFactory = $fileReadFactory;
  68. $this->translate = $translate;
  69. $this->filesUtility = (null !== $filesUtility) ?
  70. $filesUtility : new \Magento\Framework\App\Utility\Files(
  71. $componentRegistrar,
  72. $dirSearch,
  73. $themePackageList
  74. );
  75. }
  76. /**
  77. * Get translation data
  78. *
  79. * @param string $themePath
  80. * @return array
  81. * @throws \Exception
  82. * @throws \Magento\Framework\Exception\LocalizedException
  83. */
  84. public function getData($themePath)
  85. {
  86. $areaCode = $this->appState->getAreaCode();
  87. $files = array_merge(
  88. $this->filesUtility->getJsFiles('base', $themePath),
  89. $this->filesUtility->getJsFiles($areaCode, $themePath),
  90. $this->filesUtility->getStaticHtmlFiles('base', $themePath),
  91. $this->filesUtility->getStaticHtmlFiles($areaCode, $themePath)
  92. );
  93. $dictionary = [];
  94. foreach ($files as $filePath) {
  95. $read = $this->fileReadFactory->create($filePath[0], \Magento\Framework\Filesystem\DriverPool::FILE);
  96. $content = $read->readAll();
  97. foreach ($this->getPhrases($content) as $phrase) {
  98. try {
  99. $translatedPhrase = $this->translate->render([$phrase], []);
  100. if ($phrase != $translatedPhrase) {
  101. $dictionary[$phrase] = $translatedPhrase;
  102. }
  103. } catch (\Exception $e) {
  104. throw new LocalizedException(
  105. __('Error while translating phrase "%s" in file %s.', $phrase, $filePath[0]),
  106. $e
  107. );
  108. }
  109. }
  110. }
  111. return $dictionary;
  112. }
  113. /**
  114. * Parse content for entries to be translated
  115. *
  116. * @param string $content
  117. * @return string[]
  118. * @throws \Magento\Framework\Exception\LocalizedException
  119. */
  120. protected function getPhrases($content)
  121. {
  122. $phrases = [];
  123. foreach ($this->config->getPatterns() as $pattern) {
  124. $concatenatedContent = preg_replace('~(["\'])\s*?\+\s*?\1~', '', $content);
  125. $result = preg_match_all($pattern, $concatenatedContent, $matches);
  126. if ($result) {
  127. if (isset($matches[2])) {
  128. foreach ($matches[2] as $match) {
  129. $phrases[] = str_replace(["\'", '\"'], ["'", '"'], $match);
  130. }
  131. }
  132. }
  133. if (false === $result) {
  134. throw new LocalizedException(
  135. __('Error while generating js translation dictionary: "%s"', error_get_last())
  136. );
  137. }
  138. }
  139. return $phrases;
  140. }
  141. }