Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active August 29, 2015 14:04
Show Gist options
  • Save cesarmiquel/ff81587062a6a994d162 to your computer and use it in GitHub Desktop.
Save cesarmiquel/ff81587062a6a994d162 to your computer and use it in GitHub Desktop.
Import translations into Drupal from a .po file
<?php
//
// This code uses the _locale_import_po() function call from includes/locale.inc. This
// is a private function so use with care. Here's the function:
// https://api.drupal.org/api/drupal/includes%21locale.inc/function/_locale_import_po/7
//
// example usage:
$translation_filepath = drupal_get_path('module', '<module>') . '/translations-es.po';
_import_translation($translation_filepath, 'es');
function _import_translation($path, $lang) {
if (file_exists($path)) {
// create a temp file in public:// with the translations
$data = file_get_contents($path);
$filename = "public://translations-$lang.po";
$file = file_save_data($data, $filename, FILE_EXISTS_REPLACE);
if ($file) {
// import into Drupal
_locale_import_po($file, $lang, LOCALE_IMPORT_OVERWRITE, 'default');
// remove temp file
file_delete($file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment