How to: Translate template file strings in Drupal 7

How to: Translate template file strings in Drupal 7 5 min read

Drupal translations can be a pain to a novice user trying to figure out the different configurations of modules and what everything does. String translations are a powerful mechanism that will afford the creator of the site the ability to provide individual translations for various components. In this tutorial we will discuss one aspect: How do you translate strings that are stored in your template files?


When coding your template files, ensure that you are using Drupal’s built-in translation function t(). This function is a simple way to ensure that your templates are localized appropriately. An example would be echo t(‘Register’);. Check out the link for the full function listing.


Before we continue, you should have the following modules installed:


Ok, so we have all of these installed now and the appropriate items are active. The next step is to go into your admin/config/regional/translate/extract section or Configuration > Regional and Language > Translate interface > Extract. You’ll see something like the following:





Simply select the radio button next to the appropriate theme and click the “Extract” button at the bottom of the page. This will prompt you to download a .pot file. Save this file to your hard drive for the next step.


Now open this file in Wordpad (or the equivalent for MAC users). You’ll see something like this:


#: templates/page.tpl.php:349 templates/user-register.tpl.php:2
msgid "Register"
msgstr ""


The msgid refers to the string you’ve encapsulated in your t() function.


The msgstr portion refers to the translated string.


So for spanish you’d have:


#: templates/page.tpl.php:349 templates/user-register.tpl.php:2
msgid "Register"
msgstr "inscribirse"

Modify these translations as needed and then save the file. It is important to note that Drupal will only accept strings that have been HTML encoded! These strings may not contain any HTML or it will ignore the rule.


Once you’ve saved it, change the extension to .po. In Drupal 7, it will only allow .po files to be imported.


Next, we import these translations back into the interface by going to admin/config/regional/translate/import. The settings here are pretty self explanatory. When you’ve selected the correct file and configured your settings, click the “Import” button at the bottom on the page. And there you have it.


Now when you go to your Translate interface (admin/config/regional/translate/translate) you’ll see the newly imported strings available here.


Update: Translations will be built into the Drupal 8 core. This will be a LOT easier once it’s released!


  • Cosette Malinowski

    Hi, Ryan! If you’re interested in a good online tool to translate Drupal strings collaboratively, I warmly recommend

  • Appreciate the suggestion Cosette, I’ll have to give it a look!

  • Wilfred Arambhan

    Nice post. I have a doubt though, how can I make the numeric digits to
    be displayed only in english format irrespective of the language
    selected. My website translates the numbers as well based on the
    language selected. Any suggestion, what am I doing wrong?

  • Hey Wilfred, great question. Have you tried taking your string and doing a str_replace on your characters to convert to 1-9? Could handle in a switch statement then pass to the t() function.

  • Wilfred Arambhan

    Hey Ryan, thanks for the reply. I guess that would end up with the same result. I even hard coded the value, still its getting translated. Dont know what im missing.

  • Baron Teapot

    You can use placeholders to make sure a part of the string remains untranslated.

    Try the following PHP:

    t(‘Hello, my age is !age’, array(‘!age’ => 8));

    This allows you to insert a string or number to replace “!age”. There are multiple prefixes, aswell: !age, %age, @age – all are different, and can be read about on the website under the documentation for the format_string() function.