Translating Panel Content Titles Using Tokens

We recently encountered a specific situation with a Drupal site that had to be retro-fitted for translation.

The titles of content items within a panel page are not translatable.

 

 

There is an issue in the queue for panels, but it has been open for two years now, and it is unlikely to be resolved any time soon. There are also many somewhat hackish work-arounds - i.e. using blocks with the title inserted as content instead - but we found a cleaner way to do this using tokens, which panels do provide support for.

If you've ever coded a custom block, the methodology for creating tokens will look familiar. In a custom module, you first implement hook_token_info(), which lets Drupal know about the list of tokens that your module will provide, and then implement hook_tokens(), which actually outputs the content of the code.

 

function test_token_info() {
    $type = array(
        'name' => t('Test Tokens'),
        'description' => t('Tokens for i18n experiment.')
    );

$test['test_panel_translation_title'] = array(
    'name' => t("Test title translation"),
    'description' => t('Translatable version of panel content title'),
);

return array(
        'types' => array('test' => $type),
        'tokens' => array('test' => $test),
    );
}


The code, above, lets Drupal know what this module produces in the way of tokens. The $type array contains information that will be displayed about the module itself, and the $test array contains information about the token itself. You can add many tokens into the $test array, of course (and name the array appropriately for your module as well).

 

/**
      * Implements hook_tokens().
      */

    function test_tokens($type, $tokens, array $data = array(), array $options = array()){
        return array(
            'test_panel_translation_title' => t('My title is now translatable'),
        );
    }

 

The code, above, actually produces the contents of the token. Note that we have wrapped our text in t(), so it is translatable.

You'll obviously need to enable your module (in addition to the token moduleand clear your cache.

Now let's see how we can use the new token inside of panels.

As you can see, we need to first add something into your panel that lets it know that you are going to be using tokens. After you add in tokens, you'll see your new tokens listed.

Now we can actually change our content title to a token.

The token appears in the list of available substitutions, so you don't need to remember it.

You'll need to select the "Use context keywords" option so that panels knows that it needs to make substitutions on tokens in this content item.

Aside: We can also use tokens inside of content itself.

If you are exporting your panel to code (i.e. with features), the token will appear in the actual code as well.

 

As you can see, using tokens is a simple method, producing clean, easy to document code, for translating items in Drupal that are otherwise hard to make translatable.

Written by

Jeremy Lichtman

Jeremy Lichtman

Sign up for our newsletter