Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2011 12:21
Show Gist options
  • Save simme/915409 to your computer and use it in GitHub Desktop.
Save simme/915409 to your computer and use it in GitHub Desktop.
Update translations on a Drupal site
// Update translations
// This might need some 'splainin':
// RecursiveDirectoryIterator together with RecursiveIteratorIterator
// loops through all files in the give root directory.
// RegexIterator filters out the files we want (modules-topbar and
// themes-xskane) translations in this case. Matches the languages code
// and filename. Then we loop through it and win.
$di = new RecursiveDirectoryIterator('./sites/all/translations/');
$it = new RecursiveIteratorIterator($di);
$po = new RegexIterator(
'/^(?:.*\/)?(?:modules-topbar|themes-xskane)\.([a-z]{2,})\.po$/', // Tweak to match your modules/themes
$languages = language_list();
foreach ($po as $translation) {
$file = (object) array('filepath' => $translation[0]);
$langcode = $translation[1];
// Only import for activated languages
if (array_key_exists($langcode, $languages)) {
if (!_locale_import_po($file, $langcode, LOCALE_IMPORT_OVERWRITE)) {
drupal_set_message('Failed to import ' . $file->filepath);
Copy link

Copy link

simme commented Apr 18, 2011

I have not. :P I just made the old a bit prettier.
And I got to use RegexIterator which it turns out, is awesome! :D

Copy link

Seems like some crazy stuff there! I imagine a certain import of a norwegian API could have made good use of that :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment