4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Dumper
;
14 use Symfony\Component\Translation\MessageCatalogue
;
17 * IcuResDumper generates an ICU ResourceBundle formatted string representation of a message catalogue.
21 class IcuResFileDumper
implements DumperInterface
26 public function dump(MessageCatalogue
$messages, $options = array())
28 if (!array_key_exists('path', $options)) {
29 throw new \
InvalidArgumentException('The file dumper need a path options.');
32 // save a file for each domain
33 foreach ($messages->getDomains() as $domain) {
34 $file = $messages->getLocale().'.'.$this->getExtension();
35 $path = $options['path'].'/'.$domain.'/';
37 if (!file_exists($path)) {
42 if (file_exists($path.$file)) {
43 copy($path.$file, $path.$file.'~');
47 file_put_contents($path.$file, $this->format($messages, $domain));
54 public function format(MessageCatalogue
$messages, $domain = 'messages')
56 $data = $indexes = $resources = '';
58 foreach ($messages->all($domain) as $source => $target) {
59 $indexes .= pack('v', strlen($data) +
28);
60 $data .= $source."\0";
63 $data .= $this->writePadding($data);
65 $keyTop = $this->getPosition($data);
67 foreach ($messages->all($domain) as $source => $target) {
68 $resources .= pack('V', $this->getPosition($data));
70 $data .= pack('V', strlen($target))
71 .mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
72 .$this->writePadding($data)
76 $resOffset = $this->getPosition($data);
78 $data .= pack('v', count($messages))
80 .$this->writePadding($data)
84 $bundleTop = $this->getPosition($data);
87 $resOffset +
(2 << 28), // Resource Offset + Resource Type
89 $keyTop, // Index keys top
90 $bundleTop, // Index resources top
91 $bundleTop, // Index bundle top
92 count($messages), // Index max table length
96 $header = pack('vC2v4C12@32',
98 0xDA, 0x27, // Magic number 1 and 2
99 20, 0, 0, 2, // Rest of the header, ..., Size of a char
100 0x52, 0x65, 0x73, 0x42, // Data format identifier
101 1, 2, 0, 0, // Data version
102 1, 4, 0, 0 // Unicode version
112 private function writePadding($data)
114 $padding = strlen($data) %
4;
117 return str_repeat("\xAA", 4 - $padding);
121 private function getPosition($data)
123 $position = (strlen($data) +
28) / 4;
131 protected function getExtension()