3

I have enabled inline translate on the front end and can see the red box and book around certain elements.

In my .phtml file I have <h3><?php echo $this->__('my custom text');?></h3>. I can change this by .csv (located in /app/design/frontend/package/theme/locale/en_GB/translate.csv) but I cannot get the inline translate to work.

The block type is core/template.

How do I get Magento to allow me to alter this text via front-end inline translate?

Other posts suggest wrapping the text in span tags. I have done this and this has not helped.

I have added the text and translation to the core_translate table but that was not being detected.

  • this actually should work (most common answer from IT guys mabe :D). In the past, we had some issues with some browsers where the Inline translation did not work. Very far shot although. – David Verholen Mar 4 '16 at 17:48
  • Thanks for the suggestion. I have tried my page in Firefox, Chrome and IE11 and the magento components show the inline translate fine (My Account, Sign In, View Basket, etc), but my custom header does not. – Richard Parnaby-King Mar 7 '16 at 9:14
6
+50

To allow the inline translation, Magento uses the isAllowed method of Mage_Core_Model_Translate_Inline class, which basically does the following:

  • check if the inline translation is allowed in the backend for the current area (adminhtml or frontend)
  • check if the inline translation has not been disabled via setTranslateInline(false)

In your case, as you seem to see other red boxes, those two conditions are true.

To add the red box, Magento uses a data-translate attribute on the tag, then the TranslateInline JS class adds the red box and the little book so you can process the translation via AJAX.

To me, it looks like the problem is caused by the data-translate attribute not being added to your tag. Check your h3 tag and see if a data-translate attribute is present, it should look like this:

data-translate="[{"shown":"my custom text","translated":"my custom text","original":"my custom text","location":"Heading level 3","scope":"Mage_Something"}]"

If it's not present then the problem can be debugged under Mage_Core_Model_Translate_Inline in the _prepareTagAttributesForContent method as this is the method that adds the data-translate attribute to your tag:

protected function _prepareTagAttributesForContent(&$content)
{
    if ($this->getIsJson()) {
        $quoteHtml   = '\"';
    } else {
        $quoteHtml   = '"';
    }

    $tagMatch   = array();
    $nextTag    = 0;
    $tagRegExp = '#<([a-z]+)\s*?[^>]+?((' . $this->_tokenRegex . ')[^>]*?)+\\\\?/?>#iS';
    while (preg_match($tagRegExp, $content, $tagMatch, PREG_OFFSET_CAPTURE, $nextTag)) {
        $tagHtml    = $tagMatch[0][0];
        $m          = array();
        $attrRegExp = '#' . $this->_tokenRegex . '#S';
        $trArr = $this->_getTranslateData($attrRegExp, $tagHtml, array($this, '_getAttributeLocation'));
        if ($trArr) {
            $transRegExp = '# data-translate=' . $quoteHtml . '\[([^'.preg_quote($quoteHtml).']*)]' . $quoteHtml . '#i';
            if (preg_match($transRegExp, $tagHtml, $m)) {
                $tagHtml = str_replace($m[0], '', $tagHtml); //remove tra
                $trAttr  = ' data-translate=' . $quoteHtml
                    . htmlspecialchars('[' . $m[1] . ',' . join(',', $trArr) . ']') . $quoteHtml;
            } else {
                $trAttr  = ' data-translate=' . $quoteHtml
                    . htmlspecialchars('[' . join(',', $trArr) . ']') . $quoteHtml;
            }
            $tagHtml = substr_replace($tagHtml , $trAttr, strlen($tagMatch[1][0])+1, 1);
            $content = substr_replace($content, $tagHtml, $tagMatch[0][1], strlen($tagMatch[0][0]));
        }
        $nextTag = $tagMatch[0][1] + strlen($tagHtml);
    }
}

While this method can look complex, I suggest you add several logs to it in order to debug your issue, here a modified version I made in order to help you fix your problem:

protected function _prepareTagAttributesForContent(&$content)
{
    Mage::log($content);
    if ($this->getIsJson()) {
        $quoteHtml   = '\"';
    } else {
        $quoteHtml   = '"';
    }

    $tagMatch   = array();
    $nextTag    = 0;
    $tagRegExp = '#<([a-z]+)\s*?[^>]+?((' . $this->_tokenRegex . ')[^>]*?)+\\\\?/?>#iS';
    if (preg_match($tagRegExp, $content) === false) {
         Mage::log("this content does not match");
    } else {
         Mage::log("this content matches");
    }

    while (preg_match($tagRegExp, $content, $tagMatch, PREG_OFFSET_CAPTURE, $nextTag)) {
        $tagHtml    = $tagMatch[0][0];
        Mage::log("tag html: ".$tagHtml);
        $m          = array();
        $attrRegExp = '#' . $this->_tokenRegex . '#S';
        $trArr = $this->_getTranslateData($attrRegExp, $tagHtml, array($this, '_getAttributeLocation'));
        if ($trArr) {
            Mage::log("translated data");
            Mage::log($trArr);
            $transRegExp = '# data-translate=' . $quoteHtml . '\[([^'.preg_quote($quoteHtml).']*)]' . $quoteHtml . '#i';
            if (preg_match($transRegExp, $tagHtml, $m)) {
                $tagHtml = str_replace($m[0], '', $tagHtml); //remove tra
                $trAttr  = ' data-translate=' . $quoteHtml
                    . htmlspecialchars('[' . $m[1] . ',' . join(',', $trArr) . ']') . $quoteHtml;
            } else {
                $trAttr  = ' data-translate=' . $quoteHtml
                    . htmlspecialchars('[' . join(',', $trArr) . ']') . $quoteHtml;
            }
            $tagHtml = substr_replace($tagHtml , $trAttr, strlen($tagMatch[1][0])+1, 1);
            $content = substr_replace($content, $tagHtml, $tagMatch[0][1], strlen($tagMatch[0][0]));
        } else {
            Mage::log("no translated data for this tag");
        }
        $nextTag = $tagMatch[0][1] + strlen($tagHtml);
    }
}

With the use of this modified method, reload your page and see what do you get under /var/log/system.log, you will get a lot of results as it will log data for every translated content but once you get it look for the content that is not working and it should help you debug the problem.

Also disable both compilation and cache before reloading your page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.