ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Mo.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext\Generators;
4
6
7class Mo extends Generator implements GeneratorInterface
8{
9 public static $includeEmptyTranslations = false;
10
14 public static function toString(Translations $translations)
15 {
16 $array = array();
17 $headers = '';
18
19 foreach ($translations->getHeaders() as $headerName => $headerValue) {
20 $headers .= "$headerName: $headerValue\n";
21 }
22
23 if ($headers !== '') {
24 $array[''] = $headers;
25 }
26
27 foreach ($translations as $translation) {
28 if (!$translation->hasTranslation() && !static::$includeEmptyTranslations) {
29 continue;
30 }
31
32 if ($translation->hasContext()) {
33 $originalString = $translation->getContext()."\x04".$translation->getOriginal();
34 } else {
35 $originalString = $translation->getOriginal();
36 }
37
38 $array[$originalString] = $translation;
39 }
40
41 ksort($array);
42 $numEntries = count($array);
43 $originalsTable = '';
44 $translationsTable = '';
45 $originalsIndex = array();
46 $translationsIndex = array();
47
48 foreach ($array as $originalString => $translation) {
49 if (is_string($translation)) {
50 // Headers
51 $translationString = $translation;
52 } else {
53 /* @var $translation \Gettext\Translation */
54 if ($translation->hasPlural()) {
55 $originalString .= "\x00".$translation->getPlural();
56 }
57 $translationString = $translation->getTranslation();
58
59 if ($translation->hasPluralTranslation()) {
60 $translationString .= "\x00".implode("\x00", $translation->getPluralTranslation());
61 }
62 }
63
64 $originalsIndex[] = array('relativeOffset' => strlen($originalsTable), 'length' => strlen($originalString));
65 $originalsTable .= $originalString."\x00";
66 $translationsIndex[] = array('relativeOffset' => strlen($translationsTable), 'length' => strlen($translationString));
67 $translationsTable .= $translationString."\x00";
68 }
69
70 // Offset of table with the original strings index: right after the header (which is 7 words)
71 $originalsIndexOffset = 7 * 4;
72
73 // Size of table with the original strings index
74 $originalsIndexSize = $numEntries * (4 + 4);
75
76 // Offset of table with the translation strings index: right after the original strings index table
77 $translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize;
78
79 // Size of table with the translation strings index
80 $translationsIndexSize = $numEntries * (4 + 4);
81
82 // Hashing table starts after the header and after the index table
83 $originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize;
84
85 // Translations start after the keys
86 $translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable);
87
88 // Let's generate the .mo file binary data
89 $mo = '';
90
91 // Magic number
92 $mo .= pack('L', 0x950412de);
93
94 // File format revision
95 $mo .= pack('L', 0);
96
97 // Number of strings
98 $mo .= pack('L', $numEntries);
99
100 // Offset of table with original strings
101 $mo .= pack('L', $originalsIndexOffset);
102
103 // Offset of table with translation strings
104 $mo .= pack('L', $translationsIndexOffset);
105
106 // Size of hashing table: we don't use it.
107 $mo .= pack('L', 0);
108
109 // Offset of hashing table: it would start right after the translations index table
110 $mo .= pack('L', $translationsIndexOffset + $translationsIndexSize);
111
112 // Write the lengths & offsets of the original strings
113 foreach ($originalsIndex as $info) {
114 $mo .= pack('L', $info['length']);
115 $mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']);
116 }
117
118 // Write the lengths & offsets of the translated strings
119 foreach ($translationsIndex as $info) {
120 $mo .= pack('L', $info['length']);
121 $mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']);
122 }
123
124 // Write original strings
125 $mo .= $originalsTable;
126
127 // Write translation strings
128 $mo .= $translationsTable;
129
130 return $mo;
131 }
132}
An exception for terminatinating execution or to throw for unit testing.
static $includeEmptyTranslations
Definition: Mo.php:9
static toString(Translations $translations)
{@parentDoc}.
Definition: Mo.php:14
Class to manage a collection of translations.
getHeaders()
Returns all header for this translations.
$info
Definition: index.php:5