@@ -404,39 +404,64 @@ def print_set(name, set, orig_dict):
404404# Strangely, if you copy the '.../en/definitions.yaml' and paste that into translate.google.com, it does it all.
405405# Rather than waste a bunch more time on this, the code assumes you've translated the file already and stored
406406# it in 'google-defs.yaml' in the current dir
407- # It then goes through the English version leaving the English and pulling out only the translated *values*
407+ # It then goes through the English version leaving the English and pulling out only the translated *values*
408408# from 'google-defs.yaml' writing '[lang]-definitions.yaml'.
409409def translate_definitions (path_to_mathcat : str , lang : str ):
410410 if lang == 'nb' or lang == 'nn' :
411411 lang = 'no' # google doesn't know those variants
412412
413- file_to_translate = "{}/Rules/Languages/en/definitions.yaml" .format (path_to_mathcat )
413+ file_to_translate = f'{ path_to_mathcat } /Rules/Languages/en/definitions.yaml'
414+ translated_file = f'{ path_to_mathcat } /Rules/Languages/{ lang } /definitions.yaml'
414415 with open ("google-defs.yaml" , 'r' , encoding = 'utf8' ) as google_stream :
415416 translated_lines = google_stream .readlines ()
416417 with open (file_to_translate , 'r' , encoding = 'utf8' ) as in_stream :
417- with open (f" { lang } /definitions.yaml" , 'w' , encoding = 'utf8' ) as out_stream :
418+ with open (translated_file , 'w' , encoding = 'utf8' ) as out_stream :
418419 lines = in_stream .readlines ()
419- i = 0
420+ i_en = 0
421+ i_trans = 0
420422 n_lines = len (lines )
421- while i < n_lines :
422- if not (lines [i ].startswith ('#' )) and lines [i ].find (': [' ) >= 0 :
423+ while i_en < n_lines :
424+ if not (lines [i_en ].startswith ('#' )) and ( lines [i_en ].find (': [' ) >= 0 or lines [ i_en ]. find ( ': {' ) >= 0 ) :
423425 # handles 'xxx: [' inclusive of the line with the matching ']'
424- i = translate_definition (i , lines , translated_lines , out_stream )
426+ ( i_en , i_trans ) = translate_definition (i_en , lines , i_trans , translated_lines , out_stream )
425427 else :
426- out_stream .write (lines [i ])
427- i += 1
428-
429-
430- def translate_definition (start : int , lines : list [str ], translated_lines : list [str ], out_stream ):
431- out_stream .write (lines [start ])
432- i = start + 1 # first line is 'name: ['
433- while i < len (lines ):
434- if lines [i ].find (']' ) >= 0 :
435- out_stream .write (lines [i ])
436- return i
437- out_stream .write (translated_lines [i ].replace ("“" , "'" ).replace ("”" , "'" ).replace ("、" , "," )) # Chinese
438- i += 1
439- return i
428+ out_stream .write (lines [i_en ])
429+ i_en += 1
430+ # i_trans += 1
431+
432+
433+ def translate_definition (i_en : int , lines : list [str ], i_trans : int , translated_lines : list [str ], out_stream ) -> (int , int ):
434+ out_stream .write (lines [i_en ])
435+ i_en = i_en + 1 # first line is 'name: [' or 'name: {'
436+ # sync lines -- find '[' in translation
437+ while not (translated_lines [i_trans ].find (': [' ) >= 0 or translated_lines [i_trans ].find (': {' ) >= 0 ):
438+ i_trans += 1
439+ i_trans += 1 # skip the [/{ line
440+ while i_en < len (lines ):
441+ if (
442+ translated_lines [i_en ].strip ().startswith ('#' ) or
443+ len (translated_lines [i_en ].strip ()) == 0 and len (translated_lines [i_trans ].strip ()) == 0
444+ ):
445+ out_stream .write (lines [i_en ])
446+ elif lines [i_en ].find (']' ) >= 0 or lines [i_en ].find ('}' ) >= 0 :
447+ out_stream .write (lines [i_en ])
448+ return (i_en , i_trans )
449+ elif len (translated_lines [i_trans ].strip ()) == 0 : # google sometimes adds blank lines
450+ i_trans += 1
451+ continue
452+ else :
453+ print (f'en: { lines [i_en ].strip ()} \n tr: { translated_lines [i_trans ].strip ()} ' )
454+ # get indentation right
455+ i_spaces = lines [i_en ].find ('"' )
456+ cleaned_line = (
457+ translated_lines [i_trans ]
458+ .replace ("“" , '"' ).replace ("”" , '"' ).replace ("„" , '"' ).replace ("、" , "," ) # Chinese
459+ .lstrip ()
460+ )
461+ out_stream .write (f'{ " " .ljust (i_spaces )} ' + cleaned_line )
462+ i_en += 1
463+ i_trans += 1
464+ return (i_en , i_trans )
440465
441466
442467def build_euro (lang : str ):
@@ -502,7 +527,7 @@ def write_euro_braille_file():
502527# (sre_only, mp_only, differ, same) = dict_compare("fr", get_sre_unicode_dict(SRE_Location, "fr"), get_mathplayer_unicode_dict(MP_Location, "fr"))
503528# (sre_only, mp_only, differ, same) = dict_compare("it", get_sre_unicode_dict(SRE_Location, "it"), get_mathplayer_unicode_dict(MP_Location, "it"))
504529
505- language = "ru "
530+ language = "pl "
506531# build_new_translation("..", language, "unicode")
507532# build_new_translation("..", language, "unicode-full")
508533
0 commit comments