3 namespace Drupal\Core\StringTranslation\Translator;
5 use Drupal\Component\Gettext\PoStreamReader;
6 use Drupal\Component\Gettext\PoMemoryWriter;
9 * File based string translation.
11 * Translates a string when some systems are not available.
13 * Used during the install process, when database, theme, and localization
14 * system is possibly not yet available.
16 class FileTranslation extends StaticTranslation {
19 * Directory to find translation files in the file system.
26 * Constructs a StaticTranslation object.
28 * @param string $directory
29 * The directory to retrieve file translations from.
31 public function __construct($directory) {
32 parent::__construct();
33 $this->directory = $directory;
39 protected function getLanguage($langcode) {
40 // If the given langcode was selected, there should be at least one .po
41 // file with its name in the pattern drupal-$version.$langcode.po.
42 // This might or might not be the entire filename. It is also possible
43 // that multiple files end with the same suffix, even if unlikely.
44 $files = $this->findTranslationFiles($langcode);
47 return $this->filesToArray($langcode, $files);
55 * Finds installer translations either for a specific or all languages.
57 * Filenames must match the pattern:
58 * - 'drupal-[version].[langcode].po (if langcode is provided)
59 * - 'drupal-[version].*.po (if no langcode is provided)
61 * @param string $langcode
62 * (optional) The language code corresponding to the language for which we
63 * want to find translation files. If omitted, information on all available
64 * files will be returned.
67 * An associative array of file information objects keyed by file URIs as
68 * returned by file_scan_directory().
70 * @see file_scan_directory()
72 public function findTranslationFiles($langcode = NULL) {
73 $files = file_scan_directory($this->directory, $this->getTranslationFilesPattern($langcode), ['recurse' => FALSE]);
78 * Provides translation file name pattern.
80 * @param string $langcode
81 * (optional) The language code corresponding to the language for which we
82 * want to find translation files.
85 * String file pattern.
87 protected function getTranslationFilesPattern($langcode = NULL) {
88 // The file name matches: drupal-[release version].[language code].po
89 // When provided the $langcode is use as language code. If not provided all
90 // language codes will match.
91 return '!drupal-[0-9a-z\.-]+\.' . (!empty($langcode) ? preg_quote($langcode, '!') : '[^\.]+') . '\.po$!';
95 * Reads the given Gettext PO files into a data structure.
97 * @param string $langcode
98 * Language code string.
100 * List of file objects with URI properties pointing to read.
103 * Structured array as produced by a PoMemoryWriter.
105 * @see \Drupal\Component\Gettext\PoMemoryWriter
107 public static function filesToArray($langcode, array $files) {
108 $writer = new PoMemoryWriter();
109 $writer->setLangcode($langcode);
110 foreach ($files as $file) {
111 $reader = new PoStreamReader();
112 $reader->setURI($file->uri);
113 $reader->setLangcode($langcode);
115 $writer->writeItems($reader, -1);
117 return $writer->getData();