Magento翻译在在线程序中正常,但不能作为cronjob运行



我创建了一个模块(扩展Mage_Core_Model_Abstract)和一个管理控制器。

当我运行此模块时,在线翻译正常。

当我将此模块作为 cronjob 运行时,一切正常,但翻译没有完成,我在 config 中指定了翻译文件.xml以及前端和 adminhtml。

我做错了什么?

我认为这是一个非常古老的问题。我已在此处发布以供将来参考和其他人使用。

<小时 />

快速和肮脏的解决方案

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

刚过之后

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

例如。或者在您自己的foreach循环中,通过 cron/admin 调用。既然你说的是crons,我假设你知道你在做什么。

真正的问题

在 Magento 1.9 in Mage_Core_Model_App_Emulation 中(在 app/code/core/Mage/Core/Model/App/Emulation.php 年),有这个函数:

/**
 * 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;
}

$initialLocaleCode != $newLocaleCode似乎是这里的问题。当迭代订单/客户/订阅者/* 时,区域设置可能保持不变,这会阻止执行语句中的代码。因此,语言环境不会在翻译器(Mage::app()->getTranslator())中设置。

我们尚未解决此问题,但您可以将if ($initialLocaleCode != $newLocaleCode) {更改为直接在核心源中if (true) {。当然,这很丑陋。我建议扩展类,然后:

/**
 * 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;
}

马真托 2

我猜开发人员意识到它被无聊了,他们更改了Magento 2中的代码。_emulateLocale()函数一起消失了,他们将这一行添加到startEnvironmentEmulation()函数中,没有任何条件:

$this->_localeResolver->setLocale($newLocaleCode);

即使有CE 1.9.0.1也是一个错误!

看看我做了什么:

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

最新更新