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 ?

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.

|improve this answer|||||

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.