25

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?

ruuter
  • 1,079
  • 3
  • 19
  • 30

4 Answers4

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 http://community.magento.com/t5/Version-Upgrades/Static-blocks-are-mixed-between-store-views-after-upgrading-to-v/m-p/10399#M655 – 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());
nevvermind
  • 1,726
  • 1
  • 16
  • 19
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

philwinkle
  • 35,751
  • 5
  • 91
  • 145
  • 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 09 '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 09 '13 at 21:11
  • FWIW - I have had great luck with solution 2; I use both on a regular basis. – philwinkle Feb 09 '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 09 '13 at 21:51
2

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.

mbalparda
  • 7,363
  • 3
  • 23
  • 46
  • 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
  • I forked this repository and add translation in Category Description. Also, now you can install this module through composer, the files mapping are configured. Here you can check the module https://github.com/popovserhii/MB_Translate – Serhii Popov Apr 04 '20 at 17:49