locale_translate_file_attach_properties

locale_translate_file_attach_properties($file, array $options = array())

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.

File

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

Code

function locale_translate_file_attach_properties($file, array $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 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;
}
doc_Drupal
2016-10-29 09:24:21
Comments
Leave a Comment

Please login to continue.