| 148 |
lars |
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 |
protected $relativePathTemplate = '%domain%/%locale%.%extension%';
|
|
|
24 |
|
|
|
25 |
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
|
|
|
26 |
{
|
|
|
27 |
$data = $indexes = $resources = '';
|
|
|
28 |
|
|
|
29 |
foreach ($messages->all($domain) as $source => $target) {
|
|
|
30 |
$indexes .= pack('v', \strlen($data) + 28);
|
|
|
31 |
$data .= $source."\0";
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
$data .= $this->writePadding($data);
|
|
|
35 |
|
|
|
36 |
$keyTop = $this->getPosition($data);
|
|
|
37 |
|
|
|
38 |
foreach ($messages->all($domain) as $source => $target) {
|
|
|
39 |
$resources .= pack('V', $this->getPosition($data));
|
|
|
40 |
|
|
|
41 |
$data .= pack('V', \strlen($target))
|
|
|
42 |
.mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
|
|
|
43 |
.$this->writePadding($data)
|
|
|
44 |
;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$resOffset = $this->getPosition($data);
|
|
|
48 |
|
|
|
49 |
$data .= pack('v', \count($messages->all($domain)))
|
|
|
50 |
.$indexes
|
|
|
51 |
.$this->writePadding($data)
|
|
|
52 |
.$resources
|
|
|
53 |
;
|
|
|
54 |
|
|
|
55 |
$bundleTop = $this->getPosition($data);
|
|
|
56 |
|
|
|
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->all($domain)), // Index max table length
|
|
|
64 |
|
|
|
65 |
);
|
|
|
66 |
|
|
|
67 |
$header = pack('vC2v4C12@32',
|
|
|
68 |
32, // Header size
|
|
|
69 |
0xDA, 0x27, // Magic number 1 and 2
|
|
|
70 |
20, 0, 0, 2, // Rest of the header, ..., Size of a char
|
|
|
71 |
0x52, 0x65, 0x73, 0x42, // Data format identifier
|
|
|
72 |
1, 2, 0, 0, // Data version
|
|
|
73 |
1, 4, 0, 0 // Unicode version
|
|
|
74 |
);
|
|
|
75 |
|
|
|
76 |
return $header.$root.$data;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function writePadding(string $data): ?string
|
|
|
80 |
{
|
|
|
81 |
$padding = \strlen($data) % 4;
|
|
|
82 |
|
|
|
83 |
return $padding ? str_repeat("\xAA", 4 - $padding) : null;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
private function getPosition(string $data)
|
|
|
87 |
{
|
|
|
88 |
return (\strlen($data) + 28) / 4;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
protected function getExtension(): string
|
|
|
92 |
{
|
|
|
93 |
return 'res';
|
|
|
94 |
}
|
|
|
95 |
}
|