Skip to content

Commit

Permalink
Uses translations and number formatting based on config solves #73
Browse files Browse the repository at this point in the history
  • Loading branch information
madalinoprea committed Mar 16, 2016
1 parent f72edf6 commit 4aed503
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
42 changes: 38 additions & 4 deletions code/Debug/Block/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ public function getShowTemplateHints()
}


/**
* @param array $args
* @return mixed
*/
public function parentTranslate(array $args)
{
return call_user_func_array(array('parent', '__'), $args);
}


/**
* @param array $args
* @return mixed
*/
public function dummyTranslate(array $args)
{
$text = array_shift($args);
$result = @vsprintf($text, $args);
return $result ?: $text;
}


/**
* Don't use translation for our debug module
*
* @return mixed
*/
public function __()
{
$args = func_get_args();
return $this->helper->useStoreLocale() ? $this->parentTranslate($args) : $this->dummyTranslate($args);
}


/**
* Returns registered request info or request profile for current request
*
Expand Down Expand Up @@ -134,18 +168,18 @@ public function getRequestViewUrl($panel = null, $token = null)
/**
* Returns number formatted based on current locale
*
* @param $number
* @param int $precision
* @param mixed $number
* @param int $precision
* @return string
*/
public function formatNumber($number, $precision = 2)
{
return $this->helper->formatNumber($number, $precision);
return $this->helper->useStoreLocale() ? $this->helper->formatNumber($number, $precision) : number_format($number, $precision);
}


/**
* Returns an option array where evey element has its value and label filled in
* Returns an option array where every element has its value and label filled in
* with elements from passed array
*
* @param array $data
Expand Down
13 changes: 13 additions & 0 deletions code/Debug/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Sheep_Debug_Helper_Data extends Mage_Core_Helper_Data
const DEBUG_OPTION_PERSIST_PATH = 'sheep_debug/options/persist';
const DEBUG_OPTION_PERSIST_EXPIRATION_PATH = 'sheep_debug/options/persist_expiration';
const DEBUG_OPTION_FORCE_VARIEN_PROFILE_PATH = 'sheep_debug/options/force_varien_profile';
const DEBUG_OPTION_USE_STORE_LOCALE = 'sheep_debug/options/use_store_locale';


/**
Expand Down Expand Up @@ -464,6 +465,18 @@ public function canEnableVarienProfiler()
}


/**
* Checks if we need to translate or format extension content based on
* store locale
*
* @return bool
*/
public function useStoreLocale()
{
return (bool)Mage::getStoreConfig(self::DEBUG_OPTION_USE_STORE_LOCALE);
}


/**
* Returns Magento configuration instance
*
Expand Down
60 changes: 59 additions & 1 deletion code/Debug/Test/Block/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@ public function testGetShowTemplateHints()
}


public function testParentTranslate()
{
$this->assertEquals('simple text', $this->block->parentTranslate(array('simple text')));
$this->assertEquals('simple text 10 20.9', $this->block->parentTranslate(array('simple text %d %0.1f', 10, 20.90)));
}


public function testDummyTranslate()
{
$this->assertEquals('simple text', $this->block->dummyTranslate(array('simple text')));
$this->assertEquals('simple text 10 20.9', $this->block->dummyTranslate(array('simple text %d %0.1f', 10, 20.90)));
}


public function testTranslate()
{
$helper = $this->getHelperMock('sheep_debug', array('useStoreLocale'));
$helper->expects($this->once())->method('useStoreLocale')->willReturn(false);
$this->replaceByMock('helper', 'sheep_debug', $helper);

$block = $this->getBlockMock('sheep_debug/abstract', array('parentTranslate', 'dummyTranslate'));
$block->expects($this->never())->method('parentTranslate');
$block->expects($this->once())->method('dummyTranslate')->with(array('some text', 10, 20))->willReturn('translated text');;

$actual = $block->__('some text', 10, 20);
$this->assertEquals('translated text', $actual);
}


public function testTranslateWithStoreLocale()
{
$helper = $this->getHelperMock('sheep_debug', array('useStoreLocale'));
$helper->expects($this->once())->method('useStoreLocale')->willReturn(true);
$this->replaceByMock('helper', 'sheep_debug', $helper);

$block = $this->getBlockMock('sheep_debug/abstract', array('parentTranslate', 'dummyTranslate'));
$block->expects($this->once())->method('parentTranslate')->with(array('some text', 10, 20))->willReturn('translated text');
$block->expects($this->never())->method('dummyTranslate');

$actual = $block->__('some text', 10, 20);
$this->assertEquals('translated text', $actual);
}


public function testGetRequestInfo()
{
$requestInfoMock = $this->getModelMock('sheep_debug/requestInfo');
Expand Down Expand Up @@ -118,7 +162,21 @@ public function testGetRequestViewUrlWithoutToken()

public function testFormatNumber()
{
$helper = $this->getHelperMock('sheep_debug', array('formatNumber'));
$helper = $this->getHelperMock('sheep_debug', array('useStoreLocale', 'formatNumber'));
$helper->expects($this->once())->method('useStoreLocale')->willReturn(false);
$helper->expects($this->never())->method('formatNumber');
$this->replaceByMock('helper', 'sheep_debug', $helper);

$block = $this->getBlockMock('sheep_debug/abstract', array('_toHtml'));
$actual = $block->formatNumber(10.3333);
$this->assertEquals(10.33, $actual);
}


public function testFormatNumberWithStoreLocale()
{
$helper = $this->getHelperMock('sheep_debug', array('useStoreLocale', 'formatNumber'));
$helper->expects($this->once())->method('useStoreLocale')->willReturn(true);
$helper->expects($this->once())->method('formatNumber')->with(10.3333, 2)->willReturn(10.33);
$this->replaceByMock('helper', 'sheep_debug', $helper);

Expand Down
1 change: 1 addition & 0 deletions code/Debug/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<persist>1</persist>
<persist_expiration>1</persist_expiration>
<force_varien_profile>1</force_varien_profile>
<use_store_locale>0</use_store_locale>
</options>
</sheep_debug>
</default>
Expand Down
10 changes: 10 additions & 0 deletions code/Debug/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</force_varien_profile>
<use_store_locale>
<label>Use store locale settings</label>
<comment>If enabled we're going to translate toolbar labels.</comment>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</use_store_locale>
</fields>
</options>
</groups>
Expand Down

0 comments on commit 4aed503

Please sign in to comment.