PreProcessor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model\Json;
  7. use Magento\Framework\App\Area;
  8. use Magento\Framework\App\AreaList;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\TranslateInterface;
  11. use Magento\Framework\View\Asset\File\FallbackContext;
  12. use Magento\Framework\View\Asset\PreProcessor\Chain;
  13. use Magento\Framework\View\Asset\PreProcessorInterface;
  14. use Magento\Framework\View\DesignInterface;
  15. use Magento\Backend\App\Area\FrontNameResolver;
  16. use Magento\Translation\Model\Js\Config;
  17. use Magento\Translation\Model\Js\DataProviderInterface;
  18. /**
  19. * PreProcessor responsible for providing js translation dictionary
  20. */
  21. class PreProcessor implements PreProcessorInterface
  22. {
  23. /**
  24. * Js translation configuration
  25. *
  26. * @var Config
  27. */
  28. protected $config;
  29. /**
  30. * Translation data provider
  31. *
  32. * @var DataProviderInterface
  33. */
  34. protected $dataProvider;
  35. /**
  36. * @var AreaList
  37. */
  38. protected $areaList;
  39. /**
  40. * @var TranslateInterface
  41. */
  42. protected $translate;
  43. /**
  44. * @var DesignInterface
  45. */
  46. private $viewDesign;
  47. /**
  48. * @param Config $config
  49. * @param DataProviderInterface $dataProvider
  50. * @param AreaList $areaList
  51. * @param TranslateInterface $translate
  52. * @param DesignInterface|null $viewDesign
  53. */
  54. public function __construct(
  55. Config $config,
  56. DataProviderInterface $dataProvider,
  57. AreaList $areaList,
  58. TranslateInterface $translate,
  59. DesignInterface $viewDesign = null
  60. ) {
  61. $this->config = $config;
  62. $this->dataProvider = $dataProvider;
  63. $this->areaList = $areaList;
  64. $this->translate = $translate;
  65. $this->viewDesign = $viewDesign ?? ObjectManager::getInstance()->get(DesignInterface::class);
  66. }
  67. /**
  68. * Transform content and/or content type for the specified preprocessing chain object
  69. *
  70. * @param Chain $chain
  71. * @return void
  72. */
  73. public function process(Chain $chain)
  74. {
  75. if ($this->isDictionaryPath($chain->getTargetAssetPath())) {
  76. $context = $chain->getAsset()->getContext();
  77. $themePath = '*/*';
  78. $areaCode = FrontNameResolver::AREA_CODE;
  79. if ($context instanceof FallbackContext) {
  80. $themePath = $context->getThemePath();
  81. $areaCode = $context->getAreaCode();
  82. $this->viewDesign->setDesignTheme($themePath, $areaCode);
  83. }
  84. if ($areaCode !== FrontNameResolver::AREA_CODE) {
  85. $area = $this->areaList->getArea($areaCode);
  86. $area->load(Area::PART_TRANSLATE);
  87. }
  88. $this->translate->setLocale($context->getLocale())->loadData($areaCode, true);
  89. $chain->setContent(json_encode($this->dataProvider->getData($themePath)));
  90. $chain->setContentType('json');
  91. }
  92. }
  93. /**
  94. * Is provided path the path to translation dictionary
  95. *
  96. * @param string $path
  97. * @return bool
  98. */
  99. protected function isDictionaryPath($path)
  100. {
  101. return (strpos($path, $this->config->getDictionaryFileName()) !== false);
  102. }
  103. }