Drupal investigation

IcuResFileDumper.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  13. * IcuResDumper generates an ICU ResourceBundle formatted string representation of a message catalogue.
  14. *
  15. * @author Stealth35
  16. */
  17. class IcuResFileDumper extends FileDumper
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected $relativePathTemplate = '%domain%/%locale%.%extension%';
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function format(MessageCatalogue $messages, $domain = 'messages')
  27. {
  28. @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);
  29. return $this->formatCatalogue($messages, $domain);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
  35. {
  36. $data = $indexes = $resources = '';
  37. foreach ($messages->all($domain) as $source => $target) {
  38. $indexes .= pack('v', strlen($data) + 28);
  39. $data .= $source."\0";
  40. }
  41. $data .= $this->writePadding($data);
  42. $keyTop = $this->getPosition($data);
  43. foreach ($messages->all($domain) as $source => $target) {
  44. $resources .= pack('V', $this->getPosition($data));
  45. $data .= pack('V', strlen($target))
  46. .mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
  47. .$this->writePadding($data)
  48. ;
  49. }
  50. $resOffset = $this->getPosition($data);
  51. $data .= pack('v', count($messages))
  52. .$indexes
  53. .$this->writePadding($data)
  54. .$resources
  55. ;
  56. $bundleTop = $this->getPosition($data);
  57. $root = pack('V7',
  58. $resOffset + (2 << 28), // Resource Offset + Resource Type
  59. 6, // Index length
  60. $keyTop, // Index keys top
  61. $bundleTop, // Index resources top
  62. $bundleTop, // Index bundle top
  63. count($messages), // Index max table length
  64. 0 // Index attributes
  65. );
  66. $header = pack('vC2v4C12@32',
  67. 32, // Header size
  68. 0xDA, 0x27, // Magic number 1 and 2
  69. 20, 0, 0, 2, // Rest of the header, ..., Size of a char
  70. 0x52, 0x65, 0x73, 0x42, // Data format identifier
  71. 1, 2, 0, 0, // Data version
  72. 1, 4, 0, 0 // Unicode version
  73. );
  74. return $header.$root.$data;
  75. }
  76. private function writePadding($data)
  77. {
  78. $padding = strlen($data) % 4;
  79. if ($padding) {
  80. return str_repeat("\xAA", 4 - $padding);
  81. }
  82. }
  83. private function getPosition($data)
  84. {
  85. return (strlen($data) + 28) / 4;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function getExtension()
  91. {
  92. return 'res';
  93. }
  94. }