菜谱项目

MoFileDumper.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
  24. {
  25. $sources = $targets = $sourceOffsets = $targetOffsets = '';
  26. $offsets = array();
  27. $size = 0;
  28. foreach ($messages->all($domain) as $source => $target) {
  29. $offsets[] = array_map('strlen', array($sources, $source, $targets, $target));
  30. $sources .= "\0".$source;
  31. $targets .= "\0".$target;
  32. ++$size;
  33. }
  34. $header = array(
  35. 'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
  36. 'formatRevision' => 0,
  37. 'count' => $size,
  38. 'offsetId' => MoFileLoader::MO_HEADER_SIZE,
  39. 'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
  40. 'sizeHashes' => 0,
  41. 'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
  42. );
  43. $sourcesSize = strlen($sources);
  44. $sourcesStart = $header['offsetHashes'] + 1;
  45. foreach ($offsets as $offset) {
  46. $sourceOffsets .= $this->writeLong($offset[1])
  47. .$this->writeLong($offset[0] + $sourcesStart);
  48. $targetOffsets .= $this->writeLong($offset[3])
  49. .$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
  50. }
  51. $output = implode(array_map(array($this, 'writeLong'), $header))
  52. .$sourceOffsets
  53. .$targetOffsets
  54. .$sources
  55. .$targets
  56. ;
  57. return $output;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function getExtension()
  63. {
  64. return 'mo';
  65. }
  66. private function writeLong($str)
  67. {
  68. return pack('V*', $str);
  69. }
  70. }