Translating Block Contents

The Translation Dashboard (available when you activate the Translation Management module) handles block translation automatically.

Although internally nodes and blocks are constructed differently, the module makes their translation appear the same.

Blocks will appear in the Translation Dashboard just like all other content types. Behind the scenes, Translation Management will fetch the strings created by the Drupal blocks and return the translated contents as string translations.

Drupal's i18n module can translate block contents.

To display blocks in multiple languages, you can use one of these approaches:

  1. Creating a different block for each language.
  2. Using string localization

When you create a block, you can choose to assign it to a specific language or to all languages and use string translation.

Method 1 - different blocks per language

Select a unique language for the block and indicate which nodes to display it for. You should select only nodes that are written in the same language as the block.

Then, create similar blocks in other languages and enable them for the nodes in those languages.

This process will keep complete isolation between blocks in different languages. They can be translations of each other, but can also contain completely independent contents. Drupal and i18n don't offer any facilities for synchronizing between block contents in different languages.

Method 2 - string localization

Drupal can translate strings from English to other languages (currently, there is no solution for translating from any language other than English). You can tell Drupal to handle the title and contents of blocks as strings and look for translations using Drupal's string translation mechanism.

When a block is being displayed in a different language, Drupal will look up the title and body of the block to see if there is a string translation of the title and body. If they are available the translation will be displayed otherwise it will display the original text.

To use this method set the block language to "All languages (Translatable)" while creating or editing a block.

block translatable

NOTE: An important point when using this method is that the default language should be English. Drupal localizes these strings with the t() function, meant to be called whenever a module outputs an English text string.

Here is an example. We'll create a new block and translate its strings.

Create the block and set to 'All languages (Translatable)'

create a block

Choose where the block is displayed

We'll display this block in the right sidebar. The display position doesn't really matter and the block doesn't have to be displayed in order to be translated.

block in right sidebar

Which now shows as:

block on home page

Switching to Spanish shows the block visible but with no translation:

block not translated.

Manually entering translation for the block contents

The strings for the title and the body are translated using the translate interface.

Go to /admin/build/translate/search and look for the strings used in the block.

block search

Click on "Edit" and enter the translation.

Block enter translation

Repeat for the body of the block searching for some text in the body.

This should now give you a fully translated block on the Spanish home page.

block translated to Spanish


Method 2 note

For it to work you have to go into your theme folder, find the block.tpl.php file and add the t() function to the blok title.


Block Titles

Block titles should show up in admin/build/translate on their own. There should not be any need to add t() strings to the template.

Be careful with t() and variables, as I am pretty sure what you have suggested is dangerous. Read the Drupal API documentation to learn more.

Because t() is designed for handling code-based strings, in almost all cases, the actual string and not a variable must be passed through t().

Extraction of translations is done based on the strings contained in t() calls. If a variable is passed through t(), the content of the variable cannot be extracted from the file for translation.


$message = 'An error occurred.';
drupal_set_message(t($message), 'error');
$output .= t($message);

$message = t('An error occurred.');
drupal_set_message($message, 'error');
$output .= $message;

Did not find this in my case

Using version 6.x-1.23 of the module I did not find the above to be the case (overriding the block.tpl.php).