Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / StringTranslation / Translator / FileTranslation.php
1 <?php
2
3 namespace Drupal\Core\StringTranslation\Translator;
4
5 use Drupal\Component\Gettext\PoStreamReader;
6 use Drupal\Component\Gettext\PoMemoryWriter;
7
8 /**
9  * File based string translation.
10  *
11  * Translates a string when some systems are not available.
12  *
13  * Used during the install process, when database, theme, and localization
14  * system is possibly not yet available.
15  */
16 class FileTranslation extends StaticTranslation {
17
18   /**
19    * Directory to find translation files in the file system.
20    *
21    * @var string
22    */
23   protected $directory;
24
25   /**
26    * Constructs a StaticTranslation object.
27    *
28    * @param string $directory
29    *   The directory to retrieve file translations from.
30    */
31   public function __construct($directory) {
32     parent::__construct();
33     $this->directory = $directory;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
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);
45
46     if (!empty($files)) {
47       return $this->filesToArray($langcode, $files);
48     }
49     else {
50       return [];
51     }
52   }
53
54   /**
55    * Finds installer translations either for a specific or all languages.
56    *
57    * Filenames must match the pattern:
58    *  - 'drupal-[version].[langcode].po (if langcode is provided)
59    *  - 'drupal-[version].*.po (if no langcode is provided)
60    *
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.
65    *
66    * @return array
67    *   An associative array of file information objects keyed by file URIs as
68    *   returned by file_scan_directory().
69    *
70    * @see file_scan_directory()
71    */
72   public function findTranslationFiles($langcode = NULL) {
73     $files = file_scan_directory($this->directory, $this->getTranslationFilesPattern($langcode), ['recurse' => FALSE]);
74     return $files;
75   }
76
77   /**
78    * Provides translation file name pattern.
79    *
80    * @param string $langcode
81    *   (optional) The language code corresponding to the language for which we
82    *   want to find translation files.
83    *
84    * @return string
85    *   String file pattern.
86    */
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$!';
92   }
93
94   /**
95    * Reads the given Gettext PO files into a data structure.
96    *
97    * @param string $langcode
98    *   Language code string.
99    * @param array $files
100    *   List of file objects with URI properties pointing to read.
101    *
102    * @return array
103    *   Structured array as produced by a PoMemoryWriter.
104    *
105    * @see \Drupal\Component\Gettext\PoMemoryWriter
106    */
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);
114       $reader->open();
115       $writer->writeItems($reader, -1);
116     }
117     return $writer->getData();
118   }
119
120 }