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