Translation of custom modules and themes in Drupal 7
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 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. At 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 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 items, 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 gets 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 in 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 import the files and updates the translation string for the module.