Skip to content

Commit

Permalink
Added setTranslationHelper method to Mage_Core_Block_Abstract (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbeaty authored Oct 23, 2024
1 parent cb9ec68 commit e759006
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
34 changes: 34 additions & 0 deletions app/code/core/Mage/Core/Block/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ abstract class Mage_Core_Block_Abstract extends Varien_Object
*/
protected $_app;

/**
* Translation helper instance
*
* @var Mage_Core_Helper_Abstract|null
*/
protected $_translationHelper = null;

/**
* Initialize factory instance
*/
Expand Down Expand Up @@ -1165,6 +1172,30 @@ public function getModuleName()
return $module;
}

/**
* Helper getter for translations
*
* @return Mage_Core_Helper_Abstract
*/
public function getTranslationHelper()
{
return $this->_translationHelper ?? $this->helper('core');
}

/**
* Helper setter for translations
*
* @param Mage_Core_Helper_Abstract $helper
* @return $this
*/
public function setTranslationHelper($helper)
{
if ($helper instanceof Mage_Core_Helper_Abstract) {
$this->_translationHelper = $helper;
}
return $this;
}

/**
* Translate block sentence
*
Expand All @@ -1173,6 +1204,9 @@ public function getModuleName()
public function __()
{
$args = func_get_args();
if ($this->_translationHelper instanceof Mage_Core_Helper_Abstract) {
return $this->_translationHelper->__(...$args);
}
$expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->getModuleName());
array_unshift($args, $expr);
return $this->_getApp()->getTranslator()->translate($args);
Expand Down
30 changes: 14 additions & 16 deletions app/code/core/Mage/Widget/Block/Adminhtml/Widget/Chooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
* @method $this setElement(Varien_Data_Form_Element_Abstract $value)
* @method $this setFieldsetId(string $value)
* @method string getLabel()
* @method $this setTranslationHelper(Mage_Core_Helper_Abstract $value)
* @method $this setSourceUrl(string $value)
* @method $this setUniqId(string $value)
*/
class Mage_Widget_Block_Adminhtml_Widget_Chooser extends Mage_Adminhtml_Block_Template
{
/**
* Internal constructor, that is called from real constructor
*
* @return void
*/
#[\Override]
protected function _construct()
{
parent::_construct();
$this->setTranslationHelper($this->helper('widget'));
}

/**
* Chooser source URL getter
*
Expand Down Expand Up @@ -66,7 +77,7 @@ public function getConfig()

// define chooser label
if (isset($configArray['label'])) {
$config->setData('label', $this->getTranslationHelper()->__($configArray['label']));
$config->setData('label', $this->__($configArray['label']));
}

// chooser control buttons
Expand All @@ -76,27 +87,14 @@ public function getConfig()
];
if (isset($configArray['button']) && is_array($configArray['button'])) {
foreach ($configArray['button'] as $id => $label) {
$buttons[$id] = $this->getTranslationHelper()->__($label);
$buttons[$id] = $this->__($label);
}
}
$config->setButtons($buttons);

return $this->_getData('config');
}

/**
* Helper getter for translations
*
* @return Mage_Core_Helper_Abstract
*/
public function getTranslationHelper()
{
if ($this->_getData('translation_helper') instanceof Mage_Core_Helper_Abstract) {
return $this->_getData('translation_helper');
}
return $this->helper('widget');
}

/**
* Unique identifier for block that uses Chooser
*
Expand Down
18 changes: 6 additions & 12 deletions app/code/core/Mage/Widget/Block/Adminhtml/Widget/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ class Mage_Widget_Block_Adminhtml_Widget_Options extends Mage_Adminhtml_Block_Wi
*/
protected $_defaultElementType = 'text';

/**
* Translation helper instance, defined by the widget type declaration root config node
* @var Mage_Core_Helper_Abstract
*/
protected $_translationHelper = null;

/**
* Prepare Widget Options Form and values according to specified type
*
Expand Down Expand Up @@ -101,14 +95,14 @@ public function addFields()
{
// get configuration node and translation helper
if (!$this->getWidgetType()) {
Mage::throwException($this->__('Widget Type is not specified'));
Mage::throwException($this->helper('widget')->__('Widget Type is not specified'));
}
$config = Mage::getSingleton('widget/widget')->getConfigAsObject($this->getWidgetType());
if (!$config->getParameters()) {
return $this;
}
$module = $config->getModule();
$this->_translationHelper = Mage::helper($module ? $module : 'widget');
$this->setTranslationHelper(Mage::helper($module ?: 'widget'));
foreach ($config->getParameters() as $parameter) {
$this->_addField($parameter);
}
Expand All @@ -131,10 +125,10 @@ protected function _addField($parameter)
$fieldName = $parameter->getKey();
$data = [
'name' => $form->addSuffixToName($fieldName, 'parameters'),
'label' => $this->_translationHelper->__($parameter->getLabel()),
'label' => $this->__($parameter->getLabel()),
'required' => $parameter->getRequired(),
'class' => 'widget-option',
'note' => $this->_translationHelper->__($parameter->getDescription()),
'note' => $this->__($parameter->getDescription()),
];

if ($values = $this->getWidgetValues()) {
Expand All @@ -153,7 +147,7 @@ protected function _addField($parameter)
$data['values'] = [];
foreach ($values as $option) {
$data['values'][] = [
'label' => $this->_translationHelper->__($option['label']),
'label' => $this->__($option['label']),
'value' => $option['value']
];
}
Expand Down Expand Up @@ -184,7 +178,7 @@ protected function _addField($parameter)
if ($helperBlock instanceof Varien_Object) {
$helperBlock->setConfig($helper->getData())
->setFieldsetId($fieldset->getId())
->setTranslationHelper($this->_translationHelper)
->setTranslationHelper($this->getTranslationHelper())
->prepareElementHtml($field);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Widget/Model/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getConfigAsObject($type)
$object->setData($xml->asCanonicalArray());

// Set module for translations etc.
$module = $object->getData('@/module');
$module = $xml->getAttribute('module');
if ($module) {
$object->setModule($module);
}
Expand Down

0 comments on commit e759006

Please sign in to comment.