| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Dumper; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\MessageCatalogue; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class IcuResFileDumper extends FileDumper |
| 22 |
{ |
| 23 |
protected string $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), |
| 59 |
6, |
| 60 |
$keyTop, |
| 61 |
$bundleTop, |
| 62 |
$bundleTop, |
| 63 |
\count($messages->all($domain)), |
| 64 |
0 |
| 65 |
); |
| 66 |
|
| 67 |
$header = pack('vC2v4C12@32', |
| 68 |
32, |
| 69 |
0xDA, 0x27, |
| 70 |
20, 0, 0, 2, |
| 71 |
0x52, 0x65, 0x73, 0x42, |
| 72 |
1, 2, 0, 0, |
| 73 |
1, 4, 0, 0 |
| 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): float|int |
| 87 |
{ |
| 88 |
return (\strlen($data) + 28) / 4; |
| 89 |
} |
| 90 |
|
| 91 |
protected function getExtension(): string |
| 92 |
{ |
| 93 |
return 'res'; |
| 94 |
} |
| 95 |
} |