Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Translation\Dumper;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Translation\Loader\MoFileLoader;
|
Chris@0
|
15 use Symfony\Component\Translation\MessageCatalogue;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * MoFileDumper generates a gettext formatted string representation of a message catalogue.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Stealth35
|
Chris@0
|
21 */
|
Chris@0
|
22 class MoFileDumper extends FileDumper
|
Chris@0
|
23 {
|
Chris@0
|
24 /**
|
Chris@0
|
25 * {@inheritdoc}
|
Chris@0
|
26 */
|
Chris@17
|
27 public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
|
Chris@0
|
28 {
|
Chris@0
|
29 $sources = $targets = $sourceOffsets = $targetOffsets = '';
|
Chris@17
|
30 $offsets = [];
|
Chris@0
|
31 $size = 0;
|
Chris@0
|
32
|
Chris@0
|
33 foreach ($messages->all($domain) as $source => $target) {
|
Chris@17
|
34 $offsets[] = array_map('strlen', [$sources, $source, $targets, $target]);
|
Chris@0
|
35 $sources .= "\0".$source;
|
Chris@0
|
36 $targets .= "\0".$target;
|
Chris@0
|
37 ++$size;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@17
|
40 $header = [
|
Chris@0
|
41 'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
|
Chris@0
|
42 'formatRevision' => 0,
|
Chris@0
|
43 'count' => $size,
|
Chris@0
|
44 'offsetId' => MoFileLoader::MO_HEADER_SIZE,
|
Chris@0
|
45 'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
|
Chris@0
|
46 'sizeHashes' => 0,
|
Chris@0
|
47 'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
|
Chris@17
|
48 ];
|
Chris@0
|
49
|
Chris@17
|
50 $sourcesSize = \strlen($sources);
|
Chris@0
|
51 $sourcesStart = $header['offsetHashes'] + 1;
|
Chris@0
|
52
|
Chris@0
|
53 foreach ($offsets as $offset) {
|
Chris@0
|
54 $sourceOffsets .= $this->writeLong($offset[1])
|
Chris@0
|
55 .$this->writeLong($offset[0] + $sourcesStart);
|
Chris@0
|
56 $targetOffsets .= $this->writeLong($offset[3])
|
Chris@0
|
57 .$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@17
|
60 $output = implode('', array_map([$this, 'writeLong'], $header))
|
Chris@0
|
61 .$sourceOffsets
|
Chris@0
|
62 .$targetOffsets
|
Chris@0
|
63 .$sources
|
Chris@0
|
64 .$targets
|
Chris@0
|
65 ;
|
Chris@0
|
66
|
Chris@0
|
67 return $output;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 protected function getExtension()
|
Chris@0
|
74 {
|
Chris@0
|
75 return 'mo';
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 private function writeLong($str)
|
Chris@0
|
79 {
|
Chris@0
|
80 return pack('V*', $str);
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|