3

I want to know how to update some translation through Magento 2.1.9 REST API.

For example, when I add a configurable product in the cart thanks to the Magento 2.1.9 REST API, if the user do not select an option, the default error message response is You need to choose options for your item.

Which is translated here vendor/magento/module-configurable-product/i18n/en_US.csv:30

And trigger here : vendor/magento/module-configurable-product/Model/Product/Type/Configurable.php:1095

I want to update the default translation, but I do not find a way to solve it, I try :

  • I added the updated translation in my theme : app/design/frontend/Vendor/theme/i18n/en_US.csv:5 but it seems that the theme is not load in the API mode
  • I forced the current area and store with $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);

Without success.

I do not find any resource how to translate through API area.

I prefer keep all the translation in Magento and not delegate this to the frontend application.

All ideas are appreciated.

EDIT:

I just found a workaround.

You need to emulate the correct store with the frontend area, and then reload the translation data :

// Emulate store frontend
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);

$this->translator->loadData(null, true);
echo __('your text to translate');
$this->appEmulation->stopEnvironmentEmulation();

But it is just a workaround for custom API where you manage the result, not for native Magento API.

Let me know if there is a proper way to manage this.

Does language packages can solve my problem ?

4
  • You can added a store code in the API URL, So it'll translate content based on the store view locale. Eg : en is store code, yourdomain.com/en/rest/V1/your/url Commented Nov 8, 2017 at 9:00
  • It is not the problem, the content is already translated correctly from magento native translation file. The question is how to overwride the default translation. Commented Nov 8, 2017 at 10:13
  • Create a new language pack and extend vendor("en_US") language pack. Refer this [link] (devdocs.magento.com/guides/v2.0/config-guide/cli/…). So you dont need to emulate the frontend in API. Commented Nov 8, 2017 at 12:34
  • Ok I will try this, do not hesitate to answer to have a bounty. Commented Nov 9, 2017 at 8:25

1 Answer 1

2
+100

I think in API request, system will not load any theme related stuffs(style, locale, etc).

So you can create a new language pack and extend the vendor language. Refer this link

language.xml

<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
    <code>en_US</code>
    <vendor>your</vendor>
    <package>language_pack</package>
    <sort_order>100</sort_order>
    <use vendor="Magento" package="en_US"/>
</language>

Magento 2.1.9 : link

Theme locale will get overridden by the language pack.

    $this->_loadModuleTranslation();
    $this->_loadThemeTranslation();
    $this->_loadPackTranslation();
    $this->_loadDbTranslation();

Magento 2.2 : link

theme locale will not get overridden by the language pack.

    $this->_loadModuleTranslation();
    $this->_loadPackTranslation();
    $this->_loadThemeTranslation();
    $this->_loadDbTranslation();

I hope this will fix your issue.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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