2

I have extended the Magento v2 API and want to return some translated strings. The only thing that is returned, are the original strings.

To test the translation, I also added the strings to the frontend, it worked.

    foreach (Mage::helper('mb-rentalapp')->getInterfaceStrings() as $interface_string)
    {
      echo $interface_string . '::' . Mage::helper('mb-rentalapp')->__($interface_string) . '</br>' ;
    }

In the API, I added the following to the response.

Mage::helper('mb-rentalapp')->__($interface_string)

After some debugging, I noticed that the Mage_Core_Model_Config_Element returned

Mage_Core_Model_Translate->getModulesConfig()

is populated in frontend, but completely empy in the API v2.

Before adding the translation in the response, I also ran:

Mage::app()->setCurrentStore($storeView->getId());
Mage::app()->getTranslator()->init()->setLocale($storeView->getConfig('general/locale/code'));

Is it even possible to translate in the Magento v2 API?

3

As Alan mentioned above, the API runs with storeId 0. So I guess if you run Mage::app()->setCurrentStore($storeView->getId()); you translate with storeId 0 also (btw: where do you get $storeView from?).

If you extended the API, you might also add a seperate function or a store view/id parameter to your functions to translate the strings in the store view language you want.

This might be interesting for you to have a look at:

2

I've never run into this specifically, but I do know the API runs with a store id of 0 by default, which means any configuration or locale settings you grab will be for the admin console. I'd check the values you're grabbing from $storeView->getId() and $storeView->getConfig('general/locale/code'), as well as retrace the code with the fact Magento's running in a "not admin but zero store id" state during API requests.

  • For now, I've settled with no translations. Best option is probably trying to find out why there are no translate csv files in the config array. – Mondane Aug 21 '13 at 6:42
1

You should run this line at some point point before your translation is needed :

Mage::app()->getTranslator()->init($area, true);

$area should contain the area from which to load transation. If your module config.xml has the <translate> key inside <adminhtml> for example, put adminhtml in it.

Set the second param to true to force Magento to reload translation (aka not use cache).

Worked for me.

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.