Config.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Block\Html\Head;
  7. use Magento\Framework\RequireJs\Config as RequireJsConfig;
  8. use Magento\Framework\Translate\Inline as Inline;
  9. /**
  10. * Block responsible for including Inline Translation config on the page
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Config extends \Magento\Framework\View\Element\AbstractBlock
  16. {
  17. /**
  18. * @var \Magento\Translation\Model\FileManager
  19. */
  20. private $fileManager;
  21. /**
  22. * @var \Magento\Framework\View\Page\Config
  23. */
  24. protected $pageConfig;
  25. /**
  26. * @var Inline
  27. */
  28. private $inline;
  29. /**
  30. * @param \Magento\Framework\View\Element\Context $context
  31. * @param \Magento\Framework\View\Page\Config $pageConfig
  32. * @param \Magento\Translation\Model\FileManager $fileManager
  33. * @param Inline $inline
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Element\Context $context,
  38. \Magento\Framework\View\Page\Config $pageConfig,
  39. \Magento\Translation\Model\FileManager $fileManager,
  40. Inline $inline,
  41. array $data = []
  42. ) {
  43. parent::__construct($context, $data);
  44. $this->pageConfig = $pageConfig;
  45. $this->fileManager = $fileManager;
  46. $this->inline = $inline;
  47. }
  48. /**
  49. * Include RequireJs configuration as an asset on the page
  50. *
  51. * @return $this
  52. */
  53. protected function _prepareLayout()
  54. {
  55. $this->addInlineTranslationConfig();
  56. return parent::_prepareLayout();
  57. }
  58. /**
  59. * Include RequireJs inline translation configuration as an asset on the page
  60. * @return void
  61. */
  62. private function addInlineTranslationConfig()
  63. {
  64. if ($this->inline->isAllowed()) {
  65. $after = RequireJsConfig::REQUIRE_JS_FILE_NAME;
  66. $tConfig = $this->fileManager->createTranslateConfigAsset();
  67. $assetCollection = $this->pageConfig->getAssetCollection();
  68. $assetCollection->insert(
  69. $tConfig->getFilePath(),
  70. $tConfig,
  71. $after
  72. );
  73. }
  74. }
  75. }