Version 1
[yaffs-website] / web / core / modules / locale / src / Gettext.php
1 <?php
2
3 namespace Drupal\locale;
4
5 use Drupal\Component\Gettext\PoStreamReader;
6
7 /**
8  * Static class providing Drupal specific Gettext functionality.
9  *
10  * The operations are related to pumping data from a source to a destination,
11  * for example:
12  * - Remote files http://*.po to memory
13  * - File public://*.po to database
14  */
15 class Gettext {
16
17   /**
18    * Reads the given PO files into the database.
19    *
20    * @param object $file
21    *   File object with an URI property pointing at the file's path.
22    *   - "langcode": The language the strings will be added to.
23    *   - "uri": File URI.
24    * @param array $options
25    *   An array with options that can have the following elements:
26    *   - 'overwrite_options': Overwrite options array as defined in
27    *     Drupal\locale\PoDatabaseWriter. Optional, defaults to an empty array.
28    *   - 'customized': Flag indicating whether the strings imported from $file
29    *     are customized translations or come from a community source. Use
30    *     LOCALE_CUSTOMIZED or LOCALE_NOT_CUSTOMIZED. Optional, defaults to
31    *     LOCALE_NOT_CUSTOMIZED.
32    *   - 'seek': Specifies from which position in the file should the reader
33    *     start reading the next items. Optional, defaults to 0.
34    *   - 'items': Specifies the number of items to read. Optional, defaults to
35    *     -1, which means that all the items from the stream will be read.
36    *
37    * @return array
38    *   Report array as defined in Drupal\locale\PoDatabaseWriter.
39    *
40    * @see \Drupal\locale\PoDatabaseWriter
41    */
42   public static function fileToDatabase($file, $options) {
43     // Add the default values to the options array.
44     $options += [
45       'overwrite_options' => [],
46       'customized' => LOCALE_NOT_CUSTOMIZED,
47       'items' => -1,
48       'seek' => 0,
49     ];
50     // Instantiate and initialize the stream reader for this file.
51     $reader = new PoStreamReader();
52     $reader->setLangcode($file->langcode);
53     $reader->setURI($file->uri);
54
55     try {
56       $reader->open();
57     }
58     catch (\Exception $exception) {
59       throw $exception;
60     }
61
62     $header = $reader->getHeader();
63     if (!$header) {
64       throw new \Exception('Missing or malformed header.');
65     }
66
67     // Initialize the database writer.
68     $writer = new PoDatabaseWriter();
69     $writer->setLangcode($file->langcode);
70     $writer_options = [
71       'overwrite_options' => $options['overwrite_options'],
72       'customized' => $options['customized'],
73     ];
74     $writer->setOptions($writer_options);
75     $writer->setHeader($header);
76
77     // Attempt to pipe all items from the file to the database.
78     try {
79       if ($options['seek']) {
80         $reader->setSeek($options['seek']);
81       }
82       $writer->writeItems($reader, $options['items']);
83     }
84     catch (\Exception $exception) {
85       throw $exception;
86     }
87
88     // Report back with an array of status information.
89     $report = $writer->getReport();
90
91     // Add the seek position to the report. This is useful for the batch
92     // operation.
93     $report['seek'] = $reader->getSeek();
94     return $report;
95   }
96
97 }