blog-banner

Translation of Custom Modules and Themes in Drupal 7

  • Drupal 7
  • Drupal Planet
  • multilingual
  • Translation

Drupal Translation Files

 

As we discussed a lot about translation in the previous posts Translation in Drupal 7: How it works? and TRANSLATION in Drupal 7: How to work with, let us discuss a few points about how to do a custom translation to our module and pot(.po) file handling for string translations. Before getting into the custom translation let us know a few things about pot file handling. The pot (or) potx file which will be with extension .po. This pot(.po) file can manually be created or this can be generated with the help of the drupal module called Translation template extractor.

Let us see, how to generate the pot file for translation. First, we need to enable the Translation Template Extractor module. Once the module is enabled, you will be seeing the “Extract” tab in the TRANSLATE INTERFACE section under REGIONAL AND LANGUAGE.

 

Now create a custom module and build all your required functionalities in the module and before enabling it, now go to the Extract Tab in Translation Interface, there you can find your module listed under Directory "sites/all/modules".

 

Now, select your module, then choose your best options for the pot file, then click extract. Once you click extract, you will be given a file named in a specified format like this “module_name.language_name.po”. For Instance, custom.es.po, custom.fr.po, custom.ta.po, etc. After getting this file, create a subdirectory in your module named “translations”, then move the extracted .po files to the directory.

 

When you open the pot files, you will be seeing the contents like this:

 

This shows the msgid which is the string to be translated and msgstr is the string for translation, once you extract the file with the include translation option enabled, the possible translatable items with their respective msgid and msgstr will be given, whereas the unmatched items will be left null as like the first one in the above image. For this kind of left-out item, we need to add our translatable strings manually.

After this, when you attempt to enable the custom module you developed you can see the translate pot files get imported automatically.

Now we are done with the custom translation for the module. But you may think that if you add/modify some translation strings, still you can continue with the translation either way

  • By using this command

    • drush php-eval “locale_system_update(array('yourmodule_name'));drush_backend_batch_process();”

  • Also, we can import the files, directly into the Translate Interface.

The Drush command calls the locale_system_update(...) which in turn imports the files and updates the translation string for the module.

 

Get awesome tech content in your inbox