CsvFileDumper.php
2 months ago
DumperInterface.php
2 months ago
FileDumper.php
2 months ago
IcuResFileDumper.php
2 months ago
IniFileDumper.php
2 months ago
JsonFileDumper.php
2 months ago
MoFileDumper.php
2 months ago
PhpFileDumper.php
2 months ago
PoFileDumper.php
2 months ago
QtFileDumper.php
2 months ago
XliffFileDumper.php
2 months ago
YamlFileDumper.php
2 months ago
IcuResFileDumper.php
106 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\Translation\Dumper; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Translation\MessageCatalogue; |
| 14 | /** |
| 15 | * IcuResDumper generates an ICU ResourceBundle formatted string representation of a message catalogue. |
| 16 | * |
| 17 | * @author Stealth35 |
| 18 | */ |
| 19 | class IcuResFileDumper extends FileDumper |
| 20 | { |
| 21 | /** |
| 22 | * {@inheritdoc} |
| 23 | */ |
| 24 | protected $relativePathTemplate = '%domain%/%locale%.%extension%'; |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []) |
| 29 | { |
| 30 | $data = $indexes = $resources = ''; |
| 31 | foreach ($messages->all($domain) as $source => $target) { |
| 32 | $indexes .= pack('v', \strlen($data) + 28); |
| 33 | $data .= $source . "\x00"; |
| 34 | } |
| 35 | $data .= $this->writePadding($data); |
| 36 | $keyTop = $this->getPosition($data); |
| 37 | foreach ($messages->all($domain) as $source => $target) { |
| 38 | $resources .= pack('V', $this->getPosition($data)); |
| 39 | $data .= pack('V', \strlen($target)) . mb_convert_encoding($target . "\x00", 'UTF-16LE', 'UTF-8') . $this->writePadding($data); |
| 40 | } |
| 41 | $resOffset = $this->getPosition($data); |
| 42 | $data .= pack('v', \count($messages->all($domain))) . $indexes . $this->writePadding($data) . $resources; |
| 43 | $bundleTop = $this->getPosition($data); |
| 44 | $root = pack( |
| 45 | 'V7', |
| 46 | $resOffset + (2 << 28), |
| 47 | // Resource Offset + Resource Type |
| 48 | 6, |
| 49 | // Index length |
| 50 | $keyTop, |
| 51 | // Index keys top |
| 52 | $bundleTop, |
| 53 | // Index resources top |
| 54 | $bundleTop, |
| 55 | // Index bundle top |
| 56 | \count($messages->all($domain)), |
| 57 | // Index max table length |
| 58 | 0 |
| 59 | ); |
| 60 | $header = pack( |
| 61 | 'vC2v4C12@32', |
| 62 | 32, |
| 63 | // Header size |
| 64 | 0xda, |
| 65 | 0x27, |
| 66 | // Magic number 1 and 2 |
| 67 | 20, |
| 68 | 0, |
| 69 | 0, |
| 70 | 2, |
| 71 | // Rest of the header, ..., Size of a char |
| 72 | 0x52, |
| 73 | 0x65, |
| 74 | 0x73, |
| 75 | 0x42, |
| 76 | // Data format identifier |
| 77 | 1, |
| 78 | 2, |
| 79 | 0, |
| 80 | 0, |
| 81 | // Data version |
| 82 | 1, |
| 83 | 4, |
| 84 | 0, |
| 85 | 0 |
| 86 | ); |
| 87 | return $header . $root . $data; |
| 88 | } |
| 89 | private function writePadding(string $data): ?string |
| 90 | { |
| 91 | $padding = \strlen($data) % 4; |
| 92 | return $padding ? str_repeat("\xaa", 4 - $padding) : null; |
| 93 | } |
| 94 | private function getPosition(string $data) |
| 95 | { |
| 96 | return (\strlen($data) + 28) / 4; |
| 97 | } |
| 98 | /** |
| 99 | * {@inheritdoc} |
| 100 | */ |
| 101 | protected function getExtension() |
| 102 | { |
| 103 | return 'res'; |
| 104 | } |
| 105 | } |
| 106 |