Skip to content

Commit

Permalink
[issue/15] translation fix for translate.csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantus committed Apr 30, 2017
1 parent 87356ea commit 58067ed
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Ecocode_Profiler_Model_Collector_TranslationDataCollector
protected $_currentBlock;

protected $stateCounts = [
'translated' => 0,
'missing' => 0,
'invalid' => 0,
'fallback' => 0
'translated' => 0,
'missing' => 0,
'invalid' => 0,
'fallback' => 0
];

/**
Expand Down Expand Up @@ -94,8 +94,8 @@ public function getStateCount($status = null)
public function getNotOkCount()
{
return $this->getStateCount('invalid')
+ $this->getStateCount('missing')
+ $this->getStateCount('fallback');
+ $this->getStateCount('missing')
+ $this->getStateCount('fallback');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,71 @@
*/
class Mage_Core_Model_Translate extends Original_Mage_Core_Model_Translate
{
const STATE_TRANSLATED = 'translated';
const STATE_FALLBACK = 'fallback';
const STATE_MISSING = 'missing';
const STATE_INVALID = 'invalid';

const STATE_TRANSLATED = 'translated';
const STATE_FALLBACK = 'fallback';
const STATE_MISSING = 'missing';
const STATE_INVALID = 'invalid';

protected $profilerConfig = null;

protected $currentMessage = null;

protected $messages = [];

protected $source = [];

protected $sourceMap = [
'_loadModuleTranslation' => 'module',
'_loadThemeTranslation' => 'theme',
'_loadDbTranslation' => 'db',
];


public function init($area, $forceReload = false)
{
parent::init($area, $forceReload);
}

protected function _loadCache()
{
if (!$this->_canUseCache()) {
return false;
}

$cacheData = Mage::app()->loadCache($this->getCacheId());
if ($cacheData === false) {
return false;
}

$cacheData = unserialize($cacheData);
if (!isset($cacheData['source'])) {
return false;
}

$this->source = $cacheData['source'];
return $cacheData['data'];
}

/**
* Saving data cache
*
* @param string $area
* @return Mage_Core_Model_Translate
*/
protected function _saveCache()
{
if (!$this->_canUseCache()) {
return $this;
}
$data = [
'data' => $this->getData(),
'source' => $this->source
];
Mage::app()->saveCache(serialize($data), $this->getCacheId(), [self::CACHE_TAG], null);
return $this;
}


public function translate($args)
{
Expand Down Expand Up @@ -61,12 +115,15 @@ public function _getTranslatedString($text, $code)
{
$this->currentMessage['text'] = $text;
$this->currentMessage['code'] = $code;
$source = null;

$translated = parent::_getTranslatedString($text, $code);
if (array_key_exists($code, $this->_data)) {
$state = self::STATE_TRANSLATED;
$state = self::STATE_TRANSLATED;
$source = $this->source[$code];
} elseif (array_key_exists($text, $this->_data)) {
$state = self::STATE_FALLBACK;
$source = $this->source[$text];
$state = self::STATE_FALLBACK;
$this->addTrace();
} else {
$state = self::STATE_MISSING;
Expand All @@ -75,6 +132,7 @@ public function _getTranslatedString($text, $code)

$this->currentMessage['state'] = $state;
$this->currentMessage['translation'] = $translated;
$this->currentMessage['source'] = $source;

return $translated;
}
Expand All @@ -94,6 +152,7 @@ protected function log()
'state' => $this->currentMessage['state'],
'parameters' => $this->currentMessage['parameters'],
'module' => $this->currentMessage['module'],
'source' => $this->currentMessage['source'],
'trace' => $this->currentMessage['trace']
];
}
Expand All @@ -108,20 +167,32 @@ protected function log()
*/
protected function _addData($data, $scope, $forceReload = false)
{
foreach ($data as $key => $value) {
$source = debug_backtrace()[1]['function'];
if (isset($this->sourceMap[$source])) {
$source = $this->sourceMap[$source];
}

/*
we needed to simplify this to properly detect not translated strings and their scope
*/
foreach ($data as $key => $value) {
$key = $this->_prepareDataString($key);
$value = $this->_prepareDataString($value);

$scopeKey = $scope . self::SCOPE_SEPARATOR . $key;
$scopeKey = $key;
$sourceName = $source;
if ($scope) {
$scopeKey = $scope . self::SCOPE_SEPARATOR . $key;
$sourceName .= ' (' . $scope . ')';
} else {
// we have no scope key so this is coming from translate.csv or db in this case magento overwrites
// the translation in any case
}

$this->_data[$scopeKey] = $value;
$this->_data[$scopeKey] = $value;
$this->source[$scopeKey] = $sourceName;
if (!isset($this->_data[$key]) && !Mage::getIsDeveloperMode()) {
$this->_data[$key] = $value;
$this->_data[$key] = $value;
$this->source[$key] = $sourceName;
}
continue;
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ $collector = $this->getCollector();
</div>



<h2>Translation Messages</h2>

<?php $messageGroups = $this->getMessageGroups();?>
<p class="help">Its important to understand how magento does handle translations and why your translations in the
"translate.csv" are not always working. In case you have this kind of issue check out
<a target="_blank" href="https://magento.stackexchange.com/questions/28720/why-does-my-translate-csv-and-the-translation-files-not-work">magento.stackexchange</a>
</p>
<?php $messageGroups = $this->getMessageGroups(); ?>
<div class="sf-tabs">
<div class="tab">
<h3 class="tab-title">Defined <span class="badge"><?php echo count($messageGroups['translated']) ?></span></h3>
Expand Down Expand Up @@ -115,7 +117,7 @@ $collector = $this->getCollector();

<style>
tr.sf-toggle-content.sf-toggle-visible {
display: table-row!important;
display: table-row !important;
}

</style>
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $rendererHelper = Mage::helper('ecocode_profiler/renderer');
<tr>
<th>Locale</th>
<th>Module</th>
<th>Used Source</th>
<th>Times used</th>
<th>Message ID</th>
<th>Message Preview</th>
Expand All @@ -23,6 +24,7 @@ $rendererHelper = Mage::helper('ecocode_profiler/renderer');
<tr>
<td class="font-normal text-small"><?php echo $message['locale'] ?></td>
<td class="font-normal text-small text-bold nowrap"><?php echo $message['module'] ?></td>
<td class="font-normal text-small text-bold nowrap"><?php echo isset($message['source']) ? $message['source'] : '-' ?></td>
<td class="font-normal text-small"><?php echo $message['count'] ?></td>
<td>
<?php echo $message['code'] ?>
Expand Down

0 comments on commit 58067ed

Please sign in to comment.