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:
- Internationalization – i18n (and the appropriate child modules like String Translation)
- Internationalization Views – i18nviews
- Language icons – languageicons (optional)
- Consistent Language Interface – languageinterface
- Translation template extractor – potx (the key module here)
- Locale (built-in Drupal module)
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!