3 namespace Drupal\locale;
5 use Drupal\Component\Gettext\PoStreamReader;
8 * Static class providing Drupal specific Gettext functionality.
10 * The operations are related to pumping data from a source to a destination,
12 * - Remote files http://*.po to memory
13 * - File public://*.po to database
18 * Reads the given PO files into the database.
21 * File object with an URI property pointing at the file's path.
22 * - "langcode": The language the strings will be added to.
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.
38 * Report array as defined in Drupal\locale\PoDatabaseWriter.
40 * @see \Drupal\locale\PoDatabaseWriter
42 public static function fileToDatabase($file, $options) {
43 // Add the default values to the options array.
45 'overwrite_options' => [],
46 'customized' => LOCALE_NOT_CUSTOMIZED,
50 // Instantiate and initialize the stream reader for this file.
51 $reader = new PoStreamReader();
52 $reader->setLangcode($file->langcode);
53 $reader->setURI($file->uri);
58 catch (\Exception $exception) {
62 $header = $reader->getHeader();
64 throw new \Exception('Missing or malformed header.');
67 // Initialize the database writer.
68 $writer = new PoDatabaseWriter();
69 $writer->setLangcode($file->langcode);
71 'overwrite_options' => $options['overwrite_options'],
72 'customized' => $options['customized'],
74 $writer->setOptions($writer_options);
75 $writer->setHeader($header);
77 // Attempt to pipe all items from the file to the database.
79 if ($options['seek']) {
80 $reader->setSeek($options['seek']);
82 $writer->writeItems($reader, $options['items']);
84 catch (\Exception $exception) {
88 // Report back with an array of status information.
89 $report = $writer->getReport();
91 // Add the seek position to the report. This is useful for the batch
93 $report['seek'] = $reader->getSeek();