23

I need to create multilingual static block. So far I have created static block with id 'delivery_returns'.

I'm calling it in catalog\product\view.phtml like this:

$deliveryBlock = Mage::getModel('cms/block')->load('delivery_returns');
echo $deliveryBlock->getTitle();
echo $deliveryBlock->getContent();

I understand that to translate this block:

  1. I should just create another one.
  2. Choose my desired language from store_view field
  3. and keep the static block identifier same as original.

This method works with 'footer_links' and also with another static block I made, called 'header_links', but apparently it is not working with 'delivery_returns' block. Changing store language does not load corresponding translated 'delivery_returns' block

What am I missing? Is there a better way to achieve this?

25
  1. Create a static block for each language, all with the same identifier.
  2. Render the block with the cms/block block. It will automatically add the store ID to load the correct version of the block.

Here's an easy way to load and render the block directly in the template file:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('delivery_returns')->toHtml() ?>

Alternatively, declare the block in a layout file and render it with <?php echo $this->getChildHtml('delivery_returns') ?>:

<block type="cms/block" name="product.delivery_returns" as="delivery_returns">
    <action method="setBlockId"><block_id>delivery_returns</block_id></action>
</block>
  • 1
    I did not know this. Awesome. – philwinkle Feb 10 '13 at 19:37
  • Mind this gives some issues regarding block caching. Not sure how to fix yet besides turning off the magento cache for this specific block... :( – Joris Kroos Aug 26 '15 at 15:03
  • More info regarding magento chache issues with blocks with multiple store views see community.magento.com/t5/Version-Upgrades/… – Joris Kroos Aug 26 '15 at 15:11
8

The answer below talks about setting the store id prior to loading the model, but, as @benmarks noted in the comment below, this is unnecessary, as that happens in the block's _toHtml().

As with many other mage models, try setting the desired store id before loading the model:

$deliveryBlock = Mage::getModel('cms/block')
                     ->setStoreId(Mage::app()->getStore()->getId())
                     ->load('delivery_returns');

echo $deliveryBlock->getTitle();

/**
 * You shouldn't print the content directly (although I'm assuming it's for debugging purposes only).
 * Use the code below, so as the possible content directives (the "{{ }}" thingies) would be interpreted.
 * Check out Mage_Cms_Block_Block::_toHtml().
 */
echo Mage::helper('cms')->getBlockTemplateProcessor()
                        ->filter($deliveryBlock->getContent());
  • 2
    Not necessary - the cms/block does this during rendering; see Mage_Cms_Block_Block::_toHtml() (link) – benmarks Feb 10 '13 at 13:43
  • You're right. Odd, I haven't noticed. I even point the OP to that very method you're talking about. – nevvermind Feb 11 '13 at 18:37
  • Actually, when getting the block, Mage sets the store id. Whereas I directly instantiated the model, and that didn't have the store id set. See Mage_Cms_Block_Block::_toHtml(). – nevvermind Aug 27 '13 at 13:41
  • I'm not sure what you mean - I don't see anything in Mage_Core_Model_Block which sets the store id. – benmarks Aug 27 '13 at 22:39
  • That's not the block I'm talking about, but Mage_Cms_Block_Block. – nevvermind Aug 28 '13 at 9:28
4

I've accomplished this in one of a few ways:

  1. Just use a different static block name, copy and translate, and refer to it in your theme separately.
  2. You can use `{{translate text="text to translate"}} in any cms, static block page by implementing the following workaround:

    • copy app/code/core/Mage/Core/Model/Email/Template/Filter.php to app/code/local/Mage/Core/Model/Email/Template/Filter.php and modify the following:

--

public function translateDirective($construction)
{
    $params = $this->_getIncludeParameters($construction[2]);
    $text = $params['text'];
    return Mage::helper('page')->__($text);
} 

More information/source:

http://jagdeepbanga.com/blog/magento_how_add_translation_ability_into_cms_page_or_static_block.html

  • Yes, I saw those solutions. But both are workarounds. With method one I then have to duplicate product\view.phtml for every storeview. Method 2 needs .csv file edit. So both are NOT suitable for non-developer client to work with. Isn't there proper way to do this? With my method, client can create and change static block content and it works with header_links and footer_links. I do not understand why not work with delivery_returns :S – ruuter Feb 9 '13 at 20:58
  • I wish that there were an 'easier' way - the only other way to handle it would be to pull in the content via ajax and load it from an external source. – philwinkle Feb 9 '13 at 21:11
  • FWIW - I have had great luck with solution 2; I use both on a regular basis. – philwinkle Feb 9 '13 at 21:12
  • I thought about ajax, but this makes it even more difficult for client to change content. I guess I'll choose your method 1. and create different view.phtml files for every language. If no soon one comes up with better solution, I'll mark your answer as accepted. Tnx anyway! – ruuter Feb 9 '13 at 21:51
1

You can download a module i created based on this answer from here: https://github.com/miguelbalparda/MB_Translate/ It makes available the inline translator of Magento in CMS/Block pages.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Single link answers are not the best. Please explain what you extension does. I know what it does and I recommend it but make it a little clear for everyone. – Marius Feb 12 '14 at 14:52
  • Sure, it makes available the inline translator of Magento in CMS/Block pages. – mbalparda Feb 12 '14 at 21:14
  • Add it yo your answer. – Marius Feb 12 '14 at 21:15

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.