diff options
Diffstat (limited to 'vendor/symfony/translation/Symfony/Component/Translation/Dumper/IcuResFileDumper.php')
-rw-r--r-- | vendor/symfony/translation/Symfony/Component/Translation/Dumper/IcuResFileDumper.php | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Dumper/IcuResFileDumper.php b/vendor/symfony/translation/Symfony/Component/Translation/Dumper/IcuResFileDumper.php new file mode 100644 index 00000000..979153ac --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Dumper/IcuResFileDumper.php | |||
@@ -0,0 +1,135 @@ | |||
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 implements DumperInterface | ||
22 | { | ||
23 | /** | ||
24 | * {@inheritDoc} | ||
25 | */ | ||
26 | public function dump(MessageCatalogue $messages, $options = array()) | ||
27 | { | ||
28 | if (!array_key_exists('path', $options)) { | ||
29 | throw new \InvalidArgumentException('The file dumper need a path options.'); | ||
30 | } | ||
31 | |||
32 | // save a file for each domain | ||
33 | foreach ($messages->getDomains() as $domain) { | ||
34 | $file = $messages->getLocale().'.'.$this->getExtension(); | ||
35 | $path = $options['path'].'/'.$domain.'/'; | ||
36 | |||
37 | if (!file_exists($path)) { | ||
38 | mkdir($path); | ||
39 | } | ||
40 | |||
41 | // backup | ||
42 | if (file_exists($path.$file)) { | ||
43 | copy($path.$file, $path.$file.'~'); | ||
44 | } | ||
45 | |||
46 | // save file | ||
47 | file_put_contents($path.$file, $this->format($messages, $domain)); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * {@inheritDoc} | ||
53 | */ | ||
54 | public function format(MessageCatalogue $messages, $domain = 'messages') | ||
55 | { | ||
56 | $data = $indexes = $resources = ''; | ||
57 | |||
58 | foreach ($messages->all($domain) as $source => $target) { | ||
59 | $indexes .= pack('v', strlen($data) + 28); | ||
60 | $data .= $source."\0"; | ||
61 | } | ||
62 | |||
63 | $data .= $this->writePadding($data); | ||
64 | |||
65 | $keyTop = $this->getPosition($data); | ||
66 | |||
67 | foreach ($messages->all($domain) as $source => $target) { | ||
68 | $resources .= pack('V', $this->getPosition($data)); | ||
69 | |||
70 | $data .= pack('V', strlen($target)) | ||
71 | .mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8') | ||
72 | .$this->writePadding($data) | ||
73 | ; | ||
74 | } | ||
75 | |||
76 | $resOffset = $this->getPosition($data); | ||
77 | |||
78 | $data .= pack('v', count($messages)) | ||
79 | .$indexes | ||
80 | .$this->writePadding($data) | ||
81 | .$resources | ||
82 | ; | ||
83 | |||
84 | $bundleTop = $this->getPosition($data); | ||
85 | |||
86 | $root = pack('V7', | ||
87 | $resOffset + (2 << 28), // Resource Offset + Resource Type | ||
88 | 6, // Index length | ||
89 | $keyTop, // Index keys top | ||
90 | $bundleTop, // Index resources top | ||
91 | $bundleTop, // Index bundle top | ||
92 | count($messages), // Index max table length | ||
93 | 0 // Index attributes | ||
94 | ); | ||
95 | |||
96 | $header = pack('vC2v4C12@32', | ||
97 | 32, // Header size | ||
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 | ||
103 | ); | ||
104 | |||
105 | $output = $header | ||
106 | .$root | ||
107 | .$data; | ||
108 | |||
109 | return $output; | ||
110 | } | ||
111 | |||
112 | private function writePadding($data) | ||
113 | { | ||
114 | $padding = strlen($data) % 4; | ||
115 | |||
116 | if ($padding) { | ||
117 | return str_repeat("\xAA", 4 - $padding); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | private function getPosition($data) | ||
122 | { | ||
123 | $position = (strlen($data) + 28) / 4; | ||
124 | |||
125 | return $position; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * {@inheritDoc} | ||
130 | */ | ||
131 | protected function getExtension() | ||
132 | { | ||
133 | return 'res'; | ||
134 | } | ||
135 | } | ||