Gettext.php 3.0 KB

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