@@ -213,39 +213,42 @@ def escape_ascii(s, encoding):
213213def  escape_nonascii (s , encoding ):
214214    return  '' .join (escapes [b ] for  b  in  s .encode (encoding ))
215215
216- # Split a string according to whitespaces and keep 
217- # the whitespaces in the resulting array thanks to 
218- # the capturing group. 
219- _space_splitter  =  re .compile (r'(\s+)' ).split 
220216
221- def  normalize (s , encoding , options ):
217+ _space_splitter  =  re .compile (r'(\s+)' )
218+ 
219+ def  normalize (s , encoding , prefix , options ):
222220    # This converts the various Python string types into a format that is 
223221    # appropriate for .po files, namely much closer to C style, 
224222    # while wrapping to options.width. 
225223    lines  =  []
226224    for  line  in  s .splitlines (True ):
227-         if  len (escape (line , encoding )) >  options .width  and  ' '  in  line : # don't wrap single words 
228-             words  =  _space_splitter (line )
225+         escaped_line  =  escape (line , encoding )
226+         if  len (escaped_line ) +  len (prefix ) +  2  >  options .width  and  _space_splitter .search (line ):  # don't wrap single words 
227+             words  =  _space_splitter .split (line )
229228            words .reverse ()
230229            buf  =  []
231230            size  =  0 
232231            while  words :
233232                word  =  words .pop ()
234-                 escaped_word_len  =  len (escape (word , encoding ))
233+                 escaped_word  =  escape (word , encoding )
234+                 escaped_word_len  =  len (escaped_word )
235235                new_size  =  size  +  escaped_word_len 
236-                 if  new_size  <=  options .width :
237-                     buf .append (word )
236+                 if  new_size  +  2  <=  options .width :
237+                     buf .append (escaped_word )
238+                     size  =  new_size 
239+                 elif  not  buf :
240+                     buf .append (escaped_word )
238241                    size  =  new_size 
239242                else :
240243                    lines .append ('' .join (buf ))
241-                     buf  =  [word ]
244+                     buf  =  [escaped_word ]
242245                    size  =  escaped_word_len 
243246            lines .append ('' .join (buf ))
244247        else :
245-             lines .append (line )
248+             lines .append (escaped_line )
246249    if  len (lines ) <=  1 :
247250        return  f'"{ escape (s , encoding )}  
248-     return  '""\n '  +  '\n ' .join (f'"{ escape ( line ,  encoding ) }   for  line  in  lines )
251+     return  '""\n '  +  '\n ' .join (f'"{ line }   for  line  in  lines )
249252
250253
251254def  containsAny (str , set ):
@@ -636,10 +639,10 @@ def write_pot_file(messages, options, fp):
636639            # to skip translating some unimportant docstrings. 
637640            print ('#, docstring' , file = fp )
638641        if  msg .msgctxt  is  not None :
639-             print ('msgctxt' , normalize (msg .msgctxt , encoding , options ), file = fp )
640-         print ('msgid' , normalize (msg .msgid , encoding , options ), file = fp )
642+             print ('msgctxt' , normalize (msg .msgctxt , encoding , 'msgctxt' ,  options ), file = fp )
643+         print ('msgid' , normalize (msg .msgid , encoding , 'msgid' ,  options ), file = fp )
641644        if  msg .msgid_plural  is  not None :
642-             print ('msgid_plural' , normalize (msg .msgid_plural , encoding , options ), file = fp )
645+             print ('msgid_plural' , normalize (msg .msgid_plural , encoding , 'msgid_plural' ,  options ), file = fp )
643646            print ('msgstr[0] ""' , file = fp )
644647            print ('msgstr[1] ""\n ' , file = fp )
645648        else :
0 commit comments