Drupal investigation

MoFileDumper.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Dumper;
  11. use Symfony\Component\Translation\MessageCatalogue;
  12. use Symfony\Component\Translation\Loader\MoFileLoader;
  13. /**
  14. * MoFileDumper generates a gettext formatted string representation of a message catalogue.
  15. *
  16. * @author Stealth35
  17. */
  18. class MoFileDumper extends FileDumper
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function format(MessageCatalogue $messages, $domain = 'messages')
  24. {
  25. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.', E_USER_DEPRECATED);
  26. return $this->formatCatalogue($messages, $domain);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
  32. {
  33. $sources = $targets = $sourceOffsets = $targetOffsets = '';
  34. $offsets = array();
  35. $size = 0;
  36. foreach ($messages->all($domain) as $source => $target) {
  37. $offsets[] = array_map('strlen', array($sources, $source, $targets, $target));
  38. $sources .= "\0".$source;
  39. $targets .= "\0".$target;
  40. ++$size;
  41. }
  42. $header = array(
  43. 'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
  44. 'formatRevision' => 0,
  45. 'count' => $size,
  46. 'offsetId' => MoFileLoader::MO_HEADER_SIZE,
  47. 'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
  48. 'sizeHashes' => 0,
  49. 'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
  50. );
  51. $sourcesSize = strlen($sources);
  52. $sourcesStart = $header['offsetHashes'] + 1;
  53. foreach ($offsets as $offset) {
  54. $sourceOffsets .= $this->writeLong($offset[1])
  55. .$this->writeLong($offset[0] + $sourcesStart);
  56. $targetOffsets .= $this->writeLong($offset[3])
  57. .$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
  58. }
  59. $output = implode(array_map(array($this, 'writeLong'), $header))
  60. .$sourceOffsets
  61. .$targetOffsets
  62. .$sources
  63. .$targets
  64. ;
  65. return $output;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function getExtension()
  71. {
  72. return 'mo';
  73. }
  74. private function writeLong($str)
  75. {
  76. return pack('V*', $str);
  77. }
  78. }