@@ -9,13 +9,22 @@ static var POT_PATTERN := RegEx.create_from_string(
99 r '(?<id>msgid (?:""\n (?:"(?:\\ .|[^"\\ ])*"\n )+|"(?:\\ .|[^"\\ ])*"\n ))' +
1010 r '(?<str>msgstr (?:""\n (?:"(?:\\ .|[^"\\ ])*"\n )+|"(?:\\ .|[^"\\ ])*"\n ))'
1111)
12+
13+ static var UNSURE_POT_PATTERN := RegEx .create_from_string (
14+ r '(?<id>#~ msgid (?:""\n (?:"(?:\\ .|[^"\\ ])*"\n )+|"(?:\\ .|[^"\\ ])*"\n ))' +
15+ r '(?<str>#~ msgstr (?:""\n (?:"(?:\\ .|[^"\\ ])*"\n )+|"(?:\\ .|[^"\\ ])*"\n ))'
16+ )
1217static var GLOSSARY_TERM_RE := RegEx .create_from_string (r '\[ glossary term=\\ "([^\\ ]+)\\ "\] ' )
1318static var TAG_RE := RegEx .create_from_string (r '\[ [^\[ ]+\] ([^\[ ]+)\[ [^\[ ]+\] ' )
1419static var SPACE_NEWLINE_RE := RegEx .create_from_string (r '\s +\\ n' )
1520static var WHITESPACE_RE := RegEx .create_from_string (r '\s +' )
1621
1722
18- static func build_tr_blocks (po_file : String , skip_header := true , out_header : Array = []) -> Array [Dictionary ]:
23+ static func get_unsure_tr_blocks (po_file : String , skip_header := true , out_header : Array = []) -> Array [Dictionary ]:
24+ return build_tr_blocks (po_file , skip_header , out_header , UNSURE_POT_PATTERN , 3 )
25+
26+
27+ static func build_tr_blocks (po_file : String , skip_header := true , out_header : Array = [], target_regex := POT_PATTERN , prefix_offset := 0 ) -> Array [Dictionary ]:
1928 var po_text := FileAccess .open (po_file , FileAccess .READ ).get_as_text ()
2029
2130 var start_index := (po_text .find ("\n\n " )+ 2 )
@@ -26,13 +35,13 @@ static func build_tr_blocks(po_file: String, skip_header := true, out_header: Ar
2635 out_header [0 ] = header
2736
2837 var tr_blocks : Array [Dictionary ] = []
29- tr_blocks .append_array (POT_PATTERN .search_all (po_text , start_index ).map (func (block_match : RegExMatch ) -> Dictionary :
38+ tr_blocks .append_array (target_regex .search_all (po_text , start_index ).map (func (block_match : RegExMatch ) -> Dictionary :
3039 return {
3140 "comments" : _parse_course_comment (block_match ),
32- "ctxt" : block_match .get_string ("ctxt" ).substr (9 , block_match .get_string ("ctxt" ).length ()- 11 ),
33- "id" : _parse_course_string (block_match , true ),
34- "normalized_id" : normalize (_parse_course_string (block_match , true )),
35- "str" : _parse_course_string (block_match , false )
41+ "ctxt" : block_match .get_string ("ctxt" ).substr (9 + prefix_offset , block_match .get_string ("ctxt" ).length ()- ( 11 + prefix_offset ) ),
42+ "id" : _parse_course_string (block_match , true , prefix_offset ),
43+ "normalized_id" : normalize (_parse_course_string (block_match , true , prefix_offset )),
44+ "str" : _parse_course_string (block_match , false , prefix_offset )
3645 }
3746 ))
3847 var duplicate_blocks := []
@@ -54,13 +63,18 @@ static func write_from_tr_blocks(po_file: String, header: String, blocks: Array[
5463 var lines := [header ]
5564
5665 for block in blocks :
66+ var append_fuzzy := false
5767 for comment in block .comments .comments :
5868 if comment == "fuzzy" :
59- lines . append ( "#, fuzzy" )
69+ append_fuzzy = true
6070 else :
6171 lines .append ("#. %s " % [comment ])
6272 for source in block .comments .sources :
63- lines .append ("#: %s :%s " % [source .lesson , source .line_number ])
73+ if source .lesson :
74+ lines .append ("#: %s%s " % [source .lesson , ":%s " % [source .line_number ] if source .lesson .get_extension () != "tscn" else "" ])
75+
76+ if append_fuzzy :
77+ lines .append ("#, fuzzy" )
6478
6579 if block .ctxt :
6680 lines .append ('msgctxt "%s "' % [block .ctxt ])
@@ -92,18 +106,21 @@ static func _parse_course_comment(target: RegExMatch) -> Dictionary:
92106
93107 var comments := target .get_string ("comment" ).split ("\n " , false )
94108 for comment in comments :
95- if comment .begins_with ("#: course/ " ):
109+ if comment .begins_with ("#: " ):
96110 var line_number_idx := comment .rfind (":" )
97- var path := comment .substr (3 , line_number_idx - 3 )
98- var line_number := comment .substr (line_number_idx + 1 ).to_int ()
111+ var has_line_number := comment .count (":" ) > 1
112+ var path := comment .substr (3 , (line_number_idx - 3 ) if has_line_number else - 1 )
113+ var line_number := 0
114+ if has_line_number :
115+ line_number = comment .substr (line_number_idx + 1 ).to_int ()
99116 (result .sources as Array ).push_back ({"lesson" : path , "line_number" : line_number })
100117 else :
101118 (result .comments as Array ).push_back (comment .substr (2 if comment .begins_with ("# " ) else 3 ))
102119
103120 return result
104121
105122
106- static func _parse_course_string (target : RegExMatch , is_id : bool ) -> String :
123+ static func _parse_course_string (target : RegExMatch , is_id : bool , prefix_offset : = 0 ) -> String :
107124 var id := target .get_string ("id" if is_id else "str" )
108125
109126 var result := ""
@@ -112,11 +129,11 @@ static func _parse_course_string(target: RegExMatch, is_id: bool) -> String:
112129 var lines := id .split ("\n " ).slice (1 )
113130 for line in lines :
114131 if not line .ends_with ('\n "' ):
115- result += line .substr (1 , line .length ()- 2 )
132+ result += line .substr (1 , line .length ()- ( 2 + prefix_offset ) )
116133 else :
117- result += line .substr (1 , line .length ()- 2 ) + "\n "
134+ result += line .substr (1 , line .length ()- ( 2 + prefix_offset ) ) + "\n "
118135 else :
119- result = id .substr (7 + (0 if is_id else 1 ), id .length ()- (9 + (0 if is_id else 1 )))
136+ result = id .substr (7 + (0 if is_id else 1 ) + prefix_offset , id .length ()- (9 + (0 if is_id else 1 ) + prefix_offset ))
120137
121138 return result
122139
0 commit comments