1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext\Generators; |
4
|
|
|
|
5
|
|
|
use Gettext\Translations; |
6
|
|
|
use Gettext\Utils\HeadersGeneratorTrait; |
7
|
|
|
|
8
|
|
|
class Mo extends Generator implements GeneratorInterface |
9
|
|
|
{ |
10
|
|
|
use HeadersGeneratorTrait; |
11
|
|
|
|
12
|
|
|
public static $options = [ |
13
|
|
|
'includeHeaders' => true, |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@parentDoc}. |
18
|
|
|
*/ |
19
|
|
|
public static function toString(Translations $translations, array $options = []) |
20
|
|
|
{ |
21
|
|
|
$options += static::$options; |
22
|
|
|
$messages = []; |
23
|
|
|
|
24
|
|
|
if ($options['includeHeaders']) { |
25
|
|
|
$messages[''] = static::generateHeaders($translations); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
foreach ($translations as $translation) { |
29
|
|
|
if (!$translation->hasTranslation() || $translation->isDisabled()) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($translation->hasContext()) { |
34
|
|
|
$originalString = $translation->getContext()."\x04".$translation->getOriginal(); |
35
|
|
|
} else { |
36
|
|
|
$originalString = $translation->getOriginal(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$messages[$originalString] = $translation; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
ksort($messages); |
43
|
|
|
$numEntries = count($messages); |
44
|
|
|
$originalsTable = ''; |
45
|
|
|
$translationsTable = ''; |
46
|
|
|
$originalsIndex = []; |
47
|
|
|
$translationsIndex = []; |
48
|
|
|
$pluralForm = $translations->getPluralForms(); |
49
|
|
|
$pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
50
|
|
|
|
51
|
|
|
foreach ($messages as $originalString => $translation) { |
52
|
|
|
if (is_string($translation)) { |
53
|
|
|
// Headers |
54
|
|
|
$translationString = $translation; |
55
|
|
|
} else { |
56
|
|
|
/* @var $translation \Gettext\Translation */ |
57
|
|
|
if ($translation->hasPlural() && $translation->hasPluralTranslations(true)) { |
58
|
|
|
$originalString .= "\x00".$translation->getPlural(); |
59
|
|
|
$translationString = $translation->getTranslation(); |
60
|
|
|
$translationString .= "\x00".implode("\x00", $translation->getPluralTranslations($pluralSize)); |
61
|
|
|
} else { |
62
|
|
|
$translationString = $translation->getTranslation(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$originalsIndex[] = [ |
67
|
|
|
'relativeOffset' => strlen($originalsTable), |
68
|
|
|
'length' => strlen($originalString) |
69
|
|
|
]; |
70
|
|
|
$originalsTable .= $originalString."\x00"; |
71
|
|
|
$translationsIndex[] = [ |
72
|
|
|
'relativeOffset' => strlen($translationsTable), |
73
|
|
|
'length' => strlen($translationString) |
74
|
|
|
]; |
75
|
|
|
$translationsTable .= $translationString."\x00"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Offset of table with the original strings index: right after the header (which is 7 words) |
79
|
|
|
$originalsIndexOffset = 7 * 4; |
80
|
|
|
|
81
|
|
|
// Size of table with the original strings index |
82
|
|
|
$originalsIndexSize = $numEntries * (4 + 4); |
83
|
|
|
|
84
|
|
|
// Offset of table with the translation strings index: right after the original strings index table |
85
|
|
|
$translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize; |
86
|
|
|
|
87
|
|
|
// Size of table with the translation strings index |
88
|
|
|
$translationsIndexSize = $numEntries * (4 + 4); |
89
|
|
|
|
90
|
|
|
// Hashing table starts after the header and after the index table |
91
|
|
|
$originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize; |
92
|
|
|
|
93
|
|
|
// Translations start after the keys |
94
|
|
|
$translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable); |
95
|
|
|
|
96
|
|
|
// Let's generate the .mo file binary data |
97
|
|
|
$mo = ''; |
98
|
|
|
|
99
|
|
|
// Magic number |
100
|
|
|
$mo .= pack('L', 0x950412de); |
101
|
|
|
|
102
|
|
|
// File format revision |
103
|
|
|
$mo .= pack('L', 0); |
104
|
|
|
|
105
|
|
|
// Number of strings |
106
|
|
|
$mo .= pack('L', $numEntries); |
107
|
|
|
|
108
|
|
|
// Offset of table with original strings |
109
|
|
|
$mo .= pack('L', $originalsIndexOffset); |
110
|
|
|
|
111
|
|
|
// Offset of table with translation strings |
112
|
|
|
$mo .= pack('L', $translationsIndexOffset); |
113
|
|
|
|
114
|
|
|
// Size of hashing table: we don't use it. |
115
|
|
|
$mo .= pack('L', 0); |
116
|
|
|
|
117
|
|
|
// Offset of hashing table: it would start right after the translations index table |
118
|
|
|
$mo .= pack('L', $translationsIndexOffset + $translationsIndexSize); |
119
|
|
|
|
120
|
|
|
// Write the lengths & offsets of the original strings |
121
|
|
View Code Duplication |
foreach ($originalsIndex as $info) { |
|
|
|
|
122
|
|
|
$mo .= pack('L', $info['length']); |
123
|
|
|
$mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Write the lengths & offsets of the translated strings |
127
|
|
View Code Duplication |
foreach ($translationsIndex as $info) { |
|
|
|
|
128
|
|
|
$mo .= pack('L', $info['length']); |
129
|
|
|
$mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Write original strings |
133
|
|
|
$mo .= $originalsTable; |
134
|
|
|
|
135
|
|
|
// Write translation strings |
136
|
|
|
$mo .= $translationsTable; |
137
|
|
|
|
138
|
|
|
return $mo; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.