1

I have imported a .pot file using the _locale_import_read_po function, like this:

$po_files = file_scan_directory('/', '/.*\.pot$/');
if (count($po_files)) {
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $po_file = reset($po_files);
  $translations = _locale_import_read_po('db-store', $po_file);
}

Now, what I want is to convert all the imported strings into a PHP array.

Is there any way to do that, and how?

1

Passing 'db-store' as first argument of _locale_import_read_po() will cause the content of the translation file to be imported in the database. To import it in memory, it's necessary to use the following code.

(As usual, replace mymodule with the machine name of the module that contains this function.)

function mymodule_import_translations_in_memory($directory, $pattern) {
  $po_files = file_scan_directory($directory, $pattern);
  if (count($po_files)) {
    require_once DRUPAL_ROOT . '/includes/locale.inc';

    // Allow execution to continue even if the request gets canceled.
    @ignore_user_abort(TRUE);

    // Try to allocate enough time to import all the file.
    drupal_set_time_limit(240);

    foreach ($po_files as $po_file) {
      _locale_import_read_po('mem-store', $po_file);
    }

    @ignore_user_abort(FALSE);
  }

  return _locale_import_one_string('mem-report');
}

Passing 'mem-report' to _local_import_one_string() will cause the function to return all the imported strings, while passing 'mem-store' to _locale_import_read_po() will import the translation in a static variable.

The code I shown works, but if another module called _locale_import_read_po() passing 'mem-store' as first argument, mymodule_import_translations_in_memory() would return the content of the files imported from the other module and the content of the files mymodule_import_translations_in_memory() imported. A more generic code would be the following one.

function mymodule_import_translations_in_memory($directory, $pattern) {
  $po_files = file_scan_directory($directory, $pattern);
  if (count($po_files)) {
    require_once DRUPAL_ROOT . '/includes/locale.inc';

    // Allow execution to continue even if the request gets canceled.
    @ignore_user_abort(TRUE);

    // Try to allocate enough time to import all the file.
    drupal_set_time_limit(240);

    $strings =& drupal_static('_locale_import_one_string:strings', array());

    // Store the old string list to restore it at the end.
    $old_strings = $strings;

    // Reset the string array to an empty array.
    $strings = array();

    foreach ($po_files as $po_file) {
      _locale_import_read_po('mem-store', $po_file);
    }

    @ignore_user_abort(FALSE);

    // Store the value to return.
    $string_list = $strings;

    // Restore the value set before this function was called.
    $strings = $old_strings;
  }

  return $string_list;
}

The code is directly accessing a static variable set by another function, but since there isn't any function to reset its value, that is the only possibility.

In the case of importing a big translation file, keep in mind that loading it all in memory is probably going to cause memory problems. Before doing it, PHP should be set to use more memory than Drupal would normally use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.