function LocaleFile::createFromPath

Creates a LocaleFile from the filepath.

Parameters

string $filename: The filename of a file to import.

string $filepath: The filepath of a file to import.

string|null $langcodeOverride: The language code. Overrides the file language.

Return value

self A LocaleFile.

3 calls to LocaleFile::createFromPath()
ImportForm::submitForm in core/modules/locale/src/Form/ImportForm.php
Form submission handler.
LocaleFileManager::getInterfaceTranslationFiles in core/modules/locale/src/File/LocaleFileManager.php
Get interface translation files present in the translations directory.
locale_translate_file_attach_properties in core/modules/locale/locale.bulk.inc
Generates file properties from filename and options.

File

core/modules/locale/src/File/LocaleFile.php, line 76

Class

LocaleFile
Defines the locale file value object.

Namespace

Drupal\locale\File

Code

public static function createFromPath(string $filename, string $filepath, ?string $langcodeOverride = NULL) : self {
  $project = NULL;
  $version = NULL;
  $hash = hash_file(LocaleSource::LOCAL_FILE_HASH_ALGO, $filepath);
  // 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 $langcodeOverride.
  // 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', $filename, $matches);
  if (isset($matches[5])) {
    $project = $matches[2] . $matches[3];
    $version = $matches[4];
    $langcode = $langcodeOverride ?? $matches[5];
  }
  else {
    $langcode = $langcodeOverride ?? LanguageInterface::LANGCODE_NOT_SPECIFIED;
  }
  return new self($filename, $filepath, $hash, filemtime($filepath), $langcode, $project, $version);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.