Translate.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Backend\Controller\Adminhtml\Ajax;
  8. use Magento\Backend\App\Action;
  9. class Translate extends \Magento\Backend\App\Action
  10. {
  11. /**
  12. * @var \Magento\Framework\Translate\Inline\ParserInterface
  13. */
  14. protected $inlineParser;
  15. /**
  16. * @var \Magento\Framework\Controller\Result\JsonFactory
  17. */
  18. protected $resultJsonFactory;
  19. /**
  20. * Authorization level of a basic admin session
  21. */
  22. const ADMIN_RESOURCE = 'Magento_Backend::content_translation';
  23. /**
  24. * @param Action\Context $context
  25. * @param \Magento\Framework\Translate\Inline\ParserInterface $inlineParser
  26. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  27. */
  28. public function __construct(
  29. Action\Context $context,
  30. \Magento\Framework\Translate\Inline\ParserInterface $inlineParser,
  31. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  32. ) {
  33. parent::__construct($context);
  34. $this->resultJsonFactory = $resultJsonFactory;
  35. $this->inlineParser = $inlineParser;
  36. }
  37. /**
  38. * Ajax action for inline translation
  39. *
  40. * @return \Magento\Framework\Controller\Result\Json
  41. */
  42. public function execute()
  43. {
  44. $translate = (array)$this->getRequest()->getPost('translate');
  45. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  46. $resultJson = $this->resultJsonFactory->create();
  47. try {
  48. $this->inlineParser->processAjaxPost($translate);
  49. $response = ['success' => 'true'];
  50. } catch (\Exception $e) {
  51. $response = ['error' => 'true', 'message' => $e->getMessage()];
  52. }
  53. $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
  54. return $resultJson->setData($response);
  55. }
  56. }