Gettext.php 3.0 KB

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