Version 1
[yaffs-website] / vendor / symfony / translation / Dumper / IcuResFileDumper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Dumper;
13
14 use Symfony\Component\Translation\MessageCatalogue;
15
16 /**
17  * IcuResDumper generates an ICU ResourceBundle formatted string representation of a message catalogue.
18  *
19  * @author Stealth35
20  */
21 class IcuResFileDumper extends FileDumper
22 {
23     /**
24      * {@inheritdoc}
25      */
26     protected $relativePathTemplate = '%domain%/%locale%.%extension%';
27
28     /**
29      * {@inheritdoc}
30      */
31     public function format(MessageCatalogue $messages, $domain = 'messages')
32     {
33         @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);
34
35         return $this->formatCatalogue($messages, $domain);
36     }
37
38     /**
39      * {@inheritdoc}
40      */
41     public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
42     {
43         $data = $indexes = $resources = '';
44
45         foreach ($messages->all($domain) as $source => $target) {
46             $indexes .= pack('v', strlen($data) + 28);
47             $data .= $source."\0";
48         }
49
50         $data .= $this->writePadding($data);
51
52         $keyTop = $this->getPosition($data);
53
54         foreach ($messages->all($domain) as $source => $target) {
55             $resources .= pack('V', $this->getPosition($data));
56
57             $data .= pack('V', strlen($target))
58                 .mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
59                 .$this->writePadding($data)
60                   ;
61         }
62
63         $resOffset = $this->getPosition($data);
64
65         $data .= pack('v', count($messages))
66             .$indexes
67             .$this->writePadding($data)
68             .$resources
69               ;
70
71         $bundleTop = $this->getPosition($data);
72
73         $root = pack('V7',
74             $resOffset + (2 << 28), // Resource Offset + Resource Type
75             6,                      // Index length
76             $keyTop,                // Index keys top
77             $bundleTop,             // Index resources top
78             $bundleTop,             // Index bundle top
79             count($messages),       // Index max table length
80             0                       // Index attributes
81         );
82
83         $header = pack('vC2v4C12@32',
84             32,                     // Header size
85             0xDA, 0x27,             // Magic number 1 and 2
86             20, 0, 0, 2,            // Rest of the header, ..., Size of a char
87             0x52, 0x65, 0x73, 0x42, // Data format identifier
88             1, 2, 0, 0,             // Data version
89             1, 4, 0, 0              // Unicode version
90         );
91
92         return $header.$root.$data;
93     }
94
95     private function writePadding($data)
96     {
97         $padding = strlen($data) % 4;
98
99         if ($padding) {
100             return str_repeat("\xAA", 4 - $padding);
101         }
102     }
103
104     private function getPosition($data)
105     {
106         return (strlen($data) + 28) / 4;
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     protected function getExtension()
113     {
114         return 'res';
115     }
116 }