Translate Drupal Strings Programatically
Sep 22, 2015 09:04 · 156 words · 1 minute read
One of the many issues one has to face when trying to do some proper deployment with Drupal (only up to version 7, that is) is custom localized strings. Fear no more! Instead of translating strings in the admin interface (/admin/config/regional/translate/translate) we can make use of the great Update Script Processor, thus put all our custom translations into code and happily version and deploy them. Yay!
- Install Update Script Processor
- A update Script that contains your localizations, e.G.
$this->setLanguageWording('de', 'Telephone number', 'Telefonnummer');
- run
drush updatescripts
Obviously, you should put that under version control and make sure your deployment process runs drush updatescripts
after deploying to another environment.
Here’s a full example that translate the original strings Telephone number
and Fax number
to their German counterparts:
<?php
/**
* @file 0060-update-localization-string-test.php
* @author Daniel Wentsch
*/
$this->setAuthor('Daniel Wentsch');
$this->setDescription('Update localization string (test)');
// Put your updatescript code here!
$this->setLanguageWording('de', 'Telephone number', 'Telefonnummer');
$this->setLanguageWording('de', 'Fax number', 'Faxnummer');
$this->finished();