4

I am configuring a product out of stock notification / alert, and I'm having an issue with localization / translation (pt_BR) in Magento CE 1.9.0.1.

The email/productalert/stock.phtml block isn't being translated when called by cron job. Nevertheless, when I call /productalert/add/testObserver it gets translated!

I am certain that when called by cron:

  1. Module name is Mage_ProductAlert;
  2. Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId()); equals pt_BR;
  3. $locale = Mage::app()->getLocale()->getLocaleCode(); equals the same, pt_BR;

Any hints ? This is happening in core code, without any out of stock extensions.

Thanks!

10

Well, this is a bug indeed, but I came up with a fix!

The problem is that Mage_Core_Model_Translate translator isn't initialized by cron.php, since this script doesn't load() translate application area...

The fix is to initialize the translator after emulation, on Mage_ProductAlert_Model_Email::send:

$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

//Dealing with uninitialized translator!
Mage::app()->getTranslator()->init('frontend', true);

I filled a bug with Magento so probably soon this patch will not be needed.

  • Patched in lasted version of Magento 1.9.x. – GiDo Dec 2 '15 at 21:31
  • I found a similair issue with Mage_Sales_Model_Order_Shipment::sendEmail(). Thanks for the hint. It fix my issue! – GiDo Dec 2 '15 at 21:32
2

I think cronjobs run in admin store.

You could try a store emulation:

$emulation = Mage::getModel('core/app_emulation');
$oldEnvironment = $emulation->startEnvironmentEmulation(
        $storeId,
        Mage_Core_Model_App_Area::AREA_FRONTEND,
        true
    );

//do the stuff

$emulation->stopEnvironmentEmulation($oldEnvironment);
  • You may be right! Mage::app()->addEventArea('crontab'); is set on cron.php. There must be a reason for that... – Niloct Jul 1 '14 at 13:04
  • I changed cron.php, added your snippet, and also added a Mage::log to Mage_Core_Model_app::dispatchEvent, $area is being assigned global, or crontab, but not frontend. The translation still doesn't work. – Niloct Jul 1 '14 at 13:34
  • Magento uses this for sending order mails from the admin area. It just changes the design and the translations, not the current area. Have a look in \Mage_Sales_Model_Order::sendNewOrderEmail for this example. Did you instantiate the block after setting the environment emulation? – Thorsten Jul 1 '14 at 13:51
  • I just noticed that the Mage_ProductAlert_Model_Email::send around line 255 already was doing what you've suggested. – Niloct Jul 1 '14 at 14:49

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.