11<?php
2-
32namespace Gettext \Extractors ;
43
4+ use Exception ;
55use Gettext \Translations ;
66use Gettext \Utils \StringReader ;
77
@@ -31,7 +31,7 @@ public static function fromString($string, Translations $translations = null, $f
3131 } elseif ($ magic === (self ::MAGIC2 & 0xFFFFFFFF )) {
3232 $ byteOrder = 'N ' ; //big endian
3333 } else {
34- throw new \ Exception ('Not MO file ' );
34+ throw new Exception ('Not MO file ' );
3535 }
3636
3737 self ::readInt ($ stream , $ byteOrder );
@@ -42,56 +42,67 @@ public static function fromString($string, Translations $translations = null, $f
4242
4343 $ stream ->seekto ($ originals );
4444 $ table_originals = self ::readIntArray ($ stream , $ byteOrder , $ total * 2 );
45+
4546 $ stream ->seekto ($ tran );
4647 $ table_translations = self ::readIntArray ($ stream , $ byteOrder , $ total * 2 );
4748
4849 for ($ i = 0 ; $ i < $ total ; ++$ i ) {
49- $ stream ->seekto ($ table_originals [$ i * 2 + 2 ]);
50- $ original = $ stream ->read ($ table_originals [$ i * 2 + 1 ]);
51- $ stream ->seekto ($ table_translations [$ i * 2 + 2 ]);
52- $ translated = $ stream ->read ($ table_translations [$ i * 2 + 1 ]);
50+ $ next = $ i * 2 ;
51+
52+ $ stream ->seekto ($ table_originals [$ next + 2 ]);
53+ $ original = $ stream ->read ($ table_originals [$ next + 1 ]);
54+
55+ $ stream ->seekto ($ table_translations [$ next + 2 ]);
56+ $ translated = $ stream ->read ($ table_translations [$ next + 1 ]);
5357
5458 if ($ original === '' ) {
5559 // Headers
5660 foreach (explode ("\n" , $ translated ) as $ headerLine ) {
57- if ($ headerLine !== '' ) {
58- $ headerChunks = preg_split ('/:\s*/ ' , $ headerLine , 2 );
59- $ translations ->setHeader ($ headerChunks [0 ], isset ($ headerChunks [1 ]) ? $ headerChunks [1 ] : '' );
61+ if ($ headerLine === '' ) {
62+ continue ;
6063 }
64+
65+ $ headerChunks = preg_split ('/:\s*/ ' , $ headerLine , 2 );
66+ $ translations ->setHeader ($ headerChunks [0 ], isset ($ headerChunks [1 ]) ? $ headerChunks [1 ] : '' );
6167 }
68+
69+ continue ;
70+ }
71+
72+ $ chunks = explode ("\x04" , $ original , 2 );
73+
74+ if (isset ($ chunks [1 ])) {
75+ $ context = $ chunks [0 ];
76+ $ original = $ chunks [1 ];
6277 } else {
63- $ chunks = explode ("\x04" , $ original , 2 );
78+ $ context = '' ;
79+ }
6480
65- if (isset ($ chunks [1 ])) {
66- $ context = $ chunks [0 ];
67- $ original = $ chunks [1 ];
68- } else {
69- $ context = '' ;
70- }
81+ $ chunks = explode ("\x00" , $ original , 2 );
7182
72- $ chunks = explode ("\x00" , $ original , 2 );
83+ if (isset ($ chunks [1 ])) {
84+ $ original = $ chunks [0 ];
85+ $ plural = $ chunks [1 ];
86+ } else {
87+ $ plural = '' ;
88+ }
7389
74- if (isset ($ chunks [1 ])) {
75- $ original = $ chunks [0 ];
76- $ plural = $ chunks [1 ];
77- } else {
78- $ plural = '' ;
79- }
90+ $ translation = $ translations ->insert ($ context , $ original , $ plural );
8091
81- $ translation = $ translations -> insert ( $ context , $ original , $ plural );
82-
83- if ( $ translated !== '' ) {
84- if ( $ plural === '' ) {
85- $ translation -> setTranslation ( $ translated );
86- } else {
87- foreach ( explode ( "\x00" , $ translated ) as $ pluralIndex => $ pluralValue ) {
88- if ( $ pluralIndex === 0 ) {
89- $ translation -> setTranslation ( $ pluralValue );
90- } else {
91- $ translation -> setPluralTranslation ( $ pluralValue , $ pluralIndex - 1 );
92- }
93- }
94- }
92+ if ( $ translated === '' ) {
93+ continue ;
94+ }
95+
96+ if ( $ plural === '' ) {
97+ $ translation -> setTranslation ( $ translated );
98+ continue ;
99+ }
100+
101+ foreach ( explode ( "\x00" , $ translated ) as $ pluralIndex => $ pluralValue ) {
102+ if ( $ pluralIndex === 0 ) {
103+ $ translation -> setTranslation ( $ pluralValue );
104+ } else {
105+ $ translation -> setPluralTranslation ( $ pluralValue , $ pluralIndex - 1 );
95106 }
96107 }
97108 }
@@ -124,3 +135,4 @@ private static function readIntArray(StringReader $stream, $byteOrder, $count)
124135 return unpack ($ byteOrder .$ count , $ stream ->read (4 * $ count ));
125136 }
126137}
138+
0 commit comments