Skip to content

Instantly share code, notes, and snippets.

@simme
Created April 12, 2011 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
<?php
// 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(
$it,
'/^(?:.*\/)?(?:modules-topbar|themes-xskane)\.([a-z]{2,})\.po$/', // Tweak to match your modules/themes
RecursiveRegexIterator::GET_MATCH
);
require('includes/locale.inc');
$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);
}
}
}
@voxpelli
Copy link

@simme
Copy link
Author

simme commented Apr 18, 2011

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

@voxpelli
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