2

I created a module (extends Mage_Core_Model_Abstract) and an admin controller.

When I run this module online translations are going right.

When I run this module as cronjob, everything goes allright but translations are not done, I specified translation file in config.xml in as well frontend as adminhtml.

What I am doing wrong?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Francois99
  • 73
  • 1
  • 1
  • 10
  • I think this is a bug, difficult to debug a cron job. I have not find cronjobs with translations used. So meanwhile I fixed this by defining my own public function __() in my own _Helper_Data class. I retrieve $locale = Mage::app()->getLocale()->getLocaleCode(); and do my own translations hard in this function .. – Francois99 Nov 13 '13 at 19:24
  • This was for release 1.5, maybe this works in next releases .. – Francois99 Nov 14 '13 at 21:45

2 Answers2

3

I see this is a very old question. I've posted here to future reference and others.


Quick and dirty solution

// Fix unitialized translator
Mage::app()->getTranslator()->init('frontend', true);

just after

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

for instance. Or in a foreach loop of your own, which is called via cron/admin. Since you're talking about crons, I assume that you know what you are doing.

The real problem

In Magento 1.9 in Mage_Core_Model_App_Emulation (in app/code/core/Mage/Core/Model/App/Emulation.php), there's this function:

/**
 * Apply locale of the specified store
 *
 * @param integer $storeId
 * @param string $area
 *
 * @return string initial locale code
 */
protected function _emulateLocale($storeId, $area = Mage_Core_Model_App_Area::AREA_FRONTEND)
{
    $initialLocaleCode = $this->_app->getLocale()->getLocaleCode();
    $newLocaleCode = $this->_getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId);
    if ($initialLocaleCode != $newLocaleCode) {
        $this->_app->getLocale()->setLocaleCode($newLocaleCode);
        $this->_factory->getSingleton('core/translate')->setLocale($newLocaleCode)->init($area, true);
    }
    return $initialLocaleCode;
}

The $initialLocaleCode != $newLocaleCode seems to be the issue here. When iteration orders/customers/subscribers/*, the locale could stay the same, which then prevents executing the code in the statement. And the locale is thus not set in the Translator (Mage::app()->getTranslator()).

We've yet to fix the issue, but you could change if ($initialLocaleCode != $newLocaleCode) { to if (true) { straight in the core source. Off course, this is ugly. I suggest something like extending the class and then :

/**
 * Apply locale of the specified store. Extended
 * to fix Magento's uninitialized translator.
 *
 * @see http://stackoverflow.com/questions/19940733/magento-translations-ok-in-online-program-but-not-run-as-cronjob#
 * @param integer $storeId
 * @param string $area
 *
 * @return string initial locale code
 */
protected function _emulateLocale($storeId, $area = Mage_Core_Model_App_Area::AREA_FRONTEND)
{
    $initialLocaleCode = $this->_app->getLocale()->getLocaleCode();
    $newLocaleCode = $this->_getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId);

    $this->_app
        ->getLocale()
        ->setLocaleCode($newLocaleCode);

    $this->_factory
        ->getSingleton('core/translate')
        ->setLocale($newLocaleCode)
        ->init($area, true);

    return $initialLocaleCode;
}

Magento 2

I guess the developers became aware it was borked and they changed the code in Magento 2. The _emulateLocale() function is gone all together and they added this line to the startEnvironmentEmulation() function, without any conditional around it:

$this->_localeResolver->setLocale($newLocaleCode);
leeuwd
  • 66
  • 6
0

It's a bug even with CE 1.9.0.1!

See what I've done about it:

https://magento.stackexchange.com/questions/25612/cron-job-template-block-not-being-translated-but-testobserver-is/25920#25920

Community
  • 1
  • 1
Niloct
  • 9,491
  • 3
  • 44
  • 57