Gettext.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 stdClass $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, defaults to
  29. * 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. * @see PoDatabaseWriter
  39. */
  40. static function fileToDatabase($file, $options) {
  41. // Add the default values to the options array.
  42. $options += array(
  43. 'overwrite_options' => array(),
  44. 'customized' => L10N_UPDATE_NOT_CUSTOMIZED,
  45. 'items' => -1,
  46. 'seek' => 0,
  47. );
  48. // Instantiate and initialize the stream reader for this file.
  49. $reader = new PoStreamReader();
  50. $reader->setLangcode($file->langcode);
  51. $reader->setURI($file->uri);
  52. try {
  53. $reader->open();
  54. }
  55. catch (\Exception $exception) {
  56. throw $exception;
  57. }
  58. $header = $reader->getHeader();
  59. if (!$header) {
  60. throw new \Exception('Missing or malformed header.');
  61. }
  62. // Initialize the database writer.
  63. $writer = new PoDatabaseWriter();
  64. $writer->setLangcode($file->langcode);
  65. $writer_options = array(
  66. 'overwrite_options' => $options['overwrite_options'],
  67. 'customized' => $options['customized'],
  68. );
  69. $writer->setOptions($writer_options);
  70. $writer->setHeader($header);
  71. // Attempt to pipe all items from the file to the database.
  72. try {
  73. if ($options['seek']) {
  74. $reader->setSeek($options['seek']);
  75. }
  76. $writer->writeItems($reader, $options['items']);
  77. }
  78. catch (\Exception $exception) {
  79. throw $exception;
  80. }
  81. // Report back with an array of status information.
  82. $report = $writer->getReport();
  83. // Add the seek position to the report. This is useful for the batch
  84. // operation.
  85. $report['seek'] = $reader->getSeek();
  86. return $report;
  87. }
  88. }