IcuResFileDumper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 implements DumperInterface
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function dump(MessageCatalogue $messages, $options = array())
  23. {
  24. if (!array_key_exists('path', $options)) {
  25. throw new \InvalidArgumentException('The file dumper need a path options.');
  26. }
  27. // save a file for each domain
  28. foreach ($messages->getDomains() as $domain) {
  29. $file = $messages->getLocale().'.'.$this->getExtension();
  30. $path = $options['path'].'/'.$domain.'/';
  31. if (!file_exists($path)) {
  32. mkdir($path);
  33. }
  34. // backup
  35. if (file_exists($path.$file)) {
  36. copy($path.$file, $path.$file.'~');
  37. }
  38. // save file
  39. file_put_contents($path.$file, $this->format($messages, $domain));
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function format(MessageCatalogue $messages, $domain = 'messages')
  46. {
  47. $data = $indexes = $resources = '';
  48. foreach ($messages->all($domain) as $source => $target) {
  49. $indexes .= pack('v', strlen($data) + 28);
  50. $data .= $source."\0";
  51. }
  52. $data .= $this->writePadding($data);
  53. $keyTop = $this->getPosition($data);
  54. foreach ($messages->all($domain) as $source => $target) {
  55. $resources .= pack('V', $this->getPosition($data));
  56. $data .= pack('V', strlen($target))
  57. .mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
  58. .$this->writePadding($data)
  59. ;
  60. }
  61. $resOffset = $this->getPosition($data);
  62. $data .= pack('v', count($messages))
  63. .$indexes
  64. .$this->writePadding($data)
  65. .$resources
  66. ;
  67. $bundleTop = $this->getPosition($data);
  68. $root = pack('V7',
  69. $resOffset + (2 << 28), // Resource Offset + Resource Type
  70. 6, // Index length
  71. $keyTop, // Index keys top
  72. $bundleTop, // Index resources top
  73. $bundleTop, // Index bundle top
  74. count($messages), // Index max table length
  75. 0 // Index attributes
  76. );
  77. $header = pack('vC2v4C12@32',
  78. 32, // Header size
  79. 0xDA, 0x27, // Magic number 1 and 2
  80. 20, 0, 0, 2, // Rest of the header, ..., Size of a char
  81. 0x52, 0x65, 0x73, 0x42, // Data format identifier
  82. 1, 2, 0, 0, // Data version
  83. 1, 4, 0, 0 // Unicode version
  84. );
  85. $output = $header
  86. .$root
  87. .$data;
  88. return $output;
  89. }
  90. private function writePadding($data)
  91. {
  92. $padding = strlen($data) % 4;
  93. if ($padding) {
  94. return str_repeat("\xAA", 4 - $padding);
  95. }
  96. }
  97. private function getPosition($data)
  98. {
  99. $position = (strlen($data) + 28) / 4;
  100. return $position;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. protected function getExtension()
  106. {
  107. return 'res';
  108. }
  109. }