@@ -12,86 +12,112 @@ public static function toString(Translations $translations)
1212 {
1313 $ array = array ();
1414 $ headers = '' ;
15+
1516 foreach ($ translations ->getHeaders () as $ headerName => $ headerValue ) {
1617 $ headers .= "$ headerName: $ headerValue \n" ;
1718 }
19+
1820 if (strlen ($ headers )) {
1921 $ array ['' ] = $ headers ;
2022 }
23+
2124 foreach ($ translations as $ translation ) {
2225 if ($ translation ->hasContext ()) {
2326 $ originalString = $ translation ->getContext ()."\x04" .$ translation ->getOriginal ();
2427 } else {
2528 $ originalString = $ translation ->getOriginal ();
2629 }
30+
2731 if ($ translation ->hasPlural ()) {
2832 $ originalString .= "\x00" .$ translation ->getPlural ();
2933 }
34+
3035 $ array [$ originalString ] = $ translation ;
3136 }
37+
3238 ksort ($ array );
3339 $ numEntries = count ($ array );
3440 $ originalsTable = '' ;
3541 $ translationsTable = '' ;
3642 $ originalsIndex = array ();
3743 $ translationsIndex = array ();
44+
3845 foreach ($ array as $ originalString => $ translation ) {
3946 if (is_string ($ translation )) {
4047 // Headers
4148 $ translationString = $ translation ;
4249 } else {
4350 /* @var $translation \Gettext\Translation */
4451 $ translationString = $ translation ->getTranslation ();
52+
4553 if ($ translation ->hasPluralTranslation ()) {
4654 $ translationString .= "\x00" .implode ("\x00" , $ translation ->getPluralTranslation ());
4755 }
4856 }
57+
4958 $ originalsIndex [] = array ('relativeOffset ' => strlen ($ originalsTable ), 'length ' => strlen ($ originalString ));
5059 $ originalsTable .= $ originalString ."\x00" ;
5160 $ translationsIndex [] = array ('relativeOffset ' => strlen ($ translationsTable ), 'length ' => strlen ($ translationString ));
5261 $ translationsTable .= $ translationString ."\x00" ;
5362 }
63+
5464 // Offset of table with the original strings index: right after the header (which is 7 words)
5565 $ originalsIndexOffset = 7 * 4 ;
66+
5667 // Size of table with the original strings index
5768 $ originalsIndexSize = $ numEntries * (4 + 4 );
69+
5870 // Offset of table with the translation strings index: right after the original strings index table
5971 $ translationsIndexOffset = $ originalsIndexOffset + $ originalsIndexSize ;
72+
6073 // Size of table with the translation strings index
6174 $ translationsIndexSize = $ numEntries * (4 + 4 );
75+
6276 // Hashing table starts after the header and after the index table
6377 $ originalsStringsOffset = $ translationsIndexOffset + $ translationsIndexSize ;
78+
6479 // Translations start after the keys
6580 $ translationsStringsOffset = $ originalsStringsOffset + strlen ($ originalsTable );
6681
6782 // Let's generate the .mo file binary data
6883 $ mo = '' ;
84+
6985 // Magic number
7086 $ mo .= pack ('L ' , 0x950412de );
87+
7188 // File format revision
7289 $ mo .= pack ('L ' , 0 );
90+
7391 // Number of strings
7492 $ mo .= pack ('L ' , $ numEntries );
93+
7594 // Offset of table with original strings
7695 $ mo .= pack ('L ' , $ originalsIndexOffset );
96+
7797 // Offset of table with translation strings
7898 $ mo .= pack ('L ' , $ translationsIndexOffset );
99+
79100 // Size of hashing table: we don't use it.
80101 $ mo .= pack ('L ' , 0 );
102+
81103 // Offset of hashing table: it would start right after the translations index table
82104 $ mo .= pack ('L ' , $ translationsIndexOffset + $ translationsIndexSize );
105+
83106 // Write the lengths & offsets of the original strings
84107 foreach ($ originalsIndex as $ info ) {
85108 $ mo .= pack ('L ' , $ info ['length ' ]);
86109 $ mo .= pack ('L ' , $ originalsStringsOffset + $ info ['relativeOffset ' ]);
87110 }
111+
88112 // Write the lengths & offsets of the translated strings
89113 foreach ($ translationsIndex as $ info ) {
90114 $ mo .= pack ('L ' , $ info ['length ' ]);
91115 $ mo .= pack ('L ' , $ translationsStringsOffset + $ info ['relativeOffset ' ]);
92116 }
117+
93118 // Write original strings
94119 $ mo .= $ originalsTable ;
120+
95121 // Write translation strings
96122 $ mo .= $ translationsTable ;
97123
0 commit comments