You are here

function locale_translate_file_attach_properties in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/locale/locale.bulk.inc \locale_translate_file_attach_properties()

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 locale_translate_file_attach_properties()
ImportForm::submitForm in core/modules/locale/src/Form/ImportForm.php
Form submission handler.
locale_translate_get_interface_translation_files in core/modules/locale/locale.bulk.inc
Get interface translation files present in the translations directory.

File

core/modules/locale/locale.bulk.inc, line 458
Mass import-export and batch import functionality for Gettext .po files.

Code

function locale_translate_file_attach_properties($file, array $options = []) {

  // If $file is a file entity, convert it to a stdClass.
  if ($file instanceof FileInterface) {
    $file = (object) [
      '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 empty (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];
  }
  else {
    $file->langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  }
  return $file;
}