You are here

function l10n_update_file_attach_properties in Localization update 7.2

Generates file properties from filename and options.

An attempt is made to determine the translation language, project name and project version from the file name. Supported file name patterns are: {project}-{version}.{langcode}.po, {prefix}.{langcode}.po or {langcode}.po. Alternatively the translation language can be set using the $options.

Parameters

object $file: A file object of the gettext file to be imported.

array $options: An array with options:

  • 'langcode': The language code. Overrides the file language.

Return value

object Modified file object.

2 calls to l10n_update_file_attach_properties()
l10n_update_get_interface_translation_files in ./l10n_update.bulk.inc
Get interface translation files present in the translations directory.
l10n_update_import_form_submit in ./l10n_update.bulk.inc
Form submission handler for l10n_update_import_form().

File

./l10n_update.bulk.inc, line 680
Mass import-export and batch import functionality for Gettext .po files.

Code

function l10n_update_file_attach_properties($file, $options = array()) {

  // If $file is a file entity, convert it to a stdClass.
  if ($file instanceof FileInterface) {
    $file = (object) array(
      'filename' => $file
        ->getFilename(),
      'uri' => $file
        ->getFileUri(),
    );
  }

  // Extract project, version and language code from the file name. Supported:
  // {project}-{version}.{langcode}.po, {prefix}.{langcode}.po or {langcode}.po.
  preg_match('!
    (                       # project OR project and version OR emtpy (group 1)
      ([a-z_]+)             # project name (group 2)
      \\.                    # .
      |                     # OR
      ([a-z_]+)             # project name (group 3)
      \\-                    # -
      ([0-9a-z\\.\\-\\+]+)     # version (group 4)
      \\.                    # .
      |                     # OR
    )                       # (empty)
    ([^\\./]+)               # language code (group 5)
    \\.                      # .
    po                      # po extension
    $!x', $file->filename, $matches);
  if (isset($matches[5])) {
    $file->project = $matches[2] . $matches[3];
    $file->version = $matches[4];
    $file->langcode = isset($options['langcode']) ? $options['langcode'] : $matches[5];
  }
  return $file;
}