7

I have a controller that act as an API endpoint in magento 2, i need to have a response on different language base on parameter that were passed, this controller can access model and etc.

for example:

public function execute()
{
     if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');     
    }

     if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

    try {
        $lang = $this->getRequest()->getParam('lang');
        if($lang == 'en'){
          $message = 'who are you';
          $data['var_1'] = "Live";
        }elseif($lang == 'jp'){
          $messsage = 'あなたは誰';
          $data['var_1'] = "ライブ";
        } 
        return $this->_formatResponse(500,$message,$data);
    } catch (\Exception $ex) {
        $message = $ex->getMessage();
        return $this->_formatResponse(500,$message);
    }
}

protected function _formatResponse($status, $message = '', $data = [])
{ 
    $result = $this->_resultJsonFactory->create();
    $result->setHttpResponseCode($status);
    return $result->setData([
        'status' => $status,
        'message' => $message,
        'data' => $data
    ]);
}

how can i achieve to manage this translation message dynamically when there is a lot of user access my controller.

  • Can you please upload your whole controller code? – Rohan Hapani Sep 18 '18 at 4:22
  • @RohanHapani check my updated question – Tiny Dancer Sep 18 '18 at 5:06
0

Below Controller code works for me but it will change the store view for front-end too (in case same session for API call and the web site itself)

<?php

namespace Vendor\MyModule\Controller\Index;

use Magento\Store\Model\Store;
use Magento\Framework\Controller\ResultFactory;
use Company\OrderDevice\Helper\Data;


class Index extends \Magento\Framework\App\Action\Action
{


protected $_storeManagerInterface;
protected $_resultJsonFactory;
protected $_httpContext;
protected $_storeRepository;
protected $storeCookieManager;

public function __construct(
        \Magento\Framework\App\Action\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory        
    ) {

        $this->_storeManagerInterface        = $storeManager;
        $this->storeCookieManager = $storeCookieManager;
        $this->_httpContext = $httpContext;
        $this->_storeRepository = $storeRepository;
        $this->_resultJsonFactory   = $resultJsonFactory;        
        parent::__construct($context);
    }


 public function execute(){
   $fromStore= 'default'; // English
   $toStore  = 'cn';      // Chinese
   $store = $this->_storeRepository->getActiveStoreByCode($toStore);
   $this->_httpContext->setValue(Store::ENTITY, $toStore, $fromStore);
   $this->storeCookieManager->setStoreCookie($store);   
   $result = $this->_resultJsonFactory->create();       
   $message  = __('Contact Us');    
   return $result->setData(array(
                    'success' => 1,
                    'error' => 0,
                    'message' => $message
                   ));   

 }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.