class FileTranslation

Same name in other branches
  1. 9 core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php \Drupal\Core\StringTranslation\Translator\FileTranslation
  2. 8.9.x core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php \Drupal\Core\StringTranslation\Translator\FileTranslation
  3. 11.x core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php \Drupal\Core\StringTranslation\Translator\FileTranslation

File based string translation.

Translates a string when some systems are not available.

Used during the install process, when database, theme, and localization system is possibly not yet available.

Hierarchy

Expanded class hierarchy of FileTranslation

3 files declare their use of FileTranslation
install.core.inc in core/includes/install.core.inc
API functions for installing Drupal.
InstallerLanguageTest.php in core/tests/Drupal/KernelTests/Core/Installer/InstallerLanguageTest.php
InstallTranslationFilePatternTest.php in core/modules/system/tests/src/Kernel/Installer/InstallTranslationFilePatternTest.php

File

core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php, line 18

Namespace

Drupal\Core\StringTranslation\Translator
View source
class FileTranslation extends StaticTranslation {
    
    /**
     * Directory to find translation files in the file system.
     *
     * @var string
     */
    protected $directory;
    
    /**
     * The file system.
     *
     * @var \Drupal\Core\File\FileSystemInterface
     */
    protected $fileSystem;
    
    /**
     * Constructs a StaticTranslation object.
     *
     * @param string $directory
     *   The directory to retrieve file translations from.
     * @param \Drupal\Core\File\FileSystemInterface $file_system
     *   The file system service.
     */
    public function __construct($directory, FileSystemInterface $file_system) {
        parent::__construct();
        $this->directory = $directory;
        $this->fileSystem = $file_system;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getLanguage($langcode) {
        // If the given langcode was selected, there should be at least one .po
        // file with its name in the pattern drupal-$version.$langcode.po.
        // This might or might not be the entire filename. It is also possible
        // that multiple files end with the same suffix, even if unlikely.
        $files = $this->findTranslationFiles($langcode);
        if (!empty($files)) {
            return $this->filesToArray($langcode, $files);
        }
        else {
            return [];
        }
    }
    
    /**
     * Finds installer translations either for a specific or all languages.
     *
     * Filenames must match the pattern:
     *  - 'drupal-[version].[langcode].po (if langcode is provided)
     *  - 'drupal-[version].*.po (if no langcode is provided)
     *
     * @param string $langcode
     *   (optional) The language code corresponding to the language for which we
     *   want to find translation files. If omitted, information on all available
     *   files will be returned.
     *
     * @return array
     *   An associative array of file information objects keyed by file URIs as
     *   returned by FileSystemInterface::scanDirectory().
     *
     * @see \Drupal\Core\File\FileSystemInterface::scanDirectory()
     */
    public function findTranslationFiles($langcode = NULL) {
        $files = [];
        if (is_dir($this->directory)) {
            $files = $this->fileSystem
                ->scanDirectory($this->directory, $this->getTranslationFilesPattern($langcode), [
                'recurse' => FALSE,
            ]);
        }
        return $files;
    }
    
    /**
     * Provides translation file name pattern.
     *
     * @param string $langcode
     *   (optional) The language code corresponding to the language for which we
     *   want to find translation files.
     *
     * @return string
     *   String file pattern.
     */
    protected function getTranslationFilesPattern($langcode = NULL) {
        // The file name matches: drupal-[release version].[language code].po
        // When provided the $langcode is use as language code. If not provided all
        // language codes will match.
        return '!drupal-[0-9]+\\.[0-9]+\\.([0-9]+|x)(-[a-z]+[0-9]*)?\\.' . (!empty($langcode) ? preg_quote($langcode, '!') : LanguageInterface::VALID_LANGCODE_REGEX) . '\\.po$!';
    }
    
    /**
     * Reads the given Gettext PO files into a data structure.
     *
     * @param string $langcode
     *   Language code string.
     * @param array $files
     *   List of file objects with URI properties pointing to read.
     *
     * @return array
     *   Structured array as produced by a PoMemoryWriter.
     *
     * @see \Drupal\Component\Gettext\PoMemoryWriter
     */
    public static function filesToArray($langcode, array $files) {
        $writer = new PoMemoryWriter();
        $writer->setLangcode($langcode);
        foreach ($files as $file) {
            $reader = new PoStreamReader();
            $reader->setURI($file->uri);
            $reader->setLangcode($langcode);
            $reader->open();
            $writer->writeItems($reader, -1);
        }
        return $writer->getData();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FileTranslation::$directory protected property Directory to find translation files in the file system.
FileTranslation::$fileSystem protected property The file system.
FileTranslation::filesToArray public static function Reads the given Gettext PO files into a data structure.
FileTranslation::findTranslationFiles public function Finds installer translations either for a specific or all languages.
FileTranslation::getLanguage protected function Retrieves translations for a given language. Overrides StaticTranslation::getLanguage
FileTranslation::getTranslationFilesPattern protected function Provides translation file name pattern.
FileTranslation::__construct public function Constructs a StaticTranslation object. Overrides StaticTranslation::__construct
StaticTranslation::$translations protected property String translations.
StaticTranslation::getStringTranslation public function Retrieves English string to given language. Overrides TranslatorInterface::getStringTranslation
StaticTranslation::reset public function Resets translation cache. Overrides TranslatorInterface::reset

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