Magento翻译在联机程序中正常,但不能作为cronjob运行
我创建了一个模块(扩展了Mage_Core_Model_Abstract)和一个管理控制器 当我运行这个模块时,在线翻译是正确的 当我以cronjob的形式运行这个模块时,一切正常,但翻译没有完成,我在config.xml中指定了翻译文件,在frontend中指定了adminhtmlMagento翻译在联机程序中正常,但不能作为cronjob运行,magento,module,cron,translation,Magento,Module,Cron,Translation,我创建了一个模块(扩展了Mage_Core_Model_Abstract)和一个管理控制器 当我运行这个模块时,在线翻译是正确的 当我以cronjob的形式运行这个模块时,一切正常,但翻译没有完成,我在config.xml中指定了翻译文件,在frontend中指定了adminhtml 我做错了什么?即使使用CE1.9.0.1,这也是一个bug 看看我对此做了什么: 我知道这是一个很老的问题。我已经在这里发布给未来的参考和其他人 快速而肮脏的解决方案 // Fix unitialized tra
我做错了什么?即使使用
CE1.9.0.1
,这也是一个bug
看看我对此做了什么:
我知道这是一个很老的问题。我已经在这里发布给未来的参考和其他人
快速而肮脏的解决方案
// Fix unitialized translator
Mage::app()->getTranslator()->init('frontend', true);
刚过
$initialEnvironmentInfo = $appEmulation>startEnvironmentEmulation($storeId);
比如说。或者在自己的foreach
循环中,通过cron/admin调用。既然你说的是cron,我想你知道自己在做什么
真正的问题
在Magento 1.9的Mage\u Core\u Model\u App\u 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。当迭代顺序为/customers/subscribers/*时,区域设置可以保持不变,从而阻止执行语句中的代码。因此,不会在转换器中设置区域设置(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;
}
Magento 2
我猜开发人员意识到它被破坏了,他们在Magento 2中更改了代码。\u emulateScale()
函数一起消失了,他们在startEnvironmentEmulation()
函数中添加了这一行,没有任何条件:
$this->_localeResolver->setLocale($newLocaleCode);
我认为这是一个bug,很难调试cron作业。我还没有找到使用翻译的cronjobs。因此,与此同时,我通过在我自己的Helper_数据类中定义我自己的公共函数uuuuuu()来修复它。我检索$locale=Mage::app()->getLocale()->getLocaleCode();并在此功能中努力完成我自己的翻译..这是针对1.5版的,可能在下一个版本中可以使用。。